简体   繁体   English

Pytest Webdriver固定装置无法自检某些选项

[英]Pytest webdriver fixture can't introspect some options

My framework currently only has one conftest.py file in it, which contains a fixture called browser which returns the webdriver object to the test. 我的框架中目前只有一个conftest.py文件,其中包含一个称为浏览器的固定装置,该固定装置将webdriver对象返回到测试。 We've recently tried to implement metafunc.parametrize in _generate_configuration_ in order to test cross browser and cross platform on browserstack, and have encountered a problem. 我们最近试图执行,以测试对browserstack跨浏览器和跨平台_generate_configuration_ metafunc.parametrize,也碰到了问题。 When the browser fixture is called at the beginning of a ui test, it doesn't know what current browser/os/version the test is running in based on the metafunc.parametrize method. 在ui测试开始时调用浏览器固定装置时,它不知道基于metafunc.parametrize方法该测试在哪个当前浏览器/操作系统/版本中运行。

A nice example can be found here, but I like the idea of handling the webdriver object with a fixture because of how easy it is to handle the webdriver when it goes out of scope with the yield statement. 可以在此处找到一个很好的示例,但是我喜欢用固定装置处理webdriver对象的想法,因为当使用yield语句超出范围时,处理webdriver很容易。 It also DRYs up the code: https://qxf2.com/blog/selenium-cross-browser-cross-platform-pytest/ 它还会烘干代码: https ://qxf2.com/blog/selenium-cross-browser-cross-platform-pytest/

Webdrivers as fixtures are a great approach that I highly recommend, particularly because you get free teardown and driver.quit() once your tests have completed. 我强烈建议将Webdrivers作为固定装置,这是一种极好的方法,特别是因为一旦测试完成,您将获得免费的拆解和driver.quit()。 If your driver is going to be dealing with different versions/browsers, I highly recommend you do the following: 如果您的驱动程序要使用不同的版本/浏览器,我强烈建议您执行以下操作:

  1. Create a dictionary of run configurations that contain DesiredCapabilities for different browsers/versions 创建包含不同浏览器/版本的DesiredCapabilities的运行配置字典
  2. If you want to just test everything against all configurations every time, configure your parameterized test generation to iterate through all of the run configurations 如果您想每次都对所有配置进行所有测试,请配置参数化的测试生成以迭代所有运行配置
  3. If you want to be able to target specific ones, add a command line argument in your conftest.py for --run-configuration that maps to the desired DesiredCapabilities in your config. 如果希望能够针对特定目标,请在conftest.py中为--run-configuration添加一个命令行参数,该参数映射到配置中所需的DesiredCapabilities。
  4. Write a fixture called desired_capabilities (scope=session) that reads config.getoption('--run-configuration') and passes the associated DesiredCapabilities to the webdriver call in the driver fixture. 编写一个名为desired_capabilities(作用域=会话)的夹具,该夹具读取config.getoption('-run-configuration')并将关联的DesiredCapabilities传递到驱动程序夹具中的webdriver调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM