数据驱动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||异常失败

 

相关文章:

  • 2021-04-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-29
  • 2021-07-07
  • 2021-10-10
  • 2022-01-23
  • 2021-12-29
  • 2021-12-02
  • 2021-06-21
相关资源
相似解决方案