0

I'm very new to python, trying to create reusable code. when I try to call the class Login and function login_user in test_main.py by passing all the arguments that were used under Login class, I'm getting an error as InvalidArgumentException: Message: invalid argument: 'using' must be a string.

test_main.py file which runs on pytest.

Locators_test is the class of test_Locators.py file where I have all my xpaths

test_main.py

def test_setup():

driver = webdriver.Chrome(executable_path= Locators_test.browser_path)
driver.maximize_window()
driver.delete_all_cookies()
driver.get(homePage)
driver.implicitly_wait(5)
yield
print("test complete")

test_Locators.py

class Locators_test():

loginlink_xpath = "//a[@id='login-link']"
login_email = "xxxxx"
login_password = "xxxxx"
loginemail_id = "dnn_ctr1179_Login_txtEmail"
loginpassword_id = "dnn_ctr1179_Login_txtPassword"
clicklogin_id = "dnn_ctr1179_Login_btnLogin"

test_login.py

from Smoketest.locatorfile.test_Locators import Locators_test

class Login():

def __init__(self,driver):

    self.driver = driver

def login_user(self,driver):
    try:
        loginButton = self.driver.find_element((By.XPATH, Locators_test.loginlink_xpath))
        while loginButton.click() is True:
            break
        time.sleep(3)
        self.driver.execute_script("window.scrollBy(0,300);")

        EmailField = self.driver.find_element((By.ID, Locators_test.loginemail_id))
        EmailField.send_keys(Locators_test.login_email)

        PasswordField = self.driver.find_element((By.ID, Locators_test.loginpassword_id))
        PasswordField.send_keys(Locators_test.login_password)

        ClickLogin = self.driver.find_element((By.ID, Locators_test.clicklogin_id))
        while ClickLogin.click() is True:
            break

        time.sleep(5)

        userName = self.driver.find_element((By.XPATH, Locators_test.username_xpath))
        print("Logged in as", userName.text)



    except StaleElementReferenceException or ElementClickInterceptedException or TimeoutException as ex:
        print(ex.message)

test_main.py

def test_login(test_setup):

from Smoketest.pages.test_login import Login

lo = Login(driver)
lo.login_user(((Locators_test.loginlink_xpath,Locators_test.loginemail_id,Locators_test.login_email,Locators_test.loginpassword_id,Locators_test.login_password,Locators_test.clicklogin_id,Locators_test.username_xpath)))
  • Welcome. This line: lo = Login(driver), where do you get the variable driver? Your example doesn't show that. The last line in test_main.py, you're calling the function login_user() with several parameters, but look at your definition in Login class... there's only def login_user(self,driver):, that won't work well. – pavelsaman Feb 24 at 10:58
  • Your code doesn't even mention "using". I guess you've forgotten some part of it. – João Farias Feb 24 at 11:15
  • @pavelsaman Hi, I have initiated self.driver in test_login.py in init method and calling it in test_main.py – Mohan Feb 24 at 12:03
  • @JoãoFarias Hi, I have not used 'using' anywhere in my code i have no idea where it comes from – Mohan Feb 24 at 12:04
  • 1
    @pavelsaman I have added test_setup in the question, please check – Mohan Feb 24 at 12:40
0

I fixed it myself by removing the extra pair of parenthesis from the line

loginButton = self.driver.find_element((By.XPATH, Locators_test.loginlink_xpath)).

Right way is

loginButton = self.driver.find_element(By.XPATH, Locators_test.loginlink_xpath)

ps: this applies to all the lines.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.