【郑州校区】数据驱动实战一
目标1. 掌握数据驱动的开发流程 2. 掌握如何读取JSON数据文件 3. 巩固PO模式 1. 案例
1.1 实现步骤 1. 采用PO模式的分层思想对页面进行封装 2. 编写测试脚本 使用参数化传入测试数据 1. 把测试数据定义到JSON数据文件中
1.2 数据文件 第一个数字加第二个数字等于第三个数字,每一行数据代表一个用例
[AppleScript] 纯文本查看 复制代码 class DriverUtil: """
浏览器驱动工具类
"""
_driver = None @classmethod
def get_driver(cls): """
获取浏览器驱动对象,并完成初始化设置
:return: 浏览器驱动对象
"""
if cls._driver is None:
cls._driver = webdriver.Chrome() cls._driver.maximize_window() cls._driver.implicitly_wait(10) cls._driver.get("http://cal.apple886.com/")
return cls._driver
@classmethod
def quit_driver(cls): """
关闭浏览器驱动
"""
if cls._driver: cls._driver.quit()
[AppleScript] 纯文本查看 复制代码 cls._driver = None
[AppleScript] 纯文本查看 复制代码 from selenium.webdriver.common.by import By
from ddt.calculator.utils import DriverUtil
class CalculatorPage: """
计算器页面-对象库层
"""
def init (self):
self.driver = DriverUtil.get_driver()
# 数字按钮
self.digit_btn = (By.ID, "simple{}") # 加法按钮
self.add_btn = (By.ID, "simpleAdd")
# 等号按钮
self.eq_btn = (By.ID, "simpleEqual")
# 计算结果
self.result = (By.ID, "resultIpt")
def find_digit_btn(self, digit):
location = (self.digit_btn[0], self.digit_btn[1].format(digit)) return self.driver.find_element(*location)
def find_add_btn(self):
return self.driver.find_element(*self.add_btn)
def find_eq_btn(self):
return self.driver.find_element(*self.eq_btn)
def find_result_btn(self):
return self.driver.find_element(*self.result)
class CalculatorHandle: """
计算器页面-操作层
"""
def init (self):
self.calculator_page = CalculatorPage()
def click_digit_btn(self, digit):
self.calculator_page.find_digit_btn(digit).click()
def click_add_btn(self): self.calculator_page.find_add_btn().click()
def click_eq_btn(self): self.calculator_page.find_eq_btn().click()
def get_result(self):
return self.calculator_page.find_result_btn().get_attribute("value")
def input_numbers(self, numbers): for num in numbers:
self.click_digit_btn(num)
class CalculatorProxy: """
计算器页面-业务层
"""
def init (self):
self.calculator_handle = CalculatorHandle()
def add(self, num1, num2): self.calculator_handle.input_numbers(str(num1)) self.calculator_handle.click_add_btn() self.calculator_handle.input_numbers(str(num2)) self.calculator_handle.click_eq_btn()
def get_result(self):
return self.calculator_handle.get_result()
[AppleScript] 纯文本查看 复制代码
import json import time import unittest
from parameterized import parameterized
from ddt.calculator.page.calculator_page import CalculatorProxy from ddt.calculator.utils import DriverUtil
def build_data(): test_data = []
with open("../data/calculator.json", encoding='UTF-8') as f: test_data = json.load(f)
print("test_data=", test_data) return test_data
class TestCalculator(unittest.TestCase): @classmethod
def setUpClass(cls):
cls.driver = DriverUtil.get_driver() cls.calculatorProxy = CalculatorProxy()
@classmethod
def tearDownClass(cls): DriverUtil.quit_driver()
@parameterized.expand(build_data) def test_add(self, a, b, expect):
print('a={} b={} expect={}'.format(a, b, expect)) self.calculatorProxy.add(a, b)
# 获取计算结果
result = self.calculatorProxy.get_result() print("result=", result) self.assertEqual(result, str(expect))
|