Search code examples
pythongoogle-chromeselenium-webdrivertypeerrorassert

AssertTrue missing 1 required positional argument


I am trying to assert the presence of an element and I can make it work but now in the way that I would like.

I have a common functions file: -

from selenium.common.exceptions import NoSuchElementException


def is_element_present_common(self, how, what):
    try:
        self.driver.find_element(by=how, value=what)
    except NoSuchElementException as e:
        return False
    return True

...and my main file: -

import unittest
from Common import common_functions, initialisation, login
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import NoSuchElementException


class QuickTestPlanLogin(unittest.TestCase):
    def setUp(self):
        self.driver = initialisation.start_webdriver()
        self.driver = initialisation.start_sap(self.driver)

    def tearDown(self):
        self.driver.close()

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e:
            return False
        return True

    def test_login(self):
        wait = initialisation.wait_for(self.driver)
        self.driver = login.default_login(self.driver, "username", "password")

        # self.assertTrue(self.is_element_present(By.ID, "my-projects-table_info"))
        # self.assertTrue(common_functions.is_element_present_common(By.ID, "my-projects-table_info"))

There are two assert statements. If I run the first one it works fine, but it is calling the is_element_present function which I do not want. I would like to call the is_element_present_common function from the common_functions file. Every time I run the second assert statement I get the following error: -

TypeError: is_element_present() missing 1 required positional argument: 'what'

I know I am missing something very simple....


Solution

  • Change the function definition to :

    def is_element_present_common(how, what):
    

    And

    Change the call to is_element_present_common function as :

    self.assertTrue(common_functions.is_element_present_common(By.ID, "my-projects-table_info"))