Search code examples
pythonseleniumselenium-grid2parallel-testing

Distributing tests using selenium grid using subprocess approach(plain grid or using jenkins)


I would like to distribute my tests between my local machine and the remote machine. I have 2 tests and would like to run them side by side for faster execution. One on the local machine and the other one is on the remote machine. I have setup my hub and one node on the local and also I have registered the other node on the remote machine..

Here are my three code files saved in the same directory:

TestOnChrome.py

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
import time, unittest


class OnFirefox(unittest.TestCase):

    def setUp(self) :
        self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)


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



    def tearDown(self):
        self.driver.quit()


if __name__ == "__main__":
    unittest.main()

TestOnChromeTwo.py

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
import time, unittest


class OnFirefox(unittest.TestCase):

    def setUp(self) :
        self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)


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



    def tearDown(self):
        self.driver.quit()


if __name__ == "__main__":
    unittest.main()

and here is my runner.py

from subprocess import Popen
import glob
tests = glob.glob('test*.py')
processes = []
for test in tests:
    processes.append(Popen('python %s' % test, shell=True))
for process in processes:
    process.wait()

If I run the runner.py will it automatically distribute the tests? using the nodes I registered? Or I need to do something else?


Solution

  • Any time your code requests a browser from the grid hub, the grid hub will search among it's registered grid nodes for a free browser instance that match with your requested capabilities. You don't need to do anything for this except request the browser as you are doing here self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME).