1. 数据驱动:测试程序(步骤)不变,测试数据是变化的,要使用不同的数据进行测试。数据与程序的分离,提示自动化测试的的可维护性。测试步骤代码如下:
from selenium import webdriver
import time
import sys
#读取txt文件
def get_data(file_path):
with open(file_path, encoding='utf-8-sig') as fp:
datas = fp.readlines()
#去掉列表数据中的“\n”
i = 0
for temp in datas:
datas[i] = temp.strip()
i += 1
return datas
#获取数据文件中的search_word和expected_word
datas = get_data("data.txt")
#没获取一组数据后,都执行下面的测试步骤
for temp in datas:
search_word,expected_word = temp.split("||")
try:
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
input_ele= driver.find_element_by_id('kw')
input_ele.send_keys(search_word)
button_ele = driver.find_element_by_id("su")
button_ele.click()
time.sleep(3)
assert expected_word in driver.page_source
driver.quit()
except AssertionError:
print("没有找到断言内容%s"%expected_word)
driver.quit()
except Exception as e:
print("出现未知错误")
driver.quit()
data.txt文档与代码文档在同一个目录,文档内容如下:
天天基金||天天基金网
东方财富||12121212
2. 关键字驱动:将测试的步骤代码进行封装,减少重复代码的编写。直接通过txt文档写测试用例
代码如下:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import os.path
import time
driver = None
def open_browser(brower_name):
#driver前添加global,可以对 driver进行修改
global driver
if "chrome" in brower_name:
driver = webdriver.Chrome()
else:
driver = webdriver.FireFox()
def visit(url):
driver.get(url)
def input_content(xpath_ele, value):
driver.find_element_by_xpath(xpath_ele).send_keys(value)
def clear_value(xpath_ele):
driver.find_element_by_xpath(xpath_ele).clear()
def click_ele(xpath_ele):
driver.find_element_by_xpath(xpath_ele).click()
def sleep(seconds):
time.sleep(int(seconds))
#显示等待
#WebDriverWait(driver, int(seconds)).until(lambda: driver.find_element_by_link_text(expect_word))
def asser(expect_word):
assert expect_word in driver.page_source
def quit():
driver.quit()
def read_date(file_path):
#读取文件数据
date_list = []
with open(file_path, encoding = "utf-8-sig") as fp:
date = fp.readlines()
for temp in date:
date_list.append(temp.strip())
return date_list
flag = True
test_date = read_date("guanjian_data.txt")
#读出的文件拼接成命令代码
for temp in test_date:
if temp.count("||") == 0:
command = temp + "()"
elif temp.count("||") == 1:
func_name, value = temp.split("||")
command = "%s(\"%s\")" %(func_name, value)
elif temp.count("||") == 2:
func_name, xpath_ele, expect_word = temp.split("||")
command = "%s(\"%s\",\"%s\")" %(func_name, xpath_ele, expect_word)
try:
#通过eval()函数执行command的代码
eval(command)
except:
flag =False
print("%s 执行失败"%command)
else:
print("%s 测试用例执行成功"%command)
if flag:
print("整个测试用例执行成功")
else:
print("整个测试用例执行失败")
data.txt文档与代码文档在同一个目录,文档内容如下:
open_browser||chrome
visit||https://www.baidu.com/
input_content||//input[@id='kw']||天天基金网
click_ele||//input[@id='su']
sleep||3
asser||天天基金
quit
3. 混合驱动:数据驱动+关键字驱动。关键字驱动测试用例中的数据,从另一个文档中读取,这样当测试用例中的数据变化时,不需要重复写测试步骤,只需要将原来测试步骤中的数据更换即可,代码如下:
from selenium import webdriver
import time, os.path, sys, re
driver = None
def open_browser(brower_name):
#driver前添加global,可以对 driver进行修改
global driver
if "chrome" in brower_name:
driver = webdriver.Chrome()
else:
driver = webdriver.FireFox()
def visit(url):
driver.get(url)
def input_content(xpath_ele, value):
driver.find_element_by_xpath(xpath_ele).send_keys(value)
def clear_value(xpath_ele):
driver.find_element_by_xpath(xpath_ele).clear()
def click_ele(xpath_ele):
driver.find_element_by_xpath(xpath_ele).click()
def sleep(seconds):
time.sleep(int(seconds))
def asser(expect_word):
assert expect_word in driver.page_source
def quit():
driver.quit()
#读取测试步骤
def read_steps_file(test_steps_path):
test_steps = []
#判断test_steps_path路径是否存在
if not os.path.exists(test_steps_path):
print("%s 数据文件路径错误,请重新输入"%test_steps_path)
sys.exit(0)
with open(test_steps_path, encoding = "utf-8-sig") as fp:
date = fp.readlines()
fp.close()
for line in date:
test_steps.append(line.strip())
return test_steps
#读取测试数据
def read_data_file(test_data_path):
test_data = []
#判断test_steps_path路径是否存在
if not os.path.exists(test_data_path):
print("%s 数据文件路径错误,请重新输入"%test_data_path)
sys.exit(0)
with open(test_data_path, encoding = "utf-8-sig") as fp:
date = fp.readlines()
fp.close()
for line in date:
test_data.append(line.strip())
return test_data
#将每行测试步骤拼接成可执行代码并通过eval()函数执行
flag = True
def create_command(fileLine):
if fileLine.count("||") == 0:
command = fileLine + "()"
elif fileLine.count("||") == 1:
func_name, value = fileLine.split("||")
command = "%s(\"%s\")" %(func_name, value)
elif fileLine.count("||") == 2:
func_name, xpath_ele, expect_word = fileLine.split("||")
command = "%s(\"%s\",\"%s\")" %(func_name, xpath_ele, expect_word)
try:
#通过eval()函数执行command的代码
eval(command)
except:
global flag
flag = False
print("%s 执行失败"%command)
return flag
#有几行测试数据,我们就需要执行几次测试步骤
#遍历一下测试数据,每次取出一个数据来运行测试步骤
def test_main(test_data_path, test_steps_path):
test_datas = read_data_file(test_data_path)
test_steps = read_steps_file(test_steps_path)
#遍历测试数据
for test_data in test_datas:
#把test_data的json串转换成字典类型
test_data = eval(test_data)
#读取测试步骤
for test_step in test_steps:
'''
1. 先把{{xxx}}里面的xxx找出来,正则:re.search(r"{{(.*?)}}",s).group(1)
2. 用test_data['xxx']替换该值
'''
if "{{" in test_step:
key = re.search(r"{{(.*?)}}", test_step).group(1)
test_step = re.sub(r"{{%s}}"%key, test_data[key], test_step)
#拼接代码并执行
result = create_command(test_step)
if result:
print("%s整个测试用例执行成功"%test_data[key])
else:
print("%s整个测试用例执行失败"%test_data[key])
test_main("test_data.txt", "test_steps.txt")
测试步骤文档"test_steps.txt"如下:
open_browser||chrome
visit||https://www.baidu.com/
input_content||//input[@id='kw']||{{search_words}}
click_ele||//input[@id='su']
sleep||3
asser||{{expect_words}}
quit
测试数据文档"test_datas.txt"内容如下:
{"search_words":"天天基金网", "expect_words":"天天基金"}
{"search_words":"东方财富网", "expect_words":"东方财富"}
{"search_words":"51.job", "expect_words":"电影"}
【PS】:文档参照某老师视频编写