CI | python selenium tutorial Fri 30 December 2011 Olivier Mansion

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

1 $ easy_install selenose

Or pip:

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

1 $ nose --with-selenium-server

You can also add the with-selenium-server option under the nosetests section of the ~/nose.cfg:

1 [nosetests]
2 with-selenium-server = true

Options for Selenium Server can be found by downloading its jar and typing:

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

1 $ java -jar selenium-server-standalone-X.X.X.jar -debug -log selenium-server.log

Add the following options to the configuration:

1 [selenium-server]
2 debug = true
3 log = selenium-server.log

In your test, just create a new Remote Web Driver calling the server and that's it:

 1 import nose
 2 import unittest
 3 
 4 from selenium import webdriver
 5 
 6 class TestCase(unittest.TestCase):
 7 
 8     def test(self):
 9         driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)
10         try:
11             driver.get('http://www.google.com')
12             # Your test here...
13         finally:
14             driver.quit()
15 
16 if __name__ == '__main__':
17     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:
1 class TestCase(unittest.TestCase):
2     enable_selenium_driver = True

Enable the plugin

To enable this plugin, add --with-selenium-driver on the nose command line:

1 $ 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):

1 [nosetests]
2 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:

1 [selenium-driver:sample]
2 webdriver = firefox

This webdriver option defines the Web Driver to use. Here are the available values:

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

1 $ 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):

1 [nosetests]
2 with-selenium-driver = true
3 selenium-driver = sample
4 
5 [selenium-driver:sample]
6 webdriver = firefox

Selenose also provides a set of predefined but overridable environments:

 1 [selenium-driver:chrome]
 2 webdriver = chrome
 3 
 4 [selenium-driver:ie]
 5 webdriver = ie
 6 
 7 [selenium-driver:firefox]
 8 webdriver = firefox
 9 
10 [selenium-driver:remote-htmlunit]
11 webdriver = remote
12 desired_capabilities = htmlunit
13 
14 [selenium-driver:remote-htmlunitwithjs]
15 webdriver = remote
16 desired_capabilities = htmlunitwithjs
17 
18 [selenium-driver:remote-opera]
19 webdriver = remote
20 desired_capabilities = opera
21 
22 [selenium-driver:remote-...]
23 webdriver = remote
24 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:

 1 import nose
 2 
 3 from selenose.cases import SeleniumTestCase
 4 
 5 class TestCase(SeleniumTestCase):
 6 
 7     def test(self):
 8         self.driver.get('http://www.google.com')
 9         # Your test here...
10 
11 if __name__ == '__main__':
12     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:

1 $ selenium-server
2 Starting... done!
3 
4 Quit the server with CONTROL-C.

Then type CONTROL-C or CTRL-BREAK to stop the server.