python webdriver 测试框架--数据驱动之Excel驱动

jling 2020-04-09

testdata.xlsx

python webdriver 测试框架--数据驱动之Excel驱动

脚本:

from selenium import webdriverimport timeimport datetimefrom openpyxl import *wb=load_workbook(r"D:\TestFrame\TestDate\testdata.xlsx")#获取第一个sheetws = wb.active#打印最大的行号print("最大行号是:",ws.max_row)#excel行是从第1行开始的,所以我们要从第2行开始迭代遍历#使用切片的时候,必须有结束行的索引号,不能写成[1:]#列号是从0开始,列取出来是个元组#打开浏览器driver = webdriver.Chrome("e:\\chromedriver.exe")for row in ws[2:ws.max_row]:    print(row[1])    print(row[2])    try:        driver.get("http://www.baidu.com")        driver.find_element_by_id("kw").send_keys(row[1].value)        driver.find_element_by_id("su").click()        time.sleep(3)        assert row[2].value in driver.page_source        row[3].value = time.strftime("%Y-%m-%d %H:%M:%S")        row[4].value = "断言成功"    except AssertionError as e:        row[3].value =time.strftime("%Y-%m-%d %H:%M:%S")        row[4].value = "断言失败"    except AssertionError as e:        row[3].value = time.strftime("%Y-%m-%d %H:%M:%S")        row[4].value = "出现异常"driver.quit()wb.save(r"D:\TestFrame\TestDate\testdata.xlsx")   #注意是覆盖不是更新执行结果:

D:\python\python.exe D:/TestFrame/ff.py
最大行号是: 4
<Cell ‘Sheet1‘.B2>
<Cell ‘Sheet1‘.C2>
<Cell ‘Sheet1‘.B3>
<Cell ‘Sheet1‘.C3>
<Cell ‘Sheet1‘.B4>
<Cell ‘Sheet1‘.C4>

Process finished with exit code 0

python webdriver 测试框架--数据驱动之Excel驱动

相关推荐