In this recipe, we will create and execute tests in parallel with Python bindings using the subprocess
module. We will use the Hub and nodes configured in earlier recipes to run these tests.
We will create two test scripts to test the application with Firefox and Internet Explorer using the following steps. You can also create a single test and parameterize it similar to what we did for TestNG:
test_on_firefox.py
and copy the following code:import unittest from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait class OnFirefox(unittest.TestCase): def setUp(self): self.driver = webdriver.Remote( command_executor='http://localhost:4444/wd/hub',desired_capabilities=DesiredCapabilities.FIREFOX) def test_gogle_search_ie(self): driver = self.driver driver.get("http://www.google.com") inputElement = driver.find_element_by_name("q") inputElement.send_keys("Cheese!") inputElement.submit() WebDriverWait(driver, 20).until(lambda driver: driver.title.lower().startswith("cheese!")) self.assertEqual("cheese! - Google Search", driver.title) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main(verbosity=2)
test_on_ie.py
and copy the following code:import unittest from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait class OnFirefox(unittest.TestCase): def setUp(self): self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',desired_capabilities=DesiredCapabilities.INTERNETEXPLORER) def test_google_search_ff(self): driver = self.driver driver.get("http://www.google.com") inputElement = driver.find_element_by_name("q") inputElement.send_keys("Cheese!") inputElement.submit() WebDriverWait(driver, 20).until(lambda driver: driver.title.lower().startswith("cheese!")) self.assertEqual("cheese! - Google Search", driver.title) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main(verbosity=2)
subprocess
module to run these tests concurrently on different nodes. Name this script as runner.py
and copy the following code:import glob from subprocess import Popen tests = glob.glob('test*.py') processes = [Popen('python %s' % test, shell=True) for test in tests] for process in processes: process.wait()
We need to place all three scripts in the same directory. When we execute runner.py
, it collects all the tests with names starting with test
using the glob
function. Then we append each test using the Popen
function from the subprocess
module. The Popen()
function calls each test using Python as a subprocess
of the main process and waits for the script to complete.
We can also use the nose
module for Python to run the tests in parallel. First, we need to install the nose
unit testing framework using the following command:
pip nose
After the nose
module is installed, you need to open the folder where all the tests are stored and use the following command:
nosetests --processes=2
This will call the nosetests
scripts, which will locate all the files with a test prefix in the current directory and start running tests concurrently. In this example we are running two tests, so we need to specify the value for processes argumented as 2. The nose
module internally uses a multiprocessing module for concurrent execution.
3.138.37.151