结合UI自动化测试(八) 基本操作类封装&UI自动化测试(九) PageObject框架设计两章完成了框架设计
接下来我们就要开始对用例进行设计了
数据层
class LoginData:
invalid_data = [{"account": "asdsdas", "password": "dasdsdas", "content": "Please enter a valid email address"},
{"account": "lm11@qq.com", "password": "1424dgsdgf", "content": "user account doesn't exist"},
{"account": "lm@qq.com", "password": "sdgsd12515", "content": "password error"}]
success_data = {"account": "lm@qq.com", "password": "asdfghj123", "title": "My Order"}
定位层
from selenium.webdriver.common.by import By
class LoginPageLocators:
account = (By.XPATH, '//*[@name="account"]')
password = (By.XPATH, '//*[@name="password"]')
button = (By.XPATH, '//*[@id="login-btn"]')
content = (By.XPATH, '//*[@class="content"]')
逻辑层
from pagelocators.login_loc import LoginPageLocators as ll
from common.basepage import BasePage
class LoginPage(BasePage):
def login(self, user, password):
self.input_keys("login_account", ll.account, user).input_keys("login_password", ll.password, password)
self.ele_click("login_button", ll.button)
def get_login_content(self):
return self.get_text("login_content_text", ll.content)
前置条件
@pytest.fixture()
def init_driver():
driver = webdriver.Chrome(executable_path=DRIVER_DIR + "/chromedriver.exe")
driver.get(url_conf["web_url"])
yield driver
driver.quit()
driver.close()
用例
import pytest
from pagedatas.login_data import LoginData as ld, MyOrderData as md
from pageobject.login_page import LoginPage as lp
from pageobject.my_order_page import MyOrderPage as mp
@pytest.mark.smoke
@pytest.mark.usefixtures("init_driver")
class TestLogin:
def test_login_success(self, init_driver):
lp(init_driver).login(ld.success_data["account"], ld.success_data["password"])
title = mp(init_driver).get_my_order_title()
assert ld.success_data["title"] in title
@pytest.mark.parametrize("data", ld.invalid_data)
def test_login_failed(self, init_driver, data):
driver = lp(init_driver)
driver.login(data["account"], data["password"])
content = driver.get_login_content()
assert content == data["content"]