我尝试用Python实现页面对象模型。我有两个问题:
verify_login_failure
和verify_login_success
中都制作了新的页面对象?这可能是因为我的driver.get()
错误地放在了我的BasePage
类中。请告知。driver.quit()
是为了在所有测试完成后正常结束测试套件的执行?这是我的代码:
# Basepage.py
from selenium import webdriver
class BasePage(object):
def __init__(self):
self.driver = webdriver.Chrome(
executable_path="chromedriver_mac")
self.driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account")
self.driver.implicitly_wait(5)
# LoginPage.py
from selenium.webdriver.common.by import By
from page_object.pages.BasePage import BasePage
class Login(BasePage):
email = (By.ID, "email")
password = (By.ID, "passwd")
sign_in_btn = (By.ID, "SubmitLogin")
failure_message = (By.XPATH, "//li[contains(text(),'Authentication failed.')]")
def set_email(self, email):
email_element = self.driver.find_element(*Login.email)
email_element.clear()
email_element.send_keys(email)
def set_password(self, password):
password_element = self.driver.find_element(*Login.password)
password_element.clear()
password_element.send_keys(password)
def click_sign_in_btn(self):
sign_in_element = self.driver.find_element(*Login.sign_in_btn)
sign_in_element.click()
def is_failure_message_displayed(self):
failure_message_element = self.driver.find_element(*Login.failure_message)
return failure_message_element.is_displayed()
def login(self, email, password):
self.set_email(email)
self.set_password(password)
self.click_sign_in_btn()
# MyAccountPage.py
from selenium.webdriver.common.by import By
from page_object.pages.BasePage import BasePage
class MyAccount(BasePage):
my_account_text = (By.XPATH, "LoginPage.py")
def is_my_account_text_displayed(self):
my_account_text_element = self.driver.find_element(*MyAccount.my_account_text)
return my_account_text_element.is_displayed()
# LoginTests.py
import lemoncheesecake.api as lcc
from lemoncheesecake.matching import check_that, is_true
from page_object.pages.LoginPage import Login
from page_object.pages.MyAccountPage import MyAccount
@lcc.suite("Login page tests")
class LoginTests:
def setup_suite(self):
lcc.log_info("Inside setup")
@lcc.test("check login failure")
def verify_login_failure(self):
login = Login()
login.login(email="myemail@gmail.com", password="incorrectpasswd")
check_that("Failure message is displayed", login.is_failure_message_displayed(), is_true())
@lcc.test("check login success")
def verify_login_success(self):
login = Login()
login.login(email="myemail@gmail.com", password="correctpasswd")
my_account = MyAccount()
check_that("My Account text is displayed", my_account.is_my_account_text_displayed(), is_true())
def teardown_suite(self):
lcc.log_info("Inside teardown")
答案 0 :(得分:0)
为什么在我的测试运行中浏览器启动三次?是因为我要在每个测试中创建新的页面对象verify_login_failure和verify_login_success吗?这可能是因为我的driver.get()错误地放在了我的BasePage类中。请告知。
每次您初始化页面时,父类BasePage的构造函数也会初始化。如果您想在一个地方进行驱动程序初始化,那么我建议从LoginTests扩展BasePage(当然是重命名)
在所有测试完成后,我应该在哪里调用driver.quit()以便正常结束测试套件的执行?
最好的方法是在setup_suite()中初始化浏览器会话,并在teardown_suite()中退出浏览器会话。至少我们是用Java编写的,这种格式允许我们并行运行测试。
答案 1 :(得分:0)
将我的测试班级更改为以下内容很有帮助。
import lemoncheesecake.api as lcc
from lemoncheesecake.matching import check_that, is_true
from selenium import webdriver
from core.common.constants import Global
from core.utils.fileutils import capture_screenshot
from pages.login_page import LoginPage
from pages.my_account_page import MyAccountPage
@lcc.suite("Login page tests")
class LoginTests(object):
login, my_account, driver = None, None, None
def setup_suite(self):
lcc.log_info("Inside setup")
self.driver = webdriver.Chrome(executable_path=Global.CHROME_DRIVER)
self.login = LoginPage(driver=self.driver)
self.my_account = MyAccountPage(driver=self.driver)
@lcc.test("check login failure")
def verify_login_failure(self):
self.login.login(email="myemail@gmail.com", password="incorrectpasswd")
check_that("Failure message is displayed", self.login.is_failure_message_displayed(), is_true())
@lcc.test("check login success")
def verify_login_success(self):
self.login.login(email="myemail@gmail.com", password="correctpasswd")
check_that("My Account text is displayed", self.my_account.is_my_account_text_displayed(), is_true())
def teardown_test(self, test_name):
lcc.log_info("Finished running test " + test_name)
capture_screenshot(self.driver)
def teardown_suite(self):
lcc.log_info("Inside teardown")
self.driver.quit()