webtest.parser

Provides classes for parsing Visual Studio .webtest XML files.

The Visual Studio .webtest XML format does not seem to be documented anywhere, so all the elements and attributes used here are inferred from the .webtest files written by Fiddler.

This module defines three important classes:

Webtest
Parses a .webtest file and gets a list of Request objects
Request
Stores attributes and contents relevant to a single HTTP(S) request
WebtestHandler
Used internally by the sax XML parser

The Webtest class is the one you’re most likely to use directly. Simply provide the name of a .webtest XML file:

>>> my_test = Webtest('my_test.webtest')

If you provide a filename to the constructor, parsing is done automatically. Alternatively, you can delay parsing until later:

>>> my_test = Webtest()
...
>>> my_test.load('my_test.webtest')

After parsing, the Webtest object will contain a list of Request objects. You can print all requests in summarized form:

>>> print(my_test)

Or iterate over the requests and do something with them:

>>> for request in my_test.requests:
...     do_something(request)

The .webtest file is expected to have one or more Request elements, similar to the one below:

<Request Method="POST" Url="http://www.example.com/">
  <Headers>
    <Header Name="Content-Type" Value="text/plain" />
  </Headers>
  <FormPostHttpBody ContentType="text/plain">
    <FormPostParameter Name="username" Value="phil" />
    <FormPostParameter Name="session_id" Value="12345" />
  </FormPostHttpBody>
</Request>

Each <Request>...</Request> defines a single HTTP request to a particular URL, using a method of GET or POST. Headers are enclosed in a <Header .../> element, and any parameters are sent using <FormPostParameter .../>, both of which have Name and Value attributes.

Two additional elements are understood by this parser:

<Description>...</Description>
A human-readable string that describes what the request does
<Capture>...</Capture>
A block of expressions that may be used to capture or verify content in the body of the response for this request

This module is designed to be used with the webtest.runner module, which is specifically designed to work with the Grinder load test framework, but the parser defined here is not Grinder-specific, and can be used for more general-purpose parsing of .webtest files.

exception webtest.parser.MalformedXML

Raised when any malformed XML is encountered.

class webtest.parser.Request(attrs, line_number=0)

Store attributes pertaining to an HTTP Request, including:

url
The full URL path of the request
headers
A list of (Name, Value) for the request header
parameters
A list of (Name, Value) for the request parameters
add_header(attrs)

Add a header (Name, Value) pair to the request.

attrs
A dict including ‘Name’ and ‘Value’ items

If the ‘Name’ or ‘Value’ attributes are not defined, or if the ‘Name’ attribute is empty, nothing is added.

add_parameter(attrs)

Add a parameter (Name, Value) pair to the request.

attrs
A dict including ‘Name’ and ‘Value’ items

If the ‘Name’ or ‘Value’ attributes are not defined, or if the ‘Name’ attribute is empty, nothing is added.

captures()

Return capture expressions as a list of strings.

Normally, self.capture will be a literal block of text as it appeared inside the Capture element; it may contain extra spaces and newlines. This method strips out the extra newlines and whitespace, and converts to a list for easy iteration over each capture expression.

class webtest.parser.Webtest(filename='')

Webtest XML file parser.

load(filename)

Load and parse the given .webtest XML file, and store the list of requests found in it.

parse(filename)

Load and parse the given .webtest XML file, and store the list of requests found in it.

class webtest.parser.WebtestHandler

Content handler for xml.sax parser.

characters(data)

Called when character data is found inside an element.

endElement(name)

Called when a closing XML tag is found.

setDocumentLocator(locator)

Set the document locator, for tracking line numbers. This is set by the parser, and is used in startElement.

startElement(name, attrs)

Called when an opening XML tag is found. If any badly-formed XML is encountered, a MalformedXML exception is raised.

Previous topic

webtest.correlate