Search code examples
seleniumunit-testingtestingperformance-testing

Getting the error in python selenium testing for login and logout functionality


I am getting the error as...

AttributeError: 'LoginTestCase' object has no attribute 'driver'

And the code is

from selenium import webdriver;
from selenium.webdriver.common.keys import Keys
import time
import unittest

class LoginTest(unittest.TestCase):
    
    @classmethod
    def setUpclass(cls):
        cls.driver = webdriver.Firefox("D:/Frontend/18-01-2021/selenium-testing")
        cls.driver.implicitly_wait(10)
        cls.driver.maximize_window()

    
     def test_login_valid(self):
        self.driver.get("http://localhost:4200")
        self.driver.find_element_by_name("username").send_keys("admin")
        self.driver.find_element_by_name("password").send_keys("admin@123")
        self.driver.find_element_by_id('log').click()
        self.driver.find_element_by_id('ab').click()
        self.driver.find_element_by_id('usersinfo').click()
        self.driver.find_element_by_id('log_out').click()
        time.sleep(8)

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        print("Test Completed")

I have built the above code for testing login to functionality using selenium with python unit testing . But while executing the python code showing some errors as

AttributeError: 'LoginTestCase' object has no attribute 'driver'

Can anyone help me regarding this


Solution

  • There are some typos in your code:

    • setUpclass should be setUpClass

    • the test method is not indented correctly.

    I think that fixing the setUpClass typo will get you going.