Selenose

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.

Installation

Python 2

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.

Python 3

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.

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 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:

  • -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 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()

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 to True:
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 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.

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 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:

  • chrome for Chrome, allowing following options in configuration:
    • executable_path (optional): path to chromedriver executable,
    • port (optional),
    • desired_capabilities (optional),
  • firefox for Firefox, allowing following options in configuration:
    • timeout (optional),
  • ie for Internet Explorer, allowing following options in configuration:
    • port (optional),
    • timeout (optional),
  • remote to delegate to a Selenium Server (started by Selenium Server Plugin?), allowing 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 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 = ...

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 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.

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.

Project Versions

Table Of Contents

This Page