Home > database >  AttributeError: 'TestTomsLogin' object has no attribute 'driver'
AttributeError: 'TestTomsLogin' object has no attribute 'driver'

Time:01-11

conftest.py file

    import pytest
    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from webdriver_manager.chrome import ChromeDriverManager
    
    
    @pytest.fixture(scope="class")
    def setup(request):
        driver = webdriver.Chrome(ChromeDriverManager().install())
        driver.get("https://itera-qa.azurewebsites.net/Login")
        driver.maximize_window()
        wait = WebDriverWait(driver, 10)
        request.cls.driver = driver
        request.cls.driver = wait
        yield
        driver.close()

loginpage.py file

    from selenium.webdriver.common.by import By
    
    
    class TomsLogin:
        def __init__(self, driver, wait):
            self.driver = driver
            self.wait = wait
    
        def toms_login(self, usern, pwd):
            self.driver.find_element(By.ID, "Username").send_keys(usern)
            self.driver.find_element(By.ID, "Password").send_keys(pwd)

test_toms_login.py file -- Getting error while calling this file

    import pytest
    from base.loginpage import TomsLogin
    
    @pytest.mark.usefixtures("setup")
    class TestTomsLogin():
        def test_login(self):
            self.tl = TomsLogin(self.driver, self.wait)
            self.tl.toms_login("User1", "pwd1")
    
    ttl = TestTomsLogin()
    ttl.test_login()

Error Details:

collecting ... 
test_toms_login.py:None (test_toms_login.py)
test_toms_login.py:11: in <module>
    ttl.test_login()
test_toms_login.py:7: in test_login
    self.tl = TomsLogin(self.driver, self.wait)
E   AttributeError: 'TestTomsLogin' object has no attribute 'driver'

CodePudding user response:

I would change the structure of you code to something like this:
And make sure driver is actually installing (you can use hard coded path once to see the problem)

@pytest.fixture(scope="class")
def driver(request):
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get("https://itera-qa.azurewebsites.net/Login")
    driver.maximize_window()
    yield driver
    driver.quit()

@pytest.fixture(scope="class")
def setup(request, driver):
    # put your wait here too
    request.cls.driver = driver

CodePudding user response:

ttl = TestTomsLogin()
    ttl.test_login()

Issue resolved after removed above two line. I should not use this as i am using pytest methods.

  •  Tags:  
  • Related