Skip to content. | Skip to navigation

Sections
Personal tools
What is this?
Hi, my name is Tom Lazar and I'm a Plone and Zope developer based in Berlin, Germany and this is my personal and professional (no big difference, really...) website.
 

Remote control websites with zope.testbrowser

Filed Under:

It's one of those small little things that I didn't realize eventhough (at least in hindsight) it should have been perfectly obvious all along... Here it is... *drumroll*: you can use zope.testbrowser's Browser API also to test 'remote' , i.e. non-Zope sites. Here's an example:
from unittest import TestSuite, main
from Testing.ZopeTestCase import ZopeDocTestSuite

def test_remote_browsing():
"""
By importing directly from zope.testbrowser.browser we can use
the Browser API also for testing remote (non-Zope) sites:

>>> from zope.testbrowser.browser import Browser
>>> browser = Browser()
>>> browser.open('http://en.wikipedia.org/wiki/Monty_Python')
>>> browser.url
'http://en.wikipedia.org/wiki/Monty_Python'
>>> browser.contents
'...Dead Parrot...'
"""

def test_suite():
return ZopeDocTestSuite()

if __name__ == '__main__':
main(defaultTest='test_suite')
Admittedly, I haven't got a specific use case for this at the moment, but the Browser API's many useful convenience methods (such as getControl) should allow for some pretty nifty python-scripted 'remote control'-power over any site... i.e. not to use the API for testing but rather for scripting...

Once again, "tip o' the hat" to witsch – who still needs to get himself a blog, dammit ;-)

Update: Martijn is right: credit, where credit is due: it's actually zope.testbrowser (with mechanize) which provides the relevant functionality and not ZopeTestCase.