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"||成功\n".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"||断言失败\n".encode("gbk"))

    except Exception,e:

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

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

 

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

            fp.writelines(test_result)

driver.quit()

 

结果:

D:\test>python test.py

gloryroad test搜索测试执行成功

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

超人搜索测试执行成功

 

 

Result.txt:

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

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

超人||电影||成功

 

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

data.txt:

gloryroad test||光荣之路1

摔跤爸爸||阿米尔1

超人||电影1

 

D:\test>python test.py

光荣之路1测试断言失败

阿米尔1测试断言失败

电影1测试断言失败

 

Result.txt:

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

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

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

 

转载于:https://www.cnblogs.com/xiaxiaoxu/p/9231506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值