To run the current tests, use:
./run_tests [<test>]
We've found it useful to be able to test different servers with the same test suite. To this end, we've got a simple configuration file setup. See the doc string in webunit.config
If a request fails with an incorrect response code (the default valid response codes are 200, 301 and 302) then the result body fetched from the server will be appended to the logfile for that server.
The WebTestCase is best thought of as a web browser with some added features. To truly emulate a web browser (single-threaded at present) use the "page" methods below. For more fine-grained control and testing, use the other methods.
The WebTestCase objects have a number of attributes (see Setting up fetch defaults):
protocol, server, port -- these default to http://localhost:80 and are used when a relative fetch is performed. Most tests set these vars up with setServer in the setUp method and then use relative URLs.
authinfo -- basic authentication information. Use setBasicAuth to set and clearBasicAuth to clear.
cookies -- the test's store of cookies
images -- cache of images fetched
There are two modes of retrieval using two HTTP methods:
Now the actual calls that are possible:
All of these methods eventually call fetch() - the additional **kw are passed directly to the fetch method:
The HTTPResponse objects hold all the infomation about the server's response to the request. This information includes:
protocol, server, port, url - the request server and URL
code, message, headers - the information returned by httplib.HTTP.getreply()
body - the response body returned by httplib.HTTP.getfile()
Additionally, the object has several methods:
The "path" is a list of 2-tuples ('element name', index) to follow to find the form. So:
<html><head>..</head><body> <p><form>...</form></p> <p><form>...</form></p> </body></html>
To extract the second form, any of these could be used:
[('html',0), ('body',0), ('p',1), ('form',0)] [('form',1)] [('p',1)]
HTTPResponse objects are also able to fetch using WebTestCase methods. They define additional methods:
NOTE: the form submission will include any "default" values from the form extracted from this page. To "remove" a value from the form, just pass a value None for the elementn and it will be removed from the form submission.
example WebTestCase:
page = self.get('/foo') page.postForm(0, self.post, {'name': 'blahblah', 'password': 'foo'})
or the slightly more complex:
page = self.get('/foo') page.postForm(0, self.postAssertContent, {'name': 'blahblah', 'password': None}, 'password incorrect')
You may have the fetcher automatically fail when receiving certain content using:
To test for cookies being sent _to_ a server, use:
To test for cookies sent _from_ the server, access the cookies attribute of the test harness. It is a dict of:
cookies[host name][path][cookie name] = string
Simple usage:
>>> import SimpleDOM >>> parser = SimpleDOM.SimpleDOMParser() >>> parser.parseString("""<html><head><title>My Document</title></head> ... <body> ... <p>This is a paragraph!!!</p> ... <p>This is another para!!</p> ... </body> ... </html>""") >>> dom = parser.getDOM() >>> dom.getByName('p') [<SimpleDOMNode "p" {} (1 elements)>, <SimpleDOMNode "p" {} (1 elements)>] >>> dom.getByName('p')[0][0] 'This is a paragraph!!!' >>> dom.getByName('title')[0][0] 'My Document'
Form extraction example (see also the tests in the test/ directory of the source):
# fetch the start page page = self.get('/ekit/home') page = page.postForm(1, self.postAssertCode, {'__ac_name': 'joebloggs', '__ac_password': 'foo'}, [302]) # same as last fetch, but automatically follow the redirect page = page.postForm(1, self.page, {'__ac_name': 'joebloggs', '__ac_password': 'foo'})
Copyright (c) 2002 ekit.com Inc (http://www.ekit-inc.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This product includes software developed by Bizar Software.
Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
This product includes software developed by Digital Creations for use in the Z Object Publishing Environment (http://www.zope.org/).