Introducing Selenose
Selenose provides a set of Selenium related plugins for nose developed by ShiningPanda:
- Selenium Server Plugin starts a Selenium Server before running tests, and stops it at the end of the tests.
- Selenium Driver Plugin provides a Selenium WebDriver to the tests.
The use of these plugins is detailed bellow, but let's have a look on the installation process first.
Installation
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 a while as Selenium server's jar is downloaded on the fly during installation.
Selenium Server Plugin
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
to the nose command line:
$ nose --with-selenium-server
You can also add the with-selenium-server
option under the nosetests
section of the ~/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 options are:
-port <nnnn>
: the port number the Selenium Server should use (default 4444),-log <logFileName>
: writes lots of debug information out to a log file,-debug
: enable debug mode.
To set the server options, add a selenium-server
section to the 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 the following options to the configuration:
[selenium-server]
debug = true
log = selenium-server.log
In your test, just create a new Remote
Web Driver 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()
Selenium Driver Plugin
This plugin provides a Selenium Web Driver to Selenium tests.
Flag Selenium tests
This plugin only provides Web Drivers to Selenium test. To declare a Selenium test:
- Either make your test case inherit from
selenose.cases.SeleniumTestCase
, - Or set a
enable_selenium_driver
flag toTrue
:
class TestCase(unittest.TestCase):
enable_selenium_driver = True
Enable the plugin
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 to the configuration file (setup.cfg
, ~/.noserc
or ~/nose.cfg
):
[nosetests]
with-selenium-driver = true
But enabling it is not enough, a Web Driver environment is also required.
Web Driver environment
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 to the configuration file (setup.cfg
, ~/.noserc
or ~/nose.cfg
) with at least one webdriver
option:
[selenium-driver:sample]
webdriver = firefox
This webdriver
option defines the Web Driver to use. Here are the available values:
`chrome` for [Chrome](https://www.google.com/chrome), allowing the following options in configuration:
: - `executable_path` (optional): path to `chromedriver` executable,
- `port` (optional),
- `desired_capabilities` (optional),
`firefox` for [Firefox](http://www.mozilla.org/firefox/), allowing the following options in configuration:
: - `timeout` (optional),
`ie` for [Internet Explorer](http://windows.microsoft.com/en-US/internet-explorer/products/ie/home), allowing the following options in configuration:
: - `port` (optional),
- `timeout` (optional),
`remote` to delegate to a Selenium Server (started by [Selenium Server plugin](#selenium-server-plugin)?), allowing the following options in configuration:
: - `command_executor` (required): url of the server (`http://127.0.0.1:4444/wd/hub` by default),
- `desired_capabilities` (required): the desired browser, it could be the lower case field name of `selenium.webdriver.DesiredCapabilities` such as `firefox`, `htmlunitwithjs`\... or a comma separated key/value list such as `browserName=firefox,platform=ANY`.
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 to the 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 of predefined but overridable 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 = ...
Writing tests
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()
Combining Server & Driver
To combine a Selenium Server and a Driver plugin, just enable them both: the command_executor
option of the remote
Web Driver will know the correct value to reach the Selenium Server.
Tips
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.