当前位置:网站首页>Data driven testing of hand-in-hand teaching UI automation
Data driven testing of hand-in-hand teaching UI automation
2022-07-21 23:40:00 【Pot talk soft test】
stay UI In the automated testing of , We need to separate the data used in the test into files , If it is simply written in our test module , Not a good design , So no matter what type of automated testing , It is necessary to separate the data . Of course, it is separated into specific documents , There are many forms of documents , The main point here is JSON Documents and YAML The documents are in UI Application in automated testing .
One 、JSON file
JSON Library is mainly used in serialization and deserialization , Especially in API In the automated testing of , Serialization and deserialization are the systems of technology stacks that must be mastered in the knowledge system . Of course, we can also deal with serialization and deserialization of files , Serialization of files can be simply understood as writing third-party data into files , Use JSON The method in the library is dump() Method , Then the anti serialization of files can be understood as reading data from files , Used JSON In the library load() Methods , The following is mainly for UI Automate the separation of test data , The specific code is :
import unittest
from parameterized import parameterized,param
from selenium import webdriver
import time as t
#parameterized Is a parameterized Library
class AddTest(unittest.TestCase):
def setUp(self) -> None:
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get('https://mail.sina.com.cn/#')
def tearDown(self) -> None:
self.driver.quit()
@parameterized.expand([
param('','',' Please enter the email name '),
param('srtSA','saert',' The email name you entered is not in the correct format '),
param('[email protected]','asdfrty',' Wrong login or password ')
])
def test_sina_email(self,username,password,result):
t.sleep(2)
self.driver.find_element_by_id('freename').send_keys(username)
t.sleep(2)
self.driver.find_element_by_id('freepassword').send_keys(password)
t.sleep(2)
self.driver.find_element_by_link_text(' Sign in ').click()
t.sleep(3)
div=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
assert div.text==result
if __name__ == '__main__':
unittest.main(verbosity=2)
You can see in the above file , We can separate the test data into JSON In file , The separated data is :
{
"data":
[
{"username": "","password": "","text": " Please enter the email name "},
{"username": "srtSA","password": "saert","text": " The email name you entered is not in the correct format "},
{"username": "[email protected]","password": "asdfrty","text": " Wrong login or password "}
]
}
The improved test script is :
import unittest
from parameterized import parameterized,param
from selenium import webdriver
import time as t
import json
def readJson():
return json.load(open('sina.json'))['data']
class AddTest(unittest.TestCase):
def setUp(self) -> None:
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get('https://mail.sina.com.cn/#')
def tearDown(self) -> None:
self.driver.quit()
@parameterized.expand([
param(readJson()[0]['username'],readJson()[0]['password'],readJson()[0]['text']),
param(readJson()[1]['username'],readJson()[1]['password'],readJson()[1]['text']),
param(readJson()[2]['username'],readJson()[2]['password'],readJson()[2]['text'])
])
def test_sina_email(self,username,password,result):
t.sleep(2)
self.driver.find_element_by_id('freename').send_keys(username)
t.sleep(2)
self.driver.find_element_by_id('freepassword').send_keys(password)
t.sleep(2)
self.driver.find_element_by_link_text(' Sign in ').click()
t.sleep(3)
div=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
assert div.text==result
if __name__ == '__main__':
unittest.main(verbosity=2)
Two 、YAML file
Let's demonstrate how to store the test data in YAML In the document , The contents of the separated file are :
username: ""
password: ""
text: " Please enter the email name "
---
username: "srtSA"
password: "saert"
text: " The email name you entered is not in the correct format "
---
username: "[email protected]"
password: "asdfrty"
text: " Wrong login or password "
The improved test script is :
import unittest
from parameterized import parameterized,param
from selenium import webdriver
import time as t
import json
import yaml
def readJson():
return json.load(open('sina.json'))['data']
def readYaml():
with open('sina.yaml') as f:
return list(yaml.unsafe_load_all(f))
class AddTest(unittest.TestCase):
def setUp(self) -> None:
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get('https://mail.sina.com.cn/#')
def tearDown(self) -> None:
self.driver.quit()
@parameterized.expand([
param(readYaml()[0]['username'],readYaml()[0]['password'],readYaml()[0]['text']),
param(readYaml()[1]['username'],readYaml()[1]['password'],readYaml()[1]['text']),
param(readYaml()[2]['username'],readYaml()[2]['password'],readYaml()[2]['text'])
])
def test_sina_email(self,username,password,result):
t.sleep(2)
self.driver.find_element_by_id('freename').send_keys(username)
t.sleep(2)
self.driver.find_element_by_id('freepassword').send_keys(password)
t.sleep(2)
self.driver.find_element_by_link_text(' Sign in ').click()
t.sleep(3)
div=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
assert div.text==result
if __name__ == '__main__':
unittest.main(verbosity=2)
Above , A detailed demonstration of the UI In the automated testing of , We can separate the test data used into JSON Document and YAML The file of , This achieves the separation of data , The purpose is to make the test simpler and more efficient .
边栏推荐
- 设置表单
- 页面重定向
- Day03 test case knowledge points summary (Part 2)
- Using array plus function to count the average score of students in a class
- 根据进程名一键批量结束进程(chromedriver.exe)
- Use of main function with command line parameters
- Matching strategies and methods: known detection, the ratio of nearest neighbors to next nearest neighbors, fast matching methods, etc. (3D reconstruction task1-6)
- sysstat安装并升级到11.5.5版本
- 类的继承性实验报告
- 棋盘覆盖问题
猜你喜欢
“cannot get hvm parameter CONSOLE_EVTCHN (18): -22!”的解决方法
JMeter之BeanShell的变量使用方法
10061 unknown error error is reported when connecting to the previously available MySQL database
A classic book on data analysis - "lean data analysis"
Realize information exchange between pages
postman接口测试和压力测试
Audio and video development learning notes (I)
自学软件测试要学哪些?
2021-06-22
Day03 测试用例知识点总结(下)
随机推荐
What should I learn about self-study software testing?
安装交叉编译器:EABI-4.3.3_EmbedSky_20100610.tar.bz2
软件测试常问面试题【三】
Day03 test case knowledge points summary (Part 2)
Extract a number from a string
How to do regression test
使用postman批量运行接口时判断运行结果成功与否的常用断言
Day01 软件测试基础总结
一句话需求怎么测
JMeter script generation is based on RAP2
Source lnsight 的使用(通过samba共享下阅读uboot)
charles常用功能
Day02 test case knowledge summary (Part 1)
Solve the essential matrix from the basic matrix to obtain the parameters of the camera (3D reconstruction task1-5)
类的继承性实验报告
Use case exercise 1
Pycharm 2019使用设置,让你用起来更便捷!
Notes on generation model (I): basic knowledge of probability
MySQL之多表关联删除/更新
LVM磁盘多分区扩容(fdisk,vgdata,lvdata,df,resize2fs,lvextend,partprobe)