zoukankan      html  css  js  c++  java
  • python webdriver测试框架--数据驱动txt驱动

    数据驱动框架

      数据驱动核心思想:程序不变,数据变

      适合:程序本身容易实现,适合小功能测试里面

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

    testdata.txt

    gloryroad||光荣之路
    富爸爸||爸爸
    超人||动画

    ###  testdata.txt 保存格式是utf-8

    data_driver_by_txt.py

    from selenium import webdriver
    import time

    #打开data.txt文件
    with open("D:全天课学习代码第28次selenium(5)数据驱动测试框架data.txt","rb") as fp:
    data = fp.readlines()
    # for i in range(len(data)):
    # print(data[i].decode())
    # print(type(data[i].decode()))
    # print(data[i].decode().split("||")[1])
    # print(type(data[i].decode().split("||")[1]))

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


    with open(u"D:全天课学习代码第28次selenium(5)数据驱动测试框架data.txt","w") as fp:
    fp.writelines(testresult)

    driver.quit()


    执行结果:

    D:pythonpython.exe D:/data_driver_by_txt.py
    gloryroad搜索测试用例成功
    富爸爸搜索测试用例成功
    超人搜索测试用例成功

    result.txt

    gloryroad||光荣之路||成功
    富爸爸||爸爸||成功
    超人||动画||成功

    修改testdata.txt使得2条用例断言失败

    gloryroad||光荣之路
    富爸爸||爸爸555
    超人||动动

    执行结果:

    D:pythonpython.exe D:/data_driver_by_txt.py
    gloryroad搜索测试用例成功
    爸爸555测试断言失败
    动动漫测试断言失败

    result.txt

    gloryroad||光荣之路||成功
    富爸爸||爸爸555||断言失败
    超人||动动漫||断言失败



  • 相关阅读:
    移动应用安全开发指南(Android)--完结篇
    云存储密钥优化
    [安全分析报告]门磁报警系统破解猜想
    Web安全开发指南--文件系统
    C# 创建execl文件 并且填充数据
    asp.net js 获取服务器控件值
    js 日期天数相加减,格式化yyyy-MM-dd
    js获取当期日期累加天数
    ComboGrid 行内点击编辑内容
    ajax数据显示,使用js通用模板
  • 原文地址:https://www.cnblogs.com/ff-gaofeng/p/12669683.html
Copyright © 2011-2022 走看看