**混合驱动:**数据驱动+关键字驱动结合,实现了数据与程序的分离,测试步骤与程序的分离。简单来说,测试用例的每个步骤都可以封装为函数。这样测试人员只需配置好测试数据+测试步骤数据即可
**测试场景:**网易163登录。用3组数据登录163邮箱,根据测试数据里的assert_word、取到网页源码 二者断言返回测试结果
代码:
test_data.txt文件内容:
{“user_name”:“xiaoqiong_chen”,“password”:“8888”,“assert_word”:“请输入账号”}
{“user_name”:“xiaoqiong_chen”,“password”:“123”,“assert_word”:“网易邮箱6.0版”}
{“user_name”:“xiaoqiong_chen”,“password”:“xiaoqiong_chen”,“assert_word”:“请输入密码”}
test_step_data.txt文件内容:
open_browser||chrome
visit||https://mail.163.com/
sleep||3
swith_to_iframe||//div[@id=“loginDiv”]/iframe
input_data||//*[@name=“email”]||
u
s
e
r
n
a
m
e
i
n
p
u
t
d
a
t
a
∣
∣
/
/
∗
[
@
n
a
m
e
=
"
p
a
s
s
w
o
r
d
"
]
∣
∣
{user_name} input_data||//*[@name="password"]||
usernameinputdata∣∣//∗[@name="password"]∣∣{password}
click||dologin
swith_out_iframe
assert_word||${assert_word}
quit
from selenium import webdriver
import os
import time
import json
import re
driver=""
def open_browser(browser_name):#启动浏览器
global driver
if "ie" in browser_name.lower():
driver=webdriver.ie()
elif "firefox" in browser_name.lower():
driver=webdriver.Firefox()
elif "chrome" in browser_name.lower():
driver=webdriver.Chrome()
def visit(url):#访问目标页
global driver
driver.set_page_load_timeout(5)
driver.get(url)#下载网页
def sleep(times):#等待
time.sleep(int(times))
def swith_to_iframe(element):#切换到iframe
global driver
first_iframe=driver.find_element_by_xpath(element)
driver.switch_to.frame(first_iframe)
def input_data(element,value):#文本框输入值
driver.find_element_by_xpath(element).send_keys(value)
def click(element):#触发点击事件
global driver
driver.find_element_by_id(element).click()
def swith_out_iframe():#切出iframe
global driver
driver.switch_to.default_content()
def assert_word(key_word):#断言关键字
global driver
try:
assert key_word in driver.page_source
print("finish assert!")
except AssertionError:
print("网页中没有出现关键词%s" % key_word)
except Exception as e:
print("出现其他异常!", e)
else:
print("断言成功!")
def quit(): # 关闭浏览器
global driver
driver.quit()
def read_test_step_data(path):#读取测试步骤数据
test_step_data = []
if os.path.exists(path):
with open(path,"r",encoding="utf-8") as fp:
data=fp.readlines()
for line in data:
test_step_data.append(line.strip().split("||"))
return test_step_data
else:
print("路径错误")
def read_test_data(path):#读取测试数据
test_data=[]
if os.path.exists(path):
with open(path,"r",encoding="utf-8") as fp:
data=fp.readlines()
for line in data:
test_data.append(json.loads(line.strip()))
return test_data
else:
print("路径错误")
def test_login_main():
test_data=read_test_data(r"E:\pythonProject\SelenuimDemo\test_data.txt")
test_step_data=read_test_step_data(r"E:\pythonProject\SelenuimDemo\test_step_data.txt")
print(test_data)
print(test_step_data)
for testdata in test_data:
for teststepdata in test_step_data:
if len(teststepdata)==1:
command=teststepdata[0]+"()"
elif len(teststepdata)==2:
keyword=re.findall(r'\$\{(.*)\}',teststepdata[1])
if len(keyword)>0:
command=teststepdata[0]+"('"+testdata["".join(keyword)]+"')"
else:
command=teststepdata[0]+"('"+teststepdata[1]+"')"
elif len(teststepdata)==3:
keyword=re.search(r'\$\{(.*)\}',teststepdata[2]).group(1)
command="%s('%s','%s')" % (teststepdata[0],teststepdata[1],testdata["".join(keyword)])
print(command)
eval(command)#用eval执行函数调用
if __name__=="__main__":
print(test_login_main())