infrae.testbrowser is test browser for WSGI applications sharing the same ideas than zope.testbrowser. It only has lxml and zope.interface as dependency.
A Selenium version of the same browser is available in this package as well. It share the same API than the default one, and requires Selenium 2 to work.
- Test browser. You instantiate a new one by giving your WSGI application to test as arguments to the constructor. The application will be available via localhost.
>>> browser = Browser(MyWSGIApplication)
On the browser you have the following methods:
- open(url, method='GET', query=None, form=None, form_enctype='application/x-www-form-urlencoded', data=None, data_type=None)
- Open the given url, with the given method. If query is provided, it will be encoded in the URL. If form is provided, it will be set as payload depending of form_enctype (application/x-www-form-urlencoded or multipart/form-data). An authentication can be provided in the URL (via user:password@localhost). As the host part doesn't really have any meaning, you can directly specify a path as URL. It return the HTTP status code returned by the application. An alternative to form is the data and data_type parameters. The param data is the pre-encoded body of the request, and data_type is the the content type of the body. These parameters are useful for http PUT.
- Reload the currently open URL (sending back any posting data).
- login(username, password=_marker)
- Set an basic authorization header in the request to authenticate yourself with the given username and password. If password is not provided, username is used as password.
- set_request_header(key, value)
- Add an header called key with the value value used while querying the application. Headers are set for all further queries.
- Get the value of an header used while querying the application. Return None if there is no matching header.
- Remove all sets headers used while querying the application. Authentication one included.
- Return a link selected via content.
- get_form(name=None, id=None)
- Return a form selected via its name or id attribute (at least one of them is required).
The following properties are helpful as well:
- Currently viewed URL, without the hostname part, but with query data and so.
- Currently viewed path. It is recommanded to use this in your test instead of url. In case of Selenium testing, the URL will change depending of your local testing setup, meaning if your Selenium is not on the same computer than your test suite, the URL won't be localhost).
- Last previously viewed URLs.
- Method used to view the current page.
- HTTP status for the currently viewed page.
- HTTP status code as an integer for the currently viewed page.
- Content type of the currently viewed page.
- Dictionary like access to response headers.
- Dictionary like object to access existing cookies.
- Payload of the currently viewed page.
- If the response was an HTML document, this contains an LXML parsed tree of the document.
- If the response was an XML response, this contains an LXML parsed tree of it.
- If the response was a JSON response, this contains the loaded JSON object.
- Access to browser options.
The following options are attributes of the options object, example:
>>> browser.options.handle_errors = False
- Server name to use to query the WSGI application (default to localhost).
- Port number to use to query the WSGI application (default to 80).
- HTTP protocol to use to query the WSGI application (default to HTTP/1.0).
- Boolean indicating if a redirect must be automatically followed. Default to True.
- Boolean indicating if a redirect to a url that doesn't match the current server and port set in options should be automatically followed and handled by the current WSGI application. Activating it, will update the options server and port to the new value defined by the redirect URL. Default to False.
- Set the WSGI flag wsgi.handleErrors in the WSGI environment. Default to True.
- Boolean indicating if we must support cookie. By default to True.
- Dictionnary that can be used to inject variable in the WSGI environment.
The browser as an inspect attribute. You can register an Xpath expression with it, and query them after on HTML pages:
>>> browser.inspect.add('feedback', '//div[@class="feedback"]/span') >>> self.assertEqual(browser.inspect.feedback, ['Everything ok']) >>> browser.inspect.add('feedback', css='div.feedback span') >>> self.assertEqual(browser.inspect.feedback, ['Everything ok'])
- add(name, xpath=None, type='link', css=None, unique=False)
Add an expression called name that can be used to inspect the HTML content of the browser using the xpath expression (or the css one). type can be:
- The result would be a list containing the text of each matched element.
- The result would be a list containing the text where whitespaces have been normalized for each matched element. (not available on Selenium, the text is normalized by default by the browser).
- The result would be a list of links.
- The result would be a list of forms.
- The result would be the actions of a form.
- The result would be the fields of a form.
- Available only on selenium, that is a list of elements, that you can click on it (even if they are not links).
If unique is true, no more than one item matching will be expected. An error will be asserted if there are more items matching, and None will be returned if none matched.
Macros let you add listing of action to do on the browser. An example will speak by itself:
>>> def create_content(browser, identifier, title): ... form = browser.get_form('addform') ... form.get_control('identifier').value = identifier ... form.get_control('title').value = title ... assert form.inspect.actions['save'].click() == 200 >>> browser.macros.add('create', create_content)
Now you can create content with your browser:
>>> browser.macros.create('test', 'Test Content') >>> browser.macros.create('othertest', 'Other Test Content')
Forms have the following methods and attributes:
- Name of the form.
- URL where to form is posted.
- Method to use to post the form.
- Form enctype to use to post the form.
- Charset to which the form data will be encoded before being posted.
- Dictionary containing all the controls of the form.
- Inspect attribute, working like the one of the browser. By default, inspect.actions is registered to return all the submit-like controls of the form.
- Return the given form control by its name.
- submit(name=None, value=None)
- Submit the form, potentially add the control name and the given value to the submission. This return the HTTP status code returned by the application.
Calling str(form) will only return the HTML code of the form.
Forms support all the known HTTP controls.
For consistency, all form controls share the attributes:
- Name of the control.
- Type of control, like value of type attribute for input and tag name in other cases.
- Value stored in the control.
- Boolean indicating if the control store multiple value.
- If the value have to be chosen in a list of possible values, those are the possibilities.
- Boolean indicating if the control can be checked (i.e. is it a checkbox).
- Boolean indicating if the control is checked (and so if the value will be sent if the control is checkable).
In addition action controls (like submit buttons, button), have:
- Submit the form with this action. This return the HTTP status code returned by the application.
- Alias to submit().
For file control, you have to set as value the filename (i.e path to) of the file you want to upload.
Test browser. You instantiate a new one by giving your WSGI application to test as arguments to the constructor.
You have to use the browser as a context manager in order to start and stop the server that Selenium will use to access the application.
The following environement variable are available in order to control the connection to the Selenium server:
- TESTBROWSER_BROWSER (default to firefox)
- TESTBROWSER_SELENIUM_PLATFORM (default to the local one)
- TESTBROWSER_SELENIUM_HOST (default to localhost)
- TESTBROWSER_SELENIUM_PORT (default to 4444)
- TESTBROWSER_SERVER (default to localhost)
- TESTBROWSER_PORT (default to 8000)
If you set your testsuite to connect to a Selenium server that is not on your computer where you run your testsuite, please set the server and port options so that the Selenium knows how to connect to your application (it should be the network name of your computer).
The API is the same than the default browser, except for:
- you can't access HTTP status or headers,
- you can't change hidden fields (you can only do what the user can do).
Cookies do work however.
The following options are attributes of the options object, example:
They are on par with the possible configuration environment variables:
- String used to specify which browser you expect to use, i.e. 'firefox' or 'chrome' for instance.
- Network name of the computer where the Selenium server run.
- Port number where the Selenium server run.
- Operating system where the Selenium should run the wanted browsers (for instance set it to 'win' if you wish Selenium to pick a browser on Windows).
- Network name of the computer where the testsuite will be running. This is the name that Selenium will use to access the tested application.
- Port on which the test application will be bound so Selenium can access it.
|Release version||Release date||Release files|
|infrae.testbrowser 2.0.2||2013 5 23 11:52:54||
|infrae.testbrowser 2.0.1||2012 12 10 16:49:20||
|infrae.testbrowser 2.0||2012 9 24 11:06:58||
|infrae.testbrowser 2.0b1||2011 11 7 16:46:04||
|infrae.testbrowser 1.1||2011 2 7 11:58:27||
|infrae.testbrowser 1.0||2010 10 7 13:39:31||
License: New BSD