gpt4 book ai didi

python 中的 Selenium 并行浏览器测试

转载 作者:行者123 更新时间:2023-12-03 00:16:15 25 4
gpt4 key购买 nike

我使用 Selenium 工作了一段时间并做了一些测试,效果非常棒。现在我创建了一个测试用例,我想同时在 IE、Firefox 和 Google Chrome 上运行。我已经单独运行它,它们运行得很好,但我想知道是否有办法更改我的脚本并将它们一起运行。

我已经设置了带有集线器和三个 Remote 的网格(Firefox 端口=5556、IE 端口=5557 和 Chrome 端口=5558)。现在,当谈到脚本时,我设置了三个驱动程序:

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

然后我创建了三种不同的方法并使用每个驱动程序运行它们。我还没有测试过,但我想知道:有没有有效的方法来做到这一点?

最佳答案

这段代码可能会对在 pytest 中使用参数化有所帮助。

您可以使用操作系统、浏览器、remoteURL的不同组合制作另一个测试数据,然后根据 params=["chrome", "firefox", "MicrosoftEdge"] 从该文件中读取数据,这可能是一种更优雅的方式使用 py-xdist 和 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)

关于python 中的 Selenium 并行浏览器测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11869431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com