自动化测试之数据驱动和关键字驱动

数据驱动

数据和程序的分离,程序不变,数据变

from selenium import webdriver
import time
driver = webdriver.Firefox(executable_path = "d:\geckodriver")
driver.get("http://www.bing.com")
search_box = driver.find_element_by_xpath("//input[@id='sb_form_q']")
search_box.send_keys("大话西游之月光宝盒")
query_button = driver.find_element_by_xpath("//input[@id='sb_form_go']")
query_button.click()
time.sleep(5)

assert "周星驰" in driver.page_source
driver.quit()

请使用数据驱动的方式,实现访问网址、搜索框定位表达式、搜索词输入、搜索按钮定位表达式和断言值作为数据驱动的数据文件,然后执行测试脚本。
测试脚本:

from selenium import webdriver
import time
import os.path
import sys

def get_data_from_file(data_file_path,coding="utf-8"):
    if not os.path.exists(data_file_path):
        print("数据文件不存在!")
        sys.exit(0)
    test_data = []
    with open(data_file_path,'r',encoding=coding) as fp:
        for line in fp:
            if line.strip() == "":
                continue
            test_data.append(line.split("||"))
    return test_data    
test_data = get_data_from_file("f:\\urls.txt")
#print(test_data)

def test_step(url,searchbox_xpath,search_button_xpath,search_word,assert_word):
    driver = webdriver.Firefox(executable_path = "d:\geckodriver")
    driver.get(url)
    search_box = driver.find_element_by_xpath(searchbox_xpath)
    search_box.send_keys(search_word)
    query_button = driver.find_element_by_xpath(search_button_xpath)
    query_button.click()
    time.sleep(5)
    assert assert_word in driver.page_source
    driver.quit()

for data in test_data:
    url = data[0].strip()
    searchbox_xpath = data[1].strip()
    search_button_xpath = data[2].strip()
    search_word = data[3].strip()
    assert_word = data[4].strip()
    print(url,searchbox_xpath,search_button_xpath,search_word,assert_word)
    test_step(url,searchbox_xpath,search_button_xpath,search_word,assert_word)

数据文件:

http://www.sogou.com||//input[@id='query']||//input[@id='stb']||神奇动物在哪里||叶茨
http://www.bing.com||//*[@id='sb_form_q']||//*[@id='sb_form_go']||亚洲杯||国足

自动化测试之数据驱动和关键字驱动_第1张图片
基于txt的数据驱动测试框架

#encoding=utf-8
from selenium import webdriver
import time
import traceback
with open(u"data.txt") as fp:
    data = fp.readlines()


driver = webdriver.Chrome(executable_path="d:\\chromedriver")
#driver = webdriver.Ie(executable_path="d:\\IEDriverServer")
test_result = []
for i in range(len(data)):
    try:
        driver.get("http://www.baidu.com")
        driver.find_element_by_id("kw").send_keys(\
        data[i].split("||")[0].strip())
        driver.find_element_by_id("su").click()
        time.sleep()
        assert data[i].split("||")[1].strip() in driver.page_source
        test_result.append(data[i].strip()+u"||成功\n")
        print(data[i].split('||')[0].strip()+u"搜索测试执行成功")
    except AssertionError as e:
        print(data[i].split("||")[1].strip()+u"测试断言失败")
        test_result.append(data[i].strip()+u"||断言失败\n")
    except Exception as e:
        print(data[i].split("||")[1].strip()+u"测试执行失败")
        test_result.append(data[i].strip()+u"||异常失败\n")
        traceback.print_exc()

with open(u"result.txt","w") as fp:
    fp.writelines(test_result)
driver.quit()
gloryroad test||光荣之路
摔跤爸爸||阿米尔
超人||电影

自动化测试之数据驱动和关键字驱动_第2张图片
自动化测试之数据驱动和关键字驱动_第3张图片
自动化测试之数据驱动和关键字驱动_第4张图片

关键字驱动

一个单词,能够映射到python程序中的一个函数的自动调用。单词可以放在数据文件中。
测试脚本:

from selenium import webdriver
import time
import os.path
import sys

def get_data_from_file(data_file_data,coding="utf-8"):
    if not os.path.exists(data_file_data):
        print("数据文件不存在!")
        sys.exit(0)
    test_data = []
    with open(data_file_data,"r",encoding=coding) as fp:
        for line in fp:
            if line.strip() == "":
                continue
            test_data.append(line.split("||"))
    return test_data    
test_data = get_data_from_file("f:\\testcase.txt")
print(test_data)

driver = None
def open(browser_name):
    global driver
    if browser_name == "firefox":
        driver = webdriver.Firefox(executable_path = "d:\geckodriver")

def visit(url):
    global driver
    driver.get(url)

def input(xpath,content):
    global driver
    driver.find_element_by_xpath(xpath).send_keys(content)  

def click(xpath):
    global driver
    driver.find_element_by_xpath(xpath).click()  

def assert_f(assert_word):
    global driver
    assert assert_word in driver.page_source  

def close():
    global driver
    driver.quit()

for line in test_data:
    action = line[0]
    xpath = line[1]
    value = line[2].strip()        
    print(action,xpath,value)
    if xpath == "None" and value == "None":
        print("1",action+"()")
        exec(action+"()")
    elif xpath == "None" and value != "None":
        print("2",action+"(\""+value+"\")")
        exec(action+"(\""+value+"\")")
    elif xpath != "None" and value == "None":
        print("3",action+"(\""+xpath+"\")")
        exec(action+"(\""+xpath+"\")")
    else:
        print("4",action+"(\""+xpath+"\",\""+value+"\")")
        exec(action+"(\""+xpath+"\",\""+value+"\")")
        #action = "open"
        #value = "firefox"
        #open('firefox')

数据文件:

open||None||firefox
visit||None||http://www.sogou.com
input||//input[@id='query']||疯狂动物城
click||//input[@id='stb']||None
assert||None||古德温
close||None||None

自动化测试之数据驱动和关键字驱动_第5张图片

你可能感兴趣的:(自动化测试之数据驱动和关键字驱动)