0

I have 3 tests as shown in the code below to run in parallel using pytest-xdist.

from time import sleep

from selenium import webdriver
from selenium.webdriver import ChromeOptions

port = 4444
command_executor = 'http://127.0.0.1:%s/wd/hub' % port
options = ChromeOptions()
options.add_argument('--start-maximized')
options.add_argument('--no-sandbox')
options.add_argument('--autoplay-policy=no-user-gesture-required')
options.add_argument('--remote-debugging-port=9222')


def test_a():
    driver = webdriver.Remote(
        command_executor=command_executor,
        options=options
    )
    driver.get('https://www.amazon.com/')
    sleep(10)


def test_b():
    driver = webdriver.Remote(
        command_executor=command_executor,
        options=options
    )
    driver.get('https://google.com')
    sleep(10)


def test_c():
    driver = webdriver.Remote(
        command_executor=command_executor,
        options=options
    )
    driver.get('https://facebook.com/')
    sleep(10)

Ran the following command from command prompt.

 py.test -n 3 tests/tests

I expected 3 chrome instances to be launched one for each test. But, a chrome instance is launched with 3 tabs. Only the 1st tab seems to be doing the navigation and 2nd and 3rd tabs are just empty tabs. Please see screenshot below. How do I get it to launch 3 chrome instances?

enter image description here

Python version: Python 3.11.4 (latest stable version) I am on the latest xdist version: pytest-xdist 3.3.1 I am running this on local selenium server launched using:

java -jar selenium-server-4.10.0.jar standalone

selenium server is also the latest version.

sridhar249
  • 4,471
  • 6
  • 26
  • 27

1 Answers1

0

You can use SeleniumBase to simplify running tests on a grid. After pip install -U seleniumbase and then starting up a grid, you can run pytest -n 3 --server=127.0.0.1 on the following script. There's a special part added at the top to automatically call pytest with those args if calling python directly:

from seleniumbase import BaseCase

if __name__ == "__main__":
    from pytest import main
    main([__file__, "-n 3", "--server=127.0.0.1"])


class MyTestClass(BaseCase):
    def test_1(self):
        self.driver.get("https://google.com")
        self.sleep(2)

    def test_2(self):
        self.driver.get("https://facebook.com")
        self.sleep(2)

    def test_3(self):
        self.driver.get("https://seleniumbase.io")
        self.sleep(2)

If you don't want to use the grid, remove the --server=127.0.0.1 part.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48