python webdriver 测试框架-数据驱动txt文件驱动,带报告的例子

数据驱动txt文件驱动的方式,带报告

data.txt:

gloryroad test||光荣之路

摔跤爸爸||阿米尔

超人||电影

data_driven_by_txt_file.py:

#encoding=utf-8

from selenium import webdriver

import time

with open(u"e:\数据驱动\data.txt") as fp:

    data=fp.readlines()

driver=webdriver.Ie(executable_path="e:\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().decode("gbk"))

        driver.find_element_by_id("su").click()

        time.sleep(3)

        assert data[i].split('||')[1].strip().decode('gbk')

        in driver.page_source

        test_result.append(data[i].strip()+u"||成功 ".encode("gbk"))

        print data[i].split('||')[0].strip().decode('gbk')+u"搜索测试执行成功"

    except AssertionError,e:

        print data[i].split('||')[1].strip().decode('gbk')+u"测试断言失败"

        test_result.append(data[i].strip()+u"||断言失败 ".encode("gbk"))

    except Exception,e:

        print data[i].split('||')[1].strip().decode('gbk')+u"测试执行失败"

        test_result.append(data[i].strip()+u"||异常失败 ".encode("gbk"))

with open(u"e:\数据驱动\result.txt","w") as fp:

            fp.writelines(test_result)

driver.quit()

结果:

D: est>python test.py

gloryroad test搜索测试执行成功

摔跤爸爸搜索测试执行成功

超人搜索测试执行成功

Result.txt:

gloryroad test||光荣之路||成功

摔跤爸爸||阿米尔||成功

超人||电影||成功

修改data.txt使断言失败的结果:

data.txt:

gloryroad test||光荣之路1

摔跤爸爸||阿米尔1

超人||电影1

D: est>python test.py

光荣之路1测试断言失败

阿米尔1测试断言失败

电影1测试断言失败

Result.txt:

gloryroad test||光荣之路1||异常失败

摔跤爸爸||阿米尔1||异常失败

超人||电影1||异常失败

 
原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9231506.html