目标
- 掌握数据驱动的开发流程
- 掌握如何读取JSON数据文件
- 巩固PO模式
1. 案例
对TPshop网站的登录模块进行单元测试
1.1 实现步骤
- 编写测试用例
- 采用PO模式的分层思想对页面进行封装
- 编写测试脚本
- 定义数据文件,实现参数化
1.2 用例设计
1.3 数据文件
{"login_username_is_null": {"username": "","password": "123456","code": "8888","is_success": false,"expect": "用户名不能为空"},"login_password_is_null": {"username": "13012345678","password": "","code": "8888","is_success": false,"expect": "密码不能为空"},"login_password_is_error": {"username": "13012345678","password": "error","code": "8888","is_success": false,"expect": "密码错误"},"login_success": {"username": "13012345678","password": "123456","code": "8888","is_success": true,"expect": "我的账户"}
}
1.4 示例代码
from selenium import webdriver
def get_tips_msg():"""获取弹出框的提示消息:return: 消息文本内容"""msg = DriverUtil.get_driver().find_element_by_class_name("layui-layer-content").tex
treturn msg
class DriverUtil:"""浏览器驱动工具类"""_driver = None@classmethoddef get_driver(cls):"""获取浏览器驱动对象,并完成初始化设置:return: 浏览器驱动对象"""if cls._driver is None:cls._driver = webdriver.Firefox()cls._driver.maximize_window()cls._driver.implicitly_wait(10)cls._driver.get("http://localhost")return cls._driver@classmethoddef quit_driver(cls):"""关闭浏览器驱动"""if cls._driver:cls._driver.quit()cls._driver = None
# login_page.py
from selenium.webdriver.common.by import By
from ddt.tpshop.utils import DriverUtil
class LoginPage:"""登录页面-对象库层"""def __init__(self):self.driver = DriverUtil.get_driver()# 用户名输入框self.username = (By.ID, "username")# 密码self.password = (By.ID, "password")# 验证码self.verify_code = (By.ID, "verify_code")# 登录按钮self.login_btn = (By.NAME, "sbtbutton")def find_username(self):return self.driver.find_element(*self.username)def find_password(self):return self.driver.find_element(*self.password)def find_verify_code(self):return self.driver.find_element(*self.verify_code)def find_login_btn(self):return self.driver.find_element(*self.login_btn)
class LoginHandle:"""登录页面-操作层"""def __init__(self):self.login_page = LoginPage()def input_username(self, username):self.login_page.find_username().send_keys(username)def input_password(self, pwd):self.login_page.find_password().send_keys(pwd)def input_verify_code(self, code):self.login_page.find_verify_code().send_keys(code)def click_login_btn(self):self.login_page.find_login_btn().click()
class LoginProxy:"""登录页面-业务层"""def __init__(self):self.login_handle = LoginHandle()def login(self, username, password, code):self.login_handle.input_username(username)self.login_handle.input_password(password)self.login_handle.input_verify_code(code)self.login_handle.click_login_btn()
# test_login.py
import json
import time
import unittest
from parameterized import parameterized
from ddt.tpshop import utils
from ddt.tpshop.page.login_page import LoginProxy
from ddt.tpshop.utils import DriverUtil
# 构建测试数据
def build_data():test_data = []with open("../data/testData.json", encoding='UTF-8') as f:json_data = json.load(f)for login_data in json_data.values():test_data.append((login_data.get("username"),login_data.get("password"),login_data.get("code"),login_data.get("is_success"),login_data.get("expect")))print("test_data=", test_data)return test_data
class TestLogin(unittest.TestCase):@classmethoddef setUpClass(cls):cls.driver = DriverUtil.get_driver()cls.login_proxy = LoginProxy()@classmethoddef tearDownClass(cls):DriverUtil.quit_driver()def setUp(self):# 进入首页self.driver.get("http://localhost")
# 点击登录链接self.driver.find_element_by_link_text("登录").click()@parameterized.expand(build_data)def test_add(self, username, password, code, is_success, expect):print('username={} password={} code={} is_success={} expect={}'.format(username, password, code, is_success, expect))# 登录self.login_proxy.login(username, password, code)time.sleep(3)# 登录成功的用例if is_success:self.assertIn(expect, self.driver.title)else:# 获取提示框消息msg = utils.get_tips_msg()print("msg=", msg)self.assertIn(expect, msg)