python 3表现webdriver - OOP传递驱动程序

时间:2018-05-25 19:04:57

标签: python selenium selenium-webdriver python-behave

所以我一直在尝试让我的项目更多OOP,但是让驱动程序在python文件之间传递很困难。

TestAutomation/
    Features/
        somefeature.feature
    Objects/
        main_misc.py
    Steps/
        actionwords.py
        steps.py
    Tools/
        AutoTools.py
    environment.py

environment.py

class DriverSetup:
    def get_driver():
        ...
        return webdriver.Firefox(executable_path=path)

def before_all(context):
        driver = DriverSetup.get_driver()
        context.actionwords = Actionwords()
        base_url = ''
        driver.get(base_url)
        AutoTools.Navigation.wait_for_page_to_load(driver)

AutoTools.py

class Navigation:
    def __init__(self,driver):
        self.driver = driver

    @staticmethod
    def wait_for_page_to_load(self):
        old_page = self.driver.find_element_by_tag_name('html')

我只给了足够的错误,它会触及这一步。在Debug Self中有curent_url,并且有适当的url,甚至有一个窗口句柄 当我介入时,它会回到environment.py来运行钩子异常 - ' WebDriver'对象没有属性' driver'

1 个答案:

答案 0 :(得分:0)

您收到该错误的原因是,当您调用driver方法时,您正在访问self wait_for_page_to_load(self)属性。也就是说,当你将Firefox webdriver传递给方法时,self指的是Firefox webdriver,因此它试图访问Firefox webdriver的driver属性,这是不正确的。

您需要做的是实例化Navigation对象,以便self引用所述对象,self.driver指的是您传入的Firefox驱动程序。从您的代码中,我希望这样的事情:

def before_all(context):
    # These 4 lines are independent of the Navigation class
    driver = DriverSetup.get_driver()
    context.actionwords = Actionwords()
    base_url = ''
    driver.get(base_url)

    # These 2 lines are dependent on the Navigation class
    # On the right, the Navigation object is instantiated
    # On the left, the object is initialized to the navigation_driver variable
    navigation_driver = AutoTools.Navigation(driver)

    # After, you can call your method since the object's self.driver
    # was instantiated in the previous line and properly refers to the Firefox webdriver
    navigation_driver.wait_for_page_to_load()