0

I have 3 files:

  • utlis.py is the base po for page actions
  • login_page_po.py is the login page po
  • test_login.py is the test I defined

utlis.py

class DriverUtil:
    """Common utils for browser"""

    def __init__(self, browser_type=0):
        if browser_type == 0:
            self.driver = webdriver.Chrome()
        elif browser_type == 1:
            self.driver = webdriver.Firefox()
        else:
            raise NameError('Invalid browser!')

    def openurl(self, url):
        self.driver.get(url)

    def find_element(self, value):
        return self.driver.find_element(By.CSS_SELECTOR, value)

    def get_text(self, value):
        return self.find_element(value).text

    def input_text(self, value, txt):
        self.find_element(value).send_keys(txt)

    def click_element(self, value):
        self.find_element(value).click()

    def quit_driver(self):
        self.driver.quit()

login_page_po.py

elementsMap = {
    'usernameInputField': 'input[name="username"]',
    'passwordInputField': 'input[name="password"]',
    'login_button': 'input[value="login"]',
    'error_message': 'div >p'
}

test_login.py

from utlis import DriverUtil
from login_page_po import elementsMap


class TestLogin:

    def setup_class(self):
        self.driver = DriverUtil()
        self.driver.openurl('https://www.stealmylogin.com/demo.html')

    def teardown_class(self):
        self.driver.quit_driver()

    def setup(self):
        self.driver.openurl('https://www.stealmylogin.com/demo.html')

    def test_wrong_username(self):
        self.driver.input_text(elementsMap['usernameInputField'], 'wrongadmin')
        self.driver.input_text(elementsMap['passwordInputField'], 'admin')
        self.driver.click_element(elementsMap['login_button'])

    def test_wrong_password(self):
        self.driver.input_text(elementsMap['usernameInputField'], 'admin')
        self.driver.input_text(elementsMap['passwordInputField'], 'wrongpassword')
        self.driver.click_element(elementsMap['login_button'])


So far I make both tests pass. One browser opens and 2 tests are executed and passed. But there are duplicated codes inside my test_login.py (see below)

        self.driver.input_text(elementsMap['usernameInputField'], 'wrongadmin')
        self.driver.input_text(elementsMap['passwordInputField'], 'admin')
        self.driver.click_element(elementsMap['login_button'])

I modified the duplicated via moving it to login_page_po.py. Now login_page_po.py looks like below

from utlis import DriverUtil

elementsMap = {
    'usernameInputField': 'input[name="username"]',
    'passwordInputField': 'input[name="password"]',
    'login_button': 'input[value="login"]',
    'error_message': 'div >p'
}

class Login:

    def __init__(self):
        self.driver = DriverUtil()
        self.driver.openurl('https://www.stealmylogin.com/demo.html')

    def login_method(self, username, password):
        self.driver.input_text(elementsMap['usernameInputField'], username)
        self.driver.input_text(elementsMap['passwordInputField'], password)
        self.driver.click_element(elementsMap['login_button'])

and test_login.py looks like below:

from utlis import DriverUtil
from login_page_po import Login


class TestLogin:

    def setup_class(self):
        self.driver = DriverUtil()
        self.driver.openurl('https://www.stealmylogin.com/demo.html')
        self.login = Login()

    def teardown_class(self):
        self.driver.quit_driver()

    def setup(self):
        self.driver.openurl('https://www.stealmylogin.com/demo.html')

    def test_wrong_username(self):
        self.login.login_method('wrongadmin', 'admin')

    def test_wrong_password(self):
        self.login.login_method('admin', 'wrongpassword')

Now the tests start failing and I can see 2 browsers opened when I executed the test and the 1st one doesn't do anything. The first test was executed on the 2nd opened browser. Did I do something wrong?

4
  • Welcome to SO! Which framework are you using? If it is pytest, then why not make use of fixtures? I see that you are opening browser in class Login and then in class TestLogin also, why is it so? Commented Aug 18, 2022 at 5:34
  • @AnandGautam Thanks for the reply. Yes! I am using pytest and I am new to pytest. Just start to learn. Can you explain to me a little about how to use fixtures? Thank you so much! Commented Aug 18, 2022 at 15:42
  • @AnandGautam I tried only to define the login_method as a function not class. But I cannot successfully import it into my test_login.py. I don't know what is a good way to do it. Commented Aug 18, 2022 at 15:45
  • I don't know Python but I think each time you call "DriverUtil()" it's running init as a sort of constructor.... which creates a new webdriver (which will launch the browser as part of it's contructor) So you end up with one orphaned driver/browser here. (Since only the second assignment to self.driver will be available) Commented Aug 18, 2022 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.