4

I am working with Selenium for a while and doing some testing and it's been great. Now I have created a test case which I want to run on IE, Firefox, and Google Chrome at the same time. I have run it separately and they run just fine, but I was wondering if there is a way to change my script and run them all together.

I already set up the grid with a hub and three remote controls (Firefox port=5556, IE port=5557 and Chrome port=5558). Now when it comes to the script I set up the three drivers:

def setUp(self):
    # Setting up the driver for Firefox
    self.driverFF = webdriver.Firefox()
    ...
    
    # Setting up the driver for IE
    self.driverIE = webdriver.Ie()
    ...
    
    # Setting up the driver for IE
    self.driverCh = webdriver.Chrome()
    ...

Then I created three different methods and run them with each driver. I have not tested it yet, but I was wondering: Is there a efficient way to do this?

2

2 Answers 2

1

This piece of code might be helpful using parameterize in pytest.

You can make another test data with different combinations of OS, browser, remoteURL, then read data from that file according to params=["chrome", "firefox", "MicrosoftEdge"], this might be a more elegant way to do parallel test with py-xdist and selenium grid.

import pytest
import pytest_html
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
 
@pytest.fixture(params=["chrome", "firefox", "MicrosoftEdge"],scope="class")
def driver_init(request):
    if request.param == "chrome":
        web_driver = webdriver.Chrome()
    if request.param == "firefox":
        web_driver = webdriver.Firefox()
    if request.param == "MicrosoftEdge":
        web_driver = webdriver.Edge(executable_path=r'C:\EdgeDriver\MicrosoftWebDriver.exe')
    request.cls.driver = web_driver
    yield
    web_driver.close()
 
@pytest.mark.usefixtures("driver_init")
class BasicTest:
    pass
class Test_URL(BasicTest):
        def test_open_url(self):
            self.driver.get("https://www.lambdatest.com/")
            print(self.driver.title)
            sleep(5)
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please the exact command you used to run this script? I created a file test_stackoverflow.py, pasted your code in it and used this command: pytest .\test_stackoverflow.py -vs -n 2 --dist=loadfile -m "not serial". The tests are running one after the other. I am aiming at parallel execution. How to achieve that?
0

In Java ecosystem, they have testNG, which you can specify combinations of OS and browers in a xml file, then testNG can divert that file to setup in Java test script, testNG is a parallel running utility just like pytest-xdist in python ecosystem, testNG can start many threads to send tests to remote nodes according to settings in desired capability.

In python ecosystem, I cannot find that elegant way. The only way I can figure out is to make the desired capability for all that OS, browser, remoteURL in a different config file, then use that file as an argument to run test, so that you have the same python test scripts reading different config file to do test in different remote nodes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.