如何编写多个Web驱动程序执行的通用硒测试

发布于 2025-02-08 10:49:55 字数 941 浏览 3 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2025-02-15 10:49:55

为了简化您的电话,您可以直接打电话给您的驱动程序:(使用getAttr)

from selenium import webdriver

driver = getattr(webdriver, "Firefox")(options=youroptions, executable_path="path\\geckodriver.exe" )

driver = getattr(webdriver, "Chrome")(options=options, executable_path="path\\chromedriver.exe" )

getattr(webdriver, "Chrome")(options=options, executable_path="path\\chromedriver.exe" ) 

等同于

webdriver.Chrome(options=options, executable_path="path\\chromedriver.exe" )

for nav in ["Chrome", "Firefox"]:
   driver = getattr(webdriver, nav)
   driver.implicitly_wait(10)
   driver.get('http://localhost:3333')

to simplify your calls, you could directly call your driver like this: (using getattr)

from selenium import webdriver

driver = getattr(webdriver, "Firefox")(options=youroptions, executable_path="path\\geckodriver.exe" )

or

driver = getattr(webdriver, "Chrome")(options=options, executable_path="path\\chromedriver.exe" )

getattr(webdriver, "Chrome")(options=options, executable_path="path\\chromedriver.exe" ) 

is equivalent to

webdriver.Chrome(options=options, executable_path="path\\chromedriver.exe" )

for nav in ["Chrome", "Firefox"]:
   driver = getattr(webdriver, nav)
   driver.implicitly_wait(10)
   driver.get('http://localhost:3333')
皇甫轩 2025-02-15 10:49:55

好的,我找到了一个更好的解决方案,但是我仍然不确定这是否是最好的选择,所以我不会将答案标记为几天的答案。

conftest.py

import pytest

def pytest_addoption (parser):
  parser.addoption("--webdriver", action="store", default="chrome", help="chrome | firefox")

@pytest.fixture
def webdriver_name (request) :
    return request.config.getoption('--webdriver')

test_utils.pys.py

from selenium import webdriver

def construct_webdriver (webdriver_name) :

  if webdriver_name == 'firefox' :
      driver = webdriver.Firefox()
  else :
      driver = webdriver.Chrome() # chrome is also the default choice
  
  # further webdriver initialization
  driver.implicitly_wait(10)
  driver.get('http://localhost:3333')

  return driver

test_file.py.py

from test_utils import construct_webdriver

def test_method_that_is_executed_by_pytest (webdriver_name) :
  print('webdriver_name = ' + webdriver_name)
  webdriver = construct_webdriver(webdriver_name)
  # do some testing

test Invocation

pytest filename.py --webdriver=chrome
pytest filename.py --webdriver=firefox

说明

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

import pytest

def pytest_addoption (parser):
  parser.addoption("--webdriver", action="store", default="chrome", help="chrome | firefox")

@pytest.fixture
def webdriver_name (request) :
    return request.config.getoption('--webdriver')

test_utils.py

from selenium import webdriver

def construct_webdriver (webdriver_name) :

  if webdriver_name == 'firefox' :
      driver = webdriver.Firefox()
  else :
      driver = webdriver.Chrome() # chrome is also the default choice
  
  # further webdriver initialization
  driver.implicitly_wait(10)
  driver.get('http://localhost:3333')

  return driver

test_file.py

from test_utils import construct_webdriver

def test_method_that_is_executed_by_pytest (webdriver_name) :
  print('webdriver_name = ' + webdriver_name)
  webdriver = construct_webdriver(webdriver_name)
  # do some testing

Test invocation

pytest filename.py --webdriver=chrome
pytest filename.py --webdriver=firefox

Explanation

Documentation on conftest.py and fixtures

TLDR;

In conftest.py i've defined a test parameter called webdriver 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 of webdriver_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文