Python and selenium_project articles_data-driven testing

What is the role of data-driven testing?
The purpose is: to allow the same script to use different test data. Let the test data and the script be completely separated. If multiple users are used to log in, and the test data to be logged in is ready, an automated script can be implemented.

The data-driven framework needs to master the basic operations of python on files, let's talk about this first

(1) General file operations
1.1 Text file reading and writing data

# coding=utf-8

# read() 一次性读取整个文件,读取整个文本
f = open('D:\\ui_date\\test.txt','r').read()
print(f)

# readline() 逐行读取,文件大用这个,读取文本中的前2个字符串
f = open('D:\\ui_date\\test_01.txt','r').readline(2)
print(f)

# readlines() 快速一次性读读取文本内容,并将结果存储在列表中。
txt = open('D:\\ui_date\\test_01.txt','r').readlines()
print(type(txt))
print(txt)

# 写内容
f = open('D:\\ui_date\\test_02.txt','w')
f.write('写入文件,w参数打开文件权限\n')
f.write('第一行末尾加换行符哦')
print(1)

1.2 Processing CSV files

import csv
# reader函数读取打开的文件,并赋值给变量c
c = csv.reader(open('D:\\ui_date\\test_03.csv','r'))
print(type(c))
# 打印第一列的内容
for cs in c:
    print(cs[0])
# print(c)
# 打印所有列的内容
for cs in c:
    for i in range(len(cs)):
        print(cs[i])

1.3 excel file

# coding=utf-8
from copy import copy
import xlrd  # xlrd全名:read excel
# noinspection PyUnresolvedReferences
import xlwt  # xlwt全名:write excel
# noinspection PyUnresolvedReferences
from Tools.demo import vector

# 读取表中的数据
xls = xlrd.open_workbook('D:\\ui_date\\test_04.xls')  # 读取excel的值
sheet2 = xls.sheet_by_index(0)  # 通过索引值获取sheet页
print(sheet2.row_values(1)[1])  # 查第一列第二行的单元格数据
# print(sheet2.nrows) # 表格总行数
# print(sheet2.ncols) # 表格总列

# 添加数据,首先>>>copy一张新表,给个变量
xls01 = copy(xlwt.Workbook('D:\\ui_date\\test_04.xls'))
# 给excel表添加sheet页
sheet3 = xls01.add_sheet('test_test')
# 给单元格添加数据
sheet3.write(3,3,"tfjiao")
sheet3.write(3,4,"selenium course")
# 把copy后修改的表放在D盘下
xls01.save("D:\\ui_date\\test_04_01.xls")
print('ok')

1.4 Json file operation
Json is a lightweight data exchange format, the data type is: string, strong readability, used to improve the network transmission rate, dictionary to json, dumps(), read and modify json data .

import json
# 将python中字典 转成 json字符串
json_data = {
    
    'j1':1,'j2':2,'j3':3}
print(type(json_data))
print(json_data)
# 数据类型从 字典类型数据 换成 字符串类型数据
json_1 = json.dumps(json_data)
print(type(json_1))
print(json_1)

# 跟上面相反,json字符串>>>python对象(字典)
json_data1 = '{"j1": 1, "j2": 2, "j3": 3}'
json_2 = json.loads(json_data1)
print(json_2)

Write data to json

import json
filename = open('D:\\ui_date\\www.json','w')
data = {
    
    'name':'kingsan','age':'30'}
# 将字典类型数据直接入json
json.dump(data,filename)

# 第2种,在f文件中写入字符串,先通过dumps抓换成字典
with filename as f:
	f.write(json.dumps(data))

1.5 Processing xml and yaml files

【优先级排后】

1.6 Folder operation
The identification, creation, and deletion of file and folder paths can be used to make some judgments to prevent data duplication and duplicate directories. Shell scripts can also determine whether you don't like it or python is fun.

# coding=utf-8
import os

# 打印当前执行脚本所在目录
print(os.getcwd())
# 如果路径存在,则返回true
print(os.path.exists('D:\\ui_date\\'))
# 判断当前路径是否有一个文件,有,则true
print(os.path.isfile('D:\\ui_date\\test_05.json'))
# 在当期目录下创建‘test’单个文件夹
os.mkdir('D:\\ui_date\\osmkdir')
# 创建多级目录
os.makedirs('D:\\ui_date\\osmkdir')
# 删除多级目录
os.removedirs('/PycharmProjects/Sstone/tt.png')

(2) Through the excel parameter, realize the separation of the parameter and the script in
2 points: define the function to read excel, and define the framework log.
Excel function code: functions.py under [python and selenium_projects_project actual combat, code optimization, project reconstruction] .
Logging module code: There is a problem, countless brain cells were killed by the logging module. From 4 PM >>> 22:43, I decided to abandon the functions.py, and just satisfy the simple and practical first:

import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")

Big guy link: logging

(3) Data-driven framework
mainly refers to unittest unit test framework, HTMLtestRunner, DDT framework

Output >>> write a piece of code, use pytest and send an email. For details
,
please refer to P217 requirements: (1) Use DDT+excel to implement simple repetitive testing, 220 (2) pytest framework, test cases, test suites are all executed, and emails are sent

The po framework is finished, and then finish this, it feels a bit messy

Guess you like

Origin blog.csdn.net/weixin_45451320/article/details/112691559