'Selenium Parallel Browser testing in python
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?
Solution 1:[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)
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chan Austin |
| Solution 2 | cigien |
