程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 Pytest 在 selenium Python 中创建 webdriver 实例的最佳方法是什么大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 Pytest 在 SELEnium Python 中创建 webdriver 实例的最佳方法是什么?

开发过程中遇到使用 Pytest 在 SELEnium Python 中创建 webdriver 实例的最佳方法是什么的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 Pytest 在 SELEnium Python 中创建 webdriver 实例的最佳方法是什么的解决方法建议,希望对你解决使用 Pytest 在 SELEnium Python 中创建 webdriver 实例的最佳方法是什么有所启发或帮助;

我是 Python 新手,我想创建 Web 驱动程序实例 我找到了两种方法

首先:创建Fixture并在基类中使用Fixture,并在需要时继承该基类。但这是行不通的。这里的Setup fixture 在conftest.py 文件中是正确的

基类:

@pytest.mark.usefixtures("setup")
class BaseTest:

    @staticmethod
    def go_to_url(url):
        print(".............Launching Application URL : " + url)
        # Driver.Instance.get(url)
        Driver.Instance.get(url)

    @staticmethod
    def maximize_window_size():
        print("..............Maximizing browser window..............")
        Driver.Instance.maximize_window()

页面对象:

Class page_object_class1:
value_xpath="//div[@ID='excr']"
SELEniumUIAction.Isdisplayed(self.value_xpath)

Action Class:这会导致错误,例如 SELEniumUIAction 没有“驱动程序”属性。这里我们继承了 BaseTest 类,所以我认为它应该是自动来自 BaseTest 类。我能够启动和最大化浏览器的原因,直到 BaseTest 类我正在获取驱动程序的引用,但是当我在页面对象类中引用该驱动程序时出现错误。任何建议将不胜感激

class SELEniumUIAction(BaseTest):

    def click_element(self,value_xpath):
        self.driver.find_elemenT_By_xpath(value_xpath).click()
        

第二:创建不带类的Driver.py,如下所示,并在需要时使用Driver.Instance 使用它。我没有发现第二种方法有问题,但不知道这是否支持并行执行

from SELEnium import webdriver

Instance = None

def Initialize():
    global Instance
    Instance = webdriver.Chrome("C://hromedriver.exe")
    Instance.implicitly_wait(5)
    return Instance

解决方法

这是我使用的,对我来说效果很好。

@pytest.fixture()
    def test_setup(self):
        #initiaTing chrome driver
        global driver
        chrome_options=Options()
        chrome_options.add_argument('--start-maximized')
        driver = webdriver.Chrome(executable_path=r"{{Your driver path}}",chrome_options=chrome_options)

    def test_01(self,test_setup):
            driver.get(//page_url//)
            actual_title=driver.title

大佬总结

以上是大佬教程为你收集整理的使用 Pytest 在 selenium Python 中创建 webdriver 实例的最佳方法是什么全部内容,希望文章能够帮你解决使用 Pytest 在 selenium Python 中创建 webdriver 实例的最佳方法是什么所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。