2

Hi I'm trying to learn Python/Selenium using a Page Object Model.

I have been following this tutorial and as far as I can tell I haven't missed anything. However I can't run my tests because of the init constructor. In the video, he has the same init constructor and the tests run for him.

Inside my project folder I have a subfolder pageObjects and testCases.

In pageObjects I have LoginPage.py where i set up my locators and actions i want to perform on the locators

class LoginPage:
    textbox_username_id = "Email"
    textbox_password_id = "Password"
    button_login_xpath = "//input[@class='button-1 login-button']"
    link_logout_linktext = "Logout"

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

    def setUserName(self,username):
        self.driver.find_element_by_id(self.textbox_username_id).clear()
        self.driver.find_element_by_id(self.textbox_username_id).send_keys(username)

    def setPassword(self,password):
        self.driver.find_element_by_id(self.textbox_password_id).clear()
        self.driver.find_element_by_id(self.textbox_password_id).send_keys(password)

    def clickLogin(self):
        self.driver.find_element_by_xpath(self.button_login_xpath).click()

    def clickLogout(self):
        self.driver.find_element_by_link_text(self.link_logout_linktext).click()

In testCases I have test_login.py and conftest.py

I'll show conftest.py first

from selenium import webdriver
import pytest


@pytest.fixture()
def setup():
    driver = webdriver.Chrome()
    return driver

and test_login.py

import pytest
from selenium import webdriver
from pageObjects.LoginPage import LoginPage
from testCases import conftest

class Test_001_Login:
    baseURL = "https://admin=demo.nopcommerce.com/"
    username = "admin@yourstore.com"
    password = "admin"

    def __init__(self):
        self.lp = LoginPage(self.driver)
        self.driver = conftest.setup

    def test_homePageTitle(self, setup):
        self.driver = setup
        self.driver.get(self.baseURL)
        act_title = self.driver.title
        self.driver.close()
        if act_title == "Your store. Login":
            assert True
        else:
            assert False

    def test_login(self, setup):
        self.driver = setup
        self.driver.get(self.baseURL)
        self.lp.setUserName(self.username)
        self.lp.setPassword(self.password)
        self.lp.clickLogin()
        act_title = self.driver.title
        if act_title == "Dashboard / nopCommerce administration":
            assert True
        else:
            assert False

There are 2 issues.

  1. conftest.py is supposed to be a special type of class/fixture. I should be able to call it to create an instance of webdriver.Chrome(). However it does not work. In test_login.py when i call 'setup' I get an error 'unresolved reference 'setup''. So I've had to manually import it as a work around. Isn't the whole point of the conftest.py file so that my test_* files have access to the webdriver in conftest.py wihtout having to import it?

  2. When i try to run the test_login.py file, I get a warning and 0 items are collected - testCases\test_login.py:6 C:\Users\Mirza\PycharmProjects\nopcommerceApp\testCases\test_login.py:6: PytestCollectionWarning: cannot collect test class 'Test_001_Login' because it has a init constructor (from: testCases/test_login.py) class Test_001_Login:

I have absolutely no idea what to do here. Why can't my class Test_001_Login have an init constructor? I need it to create an instance of the LoginPage class and to create an instance of the webdriver from conftest.py.

Any help would be greatly appreciated.

Mirza
  • 21
  • 3
  • 1
    Does https://stackoverflow.com/questions/21430900/py-test-skips-test-class-if-constructor-is-defined help? – OneLiner Dec 10 '20 at 19:52
  • Right I removed the __init__ section of my code in the test_login.py The tests runs, opens a Chrome browser but then fails and says def test_login(self, setup): > self.lp = LoginPage(self.driver) E AttributeError: 'Test_001_Login' object has no attribute 'driver' – Mirza Dec 10 '20 at 20:29
  • i believe issue 2 was a result of issue 1. Because my conftest.py isnt working as intended. The IDE recommended 'moving attribute to __init__ method' So that's 1 problem resolved.. Now the original problem is that conftest.py is not working as a fixture so that my test cases can create an instance of the driver. – Mirza Dec 10 '20 at 20:35

0 Answers0