0

So I have been trying to make my project more OOP but am having great difficulty getting the driver to pass between the python files.

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')

I only gave enough where it gets the error, it will hit the step. In Debug Self has with curent_url with the proper url in place even has a window handle When I step into, it goes back to environment.py to run hook exception - 'WebDriver' object has no attribute 'driver'

natn2323
  • 1,983
  • 1
  • 13
  • 30
l3l00
  • 55
  • 1
  • 7
  • What is the `TestAutomation` line? Is it meant to be a command? If so, why are there single, double and triple dashes? – l0b0 May 25 '18 at 22:03

1 Answers1

0

The reason you're getting that error is because you're accessing the driver attribute of self when you call the wait_for_page_to_load(self) method. That is, when you're passing in the Firefox webdriver to the method, self is referring to the Firefox webdriver, so it's trying to access the Firefox webdriver's driver attribute, which is incorrect.

What you need to do is instantiate a Navigation object so that self is referring to said object and self.driver is referring to the Firefox driver you're passing in. From your code, I'd expect something like this:

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()
natn2323
  • 1,983
  • 1
  • 13
  • 30