当前位置:网站首页>Page Object与数据驱动测试
Page Object与数据驱动测试
2022-06-29 18:29:00 【是乔乔啊】
数据驱动测试
即根据业务逻辑将测试数据从测试脚本中拆分出来,使其成为独立的变参,实现测试脚本在不同数据集合下高度复用的目的。
ddt库
python中的ddt库科技将测试中的变量参数化,其包含一组类和方法用于数据驱动测试。
pip install ddt
将之前淘宝登录的例子数据驱动化
import unittest
from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.common.by import By
@ddt
class HomePageTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# create a new Firefox session
cls.driver = webdriver.Chrome()
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
# navigate to the application home page
cls.driver.get("https://www.taobao.com/")
@data(("login_id1", "pwd1"),("login_id2". "pwd2"))
@unpack
def test_register_new_user(self, login_id, pwd): # 参数声明
driver=self.driver
driver.find_element(By.LINK_TEXT, "亲,请登录").click() # 模拟点击
login_user = driver.find_element(By.ID, "fm-login-id")
login_password=driver.find_element(By.ID, "fm-login-password") # 定位密码框页面元素
# self.assertEqual("40", login_password.get_attribute("maxlength")) # 验证密码输入框最大长度
# 检查某个元素是否可见和可用
# self.assertTrue(login_password.is_displayed())
# self.assertTrue(login_password.is_enabled())
# 模拟用户输入和登录,注意:很多情况下会有滑块和验证码验证
login_user.send_keys(login_id) # 登录账号传参
login_password.send_keys(pwd) # 登录密码传参
login_button=driver.find_element(By.LINK_TEXT, "登录")
login_button.click()
通过外部文件数据驱动
1.通过csv文件传入数据
import csv # 导入csv包
import unittest
from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.common.by import By
def get_data(file_name):
rows=[]
data_file=open(file_name, "rb")
reader=csv.reader(data_file)
next(reader,None) # 跳过csv表头
for row in reader:
rows.append(row)
return rows
@ddt
class HomePageTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# create a new Firefox session
cls.driver = webdriver.Chrome()
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
# navigate to the application home page
cls.driver.get("https://www.taobao.com/")
@data(*get_data("testdata.csv"))
@unpack
def test_register_new_user(self, login_id, pwd): # 参数声明
driver=self.driver
driver.find_element(By.LINK_TEXT, "亲,请登录").click() # 模拟点击
login_user = driver.find_element(By.ID, "fm-login-id")
login_password=driver.find_element(By.ID, "fm-login-password") # 定位密码框页面元素
# self.assertEqual("40", login_password.get_attribute("maxlength")) # 验证密码输入框最大长度
# 检查某个元素是否可见和可用
# self.assertTrue(login_password.is_displayed())
# self.assertTrue(login_password.is_enabled())
# 模拟用户输入和登录,注意:很多情况下会有滑块和验证码验证
login_user.send_keys(login_id) # 登录账号传参
login_password.send_keys(pwd) # 登录密码传参
login_button=driver.find_element(By.LINK_TEXT, "登录")
login_button.click()
2.通过excel文件传入数据
xlrd:读Excel文件库
xlwd:写Excel文件库
openpyxl:提供了Excel读写功能
import csv # 导入csv包
import unittest
from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.common.by import By
def get_data(file_name):
rows=[]
book=xlrd.open_workbook(file_name)
sheet=book.sheet_by_index(0)
for row_idx in range(1, sheet.nrows):
rows.append(likst(sheet.row_values(row_idx,0,sheet.ncols)))
return rows
@ddt
class HomePageTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# create a new Firefox session
cls.driver = webdriver.Chrome()
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
# navigate to the application home page
cls.driver.get("https://www.taobao.com/")
@data(*get_data("testdata.xlsx"))
@unpack
def test_register_new_user(self, login_id, pwd): # 参数声明
driver=self.driver
driver.find_element(By.LINK_TEXT, "亲,请登录").click() # 模拟点击
login_user = driver.find_element(By.ID, "fm-login-id")
login_password=driver.find_element(By.ID, "fm-login-password") # 定位密码框页面元素
# self.assertEqual("40", login_password.get_attribute("maxlength")) # 验证密码输入框最大长度
# 检查某个元素是否可见和可用
# self.assertTrue(login_password.is_displayed())
# self.assertTrue(login_password.is_enabled())
# 模拟用户输入和登录,注意:很多情况下会有滑块和验证码验证
login_user.send_keys(login_id) # 登录账号传参
login_password.send_keys(pwd) # 登录密码传参
login_button=driver.find_element(By.LINK_TEXT, "登录")
login_button.click()
page Object模式
在设计测试时,把元素和方法按照页面抽象出来,分离成一定的对象,然后再进行组织。
即创建一个对象来对应页面的一个应用。
优点如下:
- 抽象出对象,可以尽量降低修改页面代码对测的影响。
- 可以在多个测试用例中复用一部分测试代码。
- 测试代码变得更易读、灵活、可维护。
边栏推荐
- jdbc_ Related codes
- 深度学习---三好学生各成绩所占权重问题(2)
- My first experience of remote office | community essay solicitation
- Sd6.24 summary of intensive training
- 【网络是怎么连接的】第三章 探索集线器,交换机和路由器
- Tag filtering and SQL filtering of rocketmq
- 数据分析--时间序列预测
- Weibo comments on high-performance and high availability architecture design (module 5 of architecture practice camp)
- MySQL Enterprise Development Specification
- JWT login authentication
猜你喜欢

js文本粒子动态背景

About microservices

Machine learning 7-Support vector machine

Travel card "star picking" hot search first! Stimulate the search volume of tourism products to rise

美法官裁定,被控掩盖黑客行为的Uber前安全主管必须面对欺诈指控

Us judge ruled that the former security director of Uber accused of covering up hacking must face fraud charges

2. add customized related files to the keil5 project established by stm32cubemx

garbage collector

Know that Chuangyu has helped the energy industry in asset management and was selected into the 2021 IOT demonstration project of the Ministry of industry and information technology

Svg circle drawing path animation
随机推荐
Fastdfs
C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
About microservices
AMAZING PANDAVERSE:META”无国界,来2.0新征程激活时髦属性
Sister Juan takes you to learn database -- 5-day sprint Day1
How do I add SmartArt to slides in PowerPoint?
C Primer Plus 第12章_存储类别、链接和内存管理_代码和练习题
Encryption and decryption of 535 tinyurl
Anfulai embedded weekly report no. 271: June 20, 2022 to June 26, 2022
6.29模拟赛总结
如何在树莓派上使用OAK相机?
How to use the low code platform of the Internet of things for service management?
RocketMQ的tag过滤和sql过滤
MySQL 企業級開發規範
1. use stm32cubemx to establish stm32g030c8t6 project
6.29 simulation summary
Apache inlong million billion level data stream processing
行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
garbage collector
WBF:检测任务NMS后虑框新方式?