gocept.selenium provides integration with several web frameworks. Different frameworks require different dependencies; this is handled via setuptools extras of gocept.selenium (e.g. for Grok integration you need to require gocept.selenium[grok]).
Generally, you need a test layer that handles the setup, and then have your tests inherit from the appropriate TestCase.
No extra requirements (simply gocept.selenium).
This test layer takes a WSGI callable and runs it in a temporary HTTP server:
import gocept.selenium.wsgi
from mypackage import App
test_layer = gocept.selenium.wsgi.Layer(App())
class WSGIExample(gocept.selenium.wsgi.TestCase):
layer = test_layer
def test_something(self):
self.selenium.open('http://%s/foo.html' % self.selenium.server)
self.selenium.assertBodyText('Hello world!')
No extra requirements (simply gocept.selenium).
This test case provides a temporary directory (as self.documentroot) that is served via HTTP where tests can put HTML files to examine:
import gocept.selenium.static
class StaticFilesExample(gocept.selenium.static.TestCase):
def test_something(self):
open(os.path.join(self.documentroot, 'foo.html'), 'w').write(
'Hello World!')
self.selenium.open('http://%s/foo.html' % self.selenium.server)
self.selenium.assertBodyText('Hello world!')
Requires gocept.selenium[ztk].
This test layer wraps your usual ZCMLLayer that is used for typical ZTK functional tests, and provides an HTTP server for testing:
import gocept.selenium.ztk
import zope.app.testing.functional
zcml_layer = zope.app.testing.functional.ZCMLLayer(
'ftesting.zcml', __name__, __name__, allow_teardown=True)
selenium_layer = gocept.selenium.ztk.Layer(zcml_layer)
class ZTKExample(gocept.selenium.ztk.TestCase):
layer = selenium_layer
def test(self):
self.selenium.open('http://%s/foo.html' % self.selenium.server)
self.selenium.assertBodyText('Hello world!')
If your ZTK application uses zope.app.wsgi.testlayer, see Grok for integrating gocept.selenium.
Requires gocept.selenium[grok].
This test layer groks your package and sets everything up so Selenium can access the application. You will probably want to setup your app in your test setup:
import gocept.selenium.grok
import transaction
selenium_layer = gocept.selenium.grok.Layer(my.package)
class GrokExample(gocept.selenium.grok.TestCase):
layer = selenium_layer
def setUp(self):
super(MyTest, self).setUp()
root = self.getRootFolder()
root['app'] = mypackage.App()
transaction.commit()
def test(self):
self.selenium.open('/app')
self.selenium.assertBodyText('Hello world!')
Requires gocept.selenium[zope2]
This test layer requires Testing.ZopeTestCase.layer.ZopeLiteLayer and provides an HTTP server for the tests. See gocept.selenium.zope2.tests.test_zope212 for details how to set this up.
If your Zope 2 setup supports it, you can use the WSGI integration instead of a specialised Zope 2 integration to run your tests.
You might see the following exception when running tests:
File ".../repoze.retry-1.0-py2.7.egg/repoze/retry/__init__.py", line 55, in __call__
cl = int(cl)
ValueError: invalid literal for int() with base 10: ''
To fix it you can use an additional middleware around your WSGI application: gocept.selenium.wsgi.CleanerMiddleware. It also fixes an issue with wsgiref. See comments in the code for more information.
Requires gocept.selenium[plone].
This test layer requires Products.PloneTestCase.laye.PloneSiteLayer and provides an HTTP server for the tests. See gocept.selenium.plone.tests.test_plone{3,4} for details how to set this up.
Requires gocept.selenium[plonetesting].
gocept.selenium provides a plone.testing.Layer at gocept.selenium.plonetesting.SELENIUM that you can mix and match with your other layers, see gocept.selenium.plonetesting.testing with gocept.selenium.plonetesting.tests.zope2, and gocept.selenium.plonetesting.testing_plone with gocept.selenium.plonetesting.tests.plone{3,4} for details how to set this up.
Selenium tests can be written in HTML tables.
Their syntax is a bit clunky. But their development and debugging is eased a lot by using Selenium IDE Firefox extension. Selenium IDE provides both initial recording of tests and stepping through those tests. However, HTML tests have a main drawback: they are hard to include in a continuous integration system.
gocept.selenium provides a script that converts a set of Selenium HTML tests into a Python module with a TestCase (based on gocept.selenium and plone.testing).
Using the converthtmltests script, the developer can use HTML tests – written, debugged and maintained with the Selenium tools – while being able to easily include those Selenium tests in a continuous integration system.
converthtmltests -l LAYER [options] directory
options:
-f FILE, --file=FILE write tests to FILE
-l LAYER, --layer=LAYER
full python import path to layer instance
The script gathers and converts all Selenium HTML tests found in the mentioned directory.
The user must refer to a plone.testing layer by specifying its Python import path. That layer is set on the test case generated in the Python module.
An output file can be specified. In case no output file name is specified, the module produced is named tests_all_selenium.py.
On Python-2.4, converthtmltests requires gocept.selenium[script].