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

testdata.xlsx

脚本:

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

原文地址:https://www.cnblogs.com/ff-gaofeng/p/12663778.html

时间: 2024-08-01 13:23:18

python webdriver 测试框架--数据驱动之Excel驱动的相关文章

python webdriver 测试框架-数据驱动DDT的例子

先在cmd环境 运行 pip install ddt 安装数据驱动ddt模块  脚本: #encoding=utf-8 from selenium import webdriver import unittest,time import logging,traceback import ddt from selenium.common.exceptions import NoSuchElementException #初始化日志对象 logging.basicConfig( #日志级别 leve

python nose测试框架全面介绍六--框架函数别名

之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.package中写在__init__.py中的函数 setup_package 用setup, setUp, or setUpPackage也可以 teardown_package 用teardown, tearDown, or tearDownPackage也可以 2.函数形式组成的用例 setup_

python nose测试框架全面介绍七--日志相关

引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的报告只有错误提示,没有日志,查看nose的官网,nose默认支持将日志显示的,如下: 脚本如下: #coding:utf-8 ''' Created on 2016年6月22日 @author: huzq ''' import logging from test_case import new fr

python nose测试框架全面介绍十---用例的跳过

又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的SkipTest典型应用  应用一: ''' @auth:hu ''' from nose.plugins.skip import SkipTest @attr(mode=1) def test_learn_1(): raise SkipTest 但这种SkipTest在实际的日志中没有显示Skip关

python nose测试框架全面介绍一

一.简介      nose 是python自带框架unttest的扩展,使测试更简单高效:nose是一个开源的项目,可以在官网上下载源码 1.快速安装 有以下几中安装方式: easy_install nose pip install nose 对于python怎么安装easy_install或pip工具,这里不介绍,网上很多教程. 如果你没有安装easy_install或pip工具,你可以去官网上下载安装包,然后解压,并cd进放解压的目录,然后输入以下代码: python setup.py i

python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这里介绍pytest生成的报告怎么带有截图,这在web自动化测试非常有用. 需求是测试用例错误就截图,方法如下: 我们要新建一个关于截图的插件文件conftest.py,注意,文件名不能变,因为pytest-html会自动找这个自己写的插件,内容如下: from selenium import web

python nose测试框架全面介绍二

二.基本使用 nosetest脚本的使用(在安装完nose之后) nosetests [options] [(optional) test files or directories] 我们可以使用配置文件,将需要运行的参数放入,配置文件的文件名为nose.cfg中,是标准的配置文件格式,如下: [nosetests] verbosity=3 with-doctest=1 有部分参数可以不使用或禁用,你可以将配置放入配置文件中,但必须在系统中建立一个环境变量,名字为 NOSE_IGNORE_CON

Python unittest测试框架1(单线程顺序执行)

为了解决之前unittest测试中文件下的所有的测试用例全部执行并生成测试报告的问题. 方案如下: 目录结构 │  testlibmain.py │  ├─public │      HTMLTestRunner.py │      ├─result ├─test_1 │      test1.py │      test2.py │      ├─test_2 │      test1.py │      test2.py │      └─test_3         test1.py   

python pytest测试框架介绍三

之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_function import pytest @pytest.fixture(scope='module') def resource_a_setup(request): print('\nresources_a_setup()') def resource_a_teardown(): print('