【郑州校区】数据驱动实战二
目标1. 掌握数据驱动的开发流程 2. 掌握如何读取JSON数据文件 3. 巩固PO模式 1. 案例 对TPshop网站的登录模块进行单元测试
1.1 实现步骤 1. 编写测试用例 2. 采用PO模式的分层思想对页面进行封装 3. 编写测试脚本 4. 定义数据文件,实现参数化
1.2 用例设计
1.3 数据文件 [AppleScript] 纯文本查看 复制代码
{
"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示例代码 [AppleScript] 纯文本查看 复制代码 from selenium import webdriver
def get_tips_msg(): """
获取弹出框的提示消息
:return: 消息文本内容
"""
msg = DriverUtil.get_driver().find_element_by_class_name("layui-layer-content").tex
t
return msg [AppleScript] 纯文本查看 复制代码 class DriverUtil: """
浏览器驱动工具类
"""
_driver = None @classmethod
def 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
@classmethod
def quit_driver(cls): """
关闭浏览器驱动
"""
if cls._driver: cls._driver.quit() cls._driver = None [AppleScript] 纯文本查看 复制代码 # 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")
# 密 码 [AppleScript] 纯文本查看 复制代码 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() [AppleScript] 纯文本查看 复制代码 # 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):
@classmethod
def setUpClass(cls):
cls.driver = DriverUtil.get_driver() cls.login_proxy = LoginProxy()
@classmethod
def 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)
|
|