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?
fixtures? I see that you are opening browser in classLoginand then in classTestLoginalso, why is it so?