[Python] Getting "AttributeError: 'LoginTests' object has no attribute '_testMethodName'"...

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am new, really new to the programming field (I just went through a QA Automation course with Python) and I am trying to work on a Unittest suite, but when I try to run an individual test (Ex: test_SuccessfulLogin) I get the following error:

    "in hash return hash((type(self), self._testMethodName)) E AttributeError: 'LoginTests' object has no attribute '_testMethodName'"

    Could you be so kind to direct me in the right way in order to understand what I'm doing wrong (I read some old threads about this error and it might have smth to do with inheritance of the classes, but I don't know exactly).

    Thanks in advance!


    1. class Browser: log = cl.customLogger()

      def init(self): self.driver = None

      def get_driver(self, browserName): if self.driver is None: if browserName == 'chrome': self.driver = webdriver.Chrome() self.log.info("Chrome Driver is initializing") elif browserName == 'firefox': self.driver = webdriver.Firefox() self.log.info("FireFox Driver is initializing")

      self.driver.maximize_window()
      self.driver.implicitly_wait(5)
      return self.driver


      def close_driver(self): if self.driver: self.driver.quit() self.driver = None

    2.class BasePage(Browser): log = cl.customLogger()

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

    def open_homepage(self, typeBrowser, url):
    try:
    wd = Browser()
    self.driver = wd.get_driver(typeBrowser)
    self.driver.get(url)
    self.log.info("Web Page Launched with URL : " + url)
    except:
    self.log.info("Web Page not Launched with URL : " + url)
    assert False

    def checkPageTitle(self, title):
    try:
    assert title in self.driver.title
    self.log.info("Web Page has the title : " + title)
    except:
    self.log.error("Web Page with the title : " + title + " has not been found")
    assert False

    def find_element(self, locator):
    web_element = None
    try:
    web_element = self.driver.find_element(*locator)
    self.log.info("WebElement with " + str(locator) + " found")
    except:
    self.log.error("WebElement with " + str(locator) + " not found")
    assert False
    return web_element

    def find_elements(self, locator):
    list_web_elements = None
    try:
    list_web_elements = self.driver.find_element(*locator)
    self.log.info("WebElements with " + str(locator) + " found")
    except:
    self.log.error("WebElements with " + str(locator) + " not found")
    assert False
    return list_web_elements

    def send_text(self, locator, text):
    try:
    self.find_element(locator).send_keys(text)
    self.log.info("Sent the text: " + text + " to the locator: " + str(locator))
    except:
    self.log.error("Unable to send the text: " + text + " to the locator: " + str(locator))
    assert False

    def click_element(self, locator):
    try:
    webElement = self.waitElement(locator)
    webElement.click()
    self.log.info("Clicked on the element with the locator: " + str(locator))
    except:
    self.log.error("Unable to click on the element with the locator: " + str(locator))
    assert False

    def waitElement(self, locator):
    webElement = None
    try:
    wait = WebDriverWait(self.driver, 25, poll_frequency=1,
    ignored_exceptions=[ElementNotVisibleException, NoSuchElementException])
    webElement = wait.until(ec.presence_of_element_located(locator))
    self.log.info("WebElement found with locator: " + str(locator))
    except:
    self.log.error("WebElement not found with locator: ", str(locator))
    assert False
    return webElement

    def verify_current_url(self, expected_url):
    return self.driver.current_url == expected_url

    def verify_if_element_is_displayed(self, locator):
    elementDisplayed = None
    try:
    webElement = self.waitElement(locator)
    elementDisplayed = webElement.is_displayed()
    self.log.info("WebElement with locator: " + str(locator) + " is displayed")
    except:
    self.log.error("WebElement with locator: " + str(locator) + " is not displayed")
    assert False
    return elementDisplayed

    def get_text(self, locator):
    elementText = None
    try:
    webElement = self.waitElement(locator)
    elementText = webElement.text
    self.log.info("Got the text: " + elementText + " from the WebElement with the locator: " + str(locator))
    except:
    self.log.error("Unable to get text from locator: " + str(locator))
    assert False
    return elementText


    3.class LoginPage(BasePage):

    LOGIN_EMAIL_FIELD = (By.ID, "user-name")
    LOGIN_PASSWORD_FIELD = (By.ID, "password")
    LOGIN_ACCOUNT_BUTTON = (By.ID, "login-button")
    SHOPPING_CART_BUTTON = (By.CLASS_NAME, 'shopping_cart_link')
    ERROR_LOGIN_MESSAGE = (By.CLASS_NAME, "h3[data-test='error']")

    def open_check_homepage(self, browser, url, title):
    self.open_homepage(browser, url)
    self.checkPageTitle(title)

    def fill_login(self, username, password):
    self.send_text(self.LOGIN_EMAIL_FIELD, username)
    self.send_text(self.LOGIN_PASSWORD_FIELD, password)
    self.click_element(self.LOGIN_ACCOUNT_BUTTON)

    def successful_login(self, url, title):
    assert self.verify_current_url(url)
    self.checkPageTitle(title)

    def loginErrorMessage(self, errorText):
    error_message = self.get_text(self.ERROR_LOGIN_MESSAGE)
    self.verify_if_element_is_displayed(error_message)
    assert error_message == errorText


    1. class LoginTests(LoginPage, unittest.TestCase):

      def init(self, webdriver): super().init(webdriver) self.driver = webdriver

      def setUpClass(self) -> None: self.open_check_homepage('chrome', 'https://www.saucedemo.com/', 'Swag Labs')

      def tearDown(self) -> None: self.close_driver()

      def test_SuccessfulLogin(self): self.fill_login('standard_user', 'secret_sauce') self.successful_login('https://www.saucedemo.com/inventory.html', 'Swag Labs')

      def test_BadCredentialsLogin(self): self.fill_login('standard_use', 'secret_sauc') self.loginErrorMessage('Epic sadface: Username and password do not match any user in this service')

    Continue reading...

Compartilhe esta Página