当前位置:网站首页>Page object and data driven test
Page object and data driven test
2022-06-29 18:57:00 【It's Joe Joe】
Data driven testing
That is to split the test data from the test script according to the business logic , Make it an independent variable parameter , Realize the purpose of highly reusing test scripts under different data sets .
ddt library
python Medium ddt Library technology parameterizes the variables in the test , It contains a set of classes and methods for data-driven testing .
pip install ddt
Make the previous example of Taobao login data-driven
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): # Parameter declarations
driver=self.driver
driver.find_element(By.LINK_TEXT, " Pro - , Please log in ").click() # Click on the simulation
login_user = driver.find_element(By.ID, "fm-login-id")
login_password=driver.find_element(By.ID, "fm-login-password") # Locate the password box page element
# self.assertEqual("40", login_password.get_attribute("maxlength")) # Verify the maximum length of the password input box
# Check if an element is visible and usable
# self.assertTrue(login_password.is_displayed())
# self.assertTrue(login_password.is_enabled())
# Simulate user input and login , Be careful : In many cases, there will be slider and verification code verification
login_user.send_keys(login_id) # Login account parameter transfer
login_password.send_keys(pwd) # Login password transfer parameter
login_button=driver.find_element(By.LINK_TEXT, " Sign in ")
login_button.click()
Data driven through external files
1. adopt csv File incoming data
import csv # Import csv package
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) # skip csv Header
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): # Parameter declarations
driver=self.driver
driver.find_element(By.LINK_TEXT, " Pro - , Please log in ").click() # Click on the simulation
login_user = driver.find_element(By.ID, "fm-login-id")
login_password=driver.find_element(By.ID, "fm-login-password") # Locate the password box page element
# self.assertEqual("40", login_password.get_attribute("maxlength")) # Verify the maximum length of the password input box
# Check if an element is visible and usable
# self.assertTrue(login_password.is_displayed())
# self.assertTrue(login_password.is_enabled())
# Simulate user input and login , Be careful : In many cases, there will be slider and verification code verification
login_user.send_keys(login_id) # Login account parameter transfer
login_password.send_keys(pwd) # Login password transfer parameter
login_button=driver.find_element(By.LINK_TEXT, " Sign in ")
login_button.click()
2. adopt excel File incoming data
xlrd: read Excel library
xlwd: Write Excel library
openpyxl: Provides Excel Read write function
import csv # Import csv package
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): # Parameter declarations
driver=self.driver
driver.find_element(By.LINK_TEXT, " Pro - , Please log in ").click() # Click on the simulation
login_user = driver.find_element(By.ID, "fm-login-id")
login_password=driver.find_element(By.ID, "fm-login-password") # Locate the password box page element
# self.assertEqual("40", login_password.get_attribute("maxlength")) # Verify the maximum length of the password input box
# Check if an element is visible and usable
# self.assertTrue(login_password.is_displayed())
# self.assertTrue(login_password.is_enabled())
# Simulate user input and login , Be careful : In many cases, there will be slider and verification code verification
login_user.send_keys(login_id) # Login account parameter transfer
login_password.send_keys(pwd) # Login password transfer parameter
login_button=driver.find_element(By.LINK_TEXT, " Sign in ")
login_button.click()
page Object Pattern
When designing tests , Abstract the elements and methods according to the page , To separate into certain objects , And then organize .
That is, create an object to correspond to an application of the page .
Advantages as follows :
- Abstract objects , You can minimize the impact of modifying the page code on the test .
- You can reuse part of the test code in multiple test cases .
- Test code becomes more readable 、 flexible 、 Maintainable .
边栏推荐
- 报错Failed to allocate graph: MYRIAD device is not opened.
- Basis of data analysis -- prediction model
- How to use an oak camera as a webcam?
- 产品-Axure9(英文版),中继器(Repeater)实现表格内容的增删查改(CRUD)
- 《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26
- Machine learning 7-Support vector machine
- SD6.23集训总结
- 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
- Shell基本语法--流程控制
- Sd6.24 summary of intensive training
猜你喜欢

面霸篇:MySQL六十六问,两万字+五十图详解!

JDBC knowledge

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

These advantages of the Institute are really fragrant! The landing rate is still very high!

七牛图片访问参数(缩略图小尺寸啥的,避免浪费流量)

Stepping on the pit: json Parse and json stringify

Mac: MySQL 66 questions, 20000 words + 50 pictures!

TP5 where queries whether a field in the database contains a value. Fuzzy queries are performed without the like method

The 8th "Internet +" competition - cloud native track invites you to challenge

Apache Doris 基本使用总结
随机推荐
About microservices
Dragon canvas animation
SD6.24集训总结
curl下载示例
Redis bloom filter and cuckoo filter
防汛救援便携式应急通信系统解决方案
MySQL enterprise development specification
Sd6.24 summary of intensive training
优雅书写Controller(参数验证+统一异常处理)
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
山东大学项目实训(八)设计轮播图进入页面
Anaconda安装并配置jupyter notebook远程
535. encryption and decryption of tinyurl / Jianzhi offer II 103 Minimum number of coins
Understanding of strong caching and negotiation caching
如何使用物联网低代码平台进行服务管理?
The table ‘table_name‘ is full 异常排查及解决方案
How to use the low code platform of the Internet of things for service management?
PHP implementation of sorting two-dimensional arrays by specified key names
数据仓库模型分层ODS、DWD、DWM实战
75. nearest common ancestor of binary search tree