Selenose provides a set of Selenium related plugins for nose developed by ShiningPanda:
The use of these plugins is detailed bellow, but let’s have a look on installation process first.
On most UNIX-like systems, you’ll probably need to run these commands as root or using sudo.
Install selenose using setuptools/distribute:
$ easy_install selenose
Or pip:
$ pip install selenose
It can take time as Selenium server’s jar is downloaded on the fly during installation.
Unfortunately, Python bindings for Selenium (a dependence of selenose) requires RDFLib which is not Python 3 compliant.
However, RDFLib is only used when adding extensions to Firefox. If you do not need this feature, you can first install a fake RDFLib module:
$ pip install https://raw.github.com/shiningpanda/selenose/master/python3wos/rdflib-3.1.0/dist/rdflib-3.1.0.tar.gz
Then install selenose as ususal.
This plugin starts a Selenium Server before running tests, and stops it at the end of the tests.
To enable it, add --with-selenium-server on the nose command line:
$ nose --with-selenium-server
You can also add the with-selenium-server option under the nosetests section in a configuration file (setup.cfg, ~/.noserc or ~/nose.cfg):
[nosetests]
with-selenium-server = true
Options for Selenium Server can be found by downloading its jar and typing:
$ java -jar /path/to/seleniumserver/libs/selenium-server-standalone-X.X.X.jar -h
Most common ones are:
To set server options, add a selenium-server section in a configuration file (setup.cfg, ~/.noserc or ~/nose.cfg). Option names are obtained by removing the initial dash, for instance to run:
$ java -jar selenium-server-standalone-X.X.X.jar -debug -log selenium-server.log
Add following options in configuration:
[selenium-server]
debug = true
log = selenium-server.log
In your test, just create a new Remote WebDriver calling the server and that’s it:
import nose
import unittest
from selenium import webdriver
class TestCase(unittest.TestCase):
def test(self):
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)
try:
driver.get('http://www.google.com')
# Your test here...
finally:
driver.quit()
if __name__ == '__main__':
nose.main()
This plugin provides a Selenium Web Driver to Selenium tests.
This plugin only provides Web Drivers to Selenium test. To declare a Selenium test:
class TestCase(unittest.TestCase):
enable_selenium_driver = True
To enable this plugin, add --with-selenium-driver on the nose command line:
$ nose --with-selenium-driver
You can also add the with-selenium-driver option under the nosetests section in a configuration file (setup.cfg, ~/.noserc or ~/nose.cfg):
[nosetests]
with-selenium-driver = true
But enabling it is not enought, a Web Driver environment is also required.
An environment declares all the necessary parameters to create a new Web Driver.
To create a new environment sample, add a selenium-driver:sample section in a configuration file (setup.cfg, ~/.noserc or ~/nose.cfg) with at least a webdriver option:
[selenium-driver:sample]
webdriver = firefox
This webdriver option defines the Web Driver to use. Here are the available values:
To enable an environment, add --selenium-driver on the nose command line:
$ nose --with-selenium-driver --selenium-driver=sample
You can also add the selenium-driver option under the nosetests section in a configuration file (setup.cfg, ~/.noserc or ~/nose.cfg):
[nosetests]
with-selenium-driver = true
selenium-driver = sample
[selenium-driver:sample]
webdriver = firefox
Selenose also provides a set a predefined but overidable environments:
[selenium-driver:chrome]
webdriver = chrome
[selenium-driver:ie]
webdriver = ie
[selenium-driver:firefox]
webdriver = firefox
[selenium-driver:remote-htmlunit]
webdriver = remote
desired_capabilities = htmlunit
[selenium-driver:remote-htmlunitwithjs]
webdriver = remote
desired_capabilities = htmlunitwithjs
[selenium-driver:remote-opera]
webdriver = remote
desired_capabilities = opera
[selenium-driver:remote-...]
webdriver = remote
desired_capabilities = ...
The Web Driver is directly available with self.driver and there is no need to cleanup after use, selenose will do it for you:
import nose
from selenose.cases import SeleniumTestCase
class TestCase(SeleniumTestCase):
def test(self):
self.driver.get('http://www.google.com')
# Your test here...
if __name__ == '__main__':
nose.main()
To combine Selenium Server and Driver plugins, just enable them both: the command_executor option for a remote Web Driver will directly have the correct value to reach the started Selenium Server.
When writing tests, it’s convenient to start a Selenium Server manually to reduce setup time when running tests. To do so, execute:
$ selenium-server
Starting... done!
Quit the server with CONTROL-C.
Then type CONTROL-C or CTRL-BREAK to stop the server.