如何编写多个Web驱动程序执行的通用硒测试
我是Python和Selenium测试的新手,我遇到了一个可能具有标准解决方案的问题,但我找不到它。
假设我正在从事的项目正在支持Google Chrome和Firefox。为每个受支持的浏览器编写单独的测试似乎是多余的,因此我一直在尝试编写通用测试方法,该方法将使用不同的Web Drivers单独执行。
我当前的方法如下
from selenium import webdriver
def construct_chrome_webdriver () :
driver = webdriver.Chrome()
driver.implicitly_wait(10)
return driver
def construct_firefox_webdriver () :
driver = webdriver.Firefox()
driver.implicitly_wait(10)
return driver
def actual_test_method_implementation (construct_webdriver) :
webdriver = construct_webdriver()
webdriver.get('http://localhost:3333')
# do some testing
def test_method_that_is_executed_by_pytest () :
actual_test_method_implementation(construct_chrome_webdriver)
actual_test_method_implementation(construct_firefox_webdriver)
,测试是使用命令pytest fileName.py
执行的,
此方法需要大量的样板 - 每个测试的两种方法。有更好的方法可以实现这一目标吗?
i'm new to both python and selenium testing and i've encountered a problem that probably has a standard solution but i'm unable to find it.
Let's say that the project that i'm working on is supporting Google Chrome and Firefox. Writing separate tests for every supported browser seems redundant, so i've been trying to write generic test methods that will be executed separately using different webdrivers.
My current approach goes as follows
from selenium import webdriver
def construct_chrome_webdriver () :
driver = webdriver.Chrome()
driver.implicitly_wait(10)
return driver
def construct_firefox_webdriver () :
driver = webdriver.Firefox()
driver.implicitly_wait(10)
return driver
def actual_test_method_implementation (construct_webdriver) :
webdriver = construct_webdriver()
webdriver.get('http://localhost:3333')
# do some testing
def test_method_that_is_executed_by_pytest () :
actual_test_method_implementation(construct_chrome_webdriver)
actual_test_method_implementation(construct_firefox_webdriver)
And the tests are executed using command pytest filename.py
This approach requires a lot of boilerplate - two methods for each test. Is there a better way of achieving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了简化您的电话,您可以直接打电话给您的驱动程序:(使用getAttr)
或
等同于
to simplify your calls, you could directly call your driver like this: (using getattr)
or
is equivalent to
好的,我找到了一个更好的解决方案,但是我仍然不确定这是否是最好的选择,所以我不会将答案标记为几天的答案。
conftest.py
test_utils.pys.py
test_file.py.py
test Invocation
说明
conftest.py和fixtures上的文档
tldr;
在
conftest.py
中,我定义了一个称为webdriver
的测试参数,该参数在调用命令行调用测试时设置。称为
webdriver_name
的固定装置将被注入到由Pytest管理的每个测试方法中。struct_webdriver
功能将根据webdriver_name
is的任何值选择WebDriver实现。专业人士
不需要很多样板代码 - 每个测试需要一个方法,一个方法
弊端
仍然不是一个明确的做事方式,我觉得自己正在重新发明轮子。
Allright, i've found a better solution but im still not sure if that's the best available option so i wont mark that answer as an accepted answer for a few days.
conftest.py
test_utils.py
test_file.py
Test invocation
Explanation
Documentation on conftest.py and fixtures
TLDR;
In
conftest.py
i've defined a test parameter calledwebdriver
which is set while invoking the tests from command line.Fixture called
webdriver_name
will be injected to every test method that is managed by pytest.construct_webdriver
function will choose a webdriver implementation based on whatever the value ofwebdriver_name
is.PROS
Doesn't require a lot of boilerplate code - each test needs exactly one method
CONS
This still isn't a standarized way of doing things and i feel like i'm reinventing the wheel.