基于 Excel 参数化你的 Selenium2 测试代码
来源:互联网 发布:德国表现主义电影知乎 编辑:程序博客网 时间:2024/05/16 03:52
在测试过程中直接读取本地excl文件中的数据,一方面,将数据存放本地进行测试非常方便,不需要在代码中一个一个传参;另一方面,简化了代码,是你的测试代码看起来非常简洁易懂。
今天我们就如何使用 xlrd 模块来进行 python selenium2 + excel 自动化测试过程中的参数化进行演示说明,以解决大家在自动化测试实践过程中参数化的疑问。
一、安装xlrd
进入Python安装目录下的site-packages,在此目录下打开cmd,输入
pip install xlrd
二、xlrd 基本用法
以图片excl文件为例:
1.导入扩展包
import xlrd
2.打开 excel 文件,参数是你的excl文件路径
excel = xlrd.open_workbook(u'excelFile.xls')
3.获取工作表(以下三种方法任意一种都可以获取,大家选一种即可)
table = excel.sheets()[0]#通过索引顺序获取table = excel.sheet_by_index(0)#通过索引顺序获取table = excel.sheet_by_name('Sheet1')#通过名称获取
4.获取总行数和总列数
nrows = table.nrows#总行数ncols = table.ncols#总列数print nrows,ncols
执行结果:
3 2
5.分别获取第一行和第一列的值(注意这里的结果是第一行的值、第二列的值都会被打印出来)
table.row_values(0)#获取第一行的值table.col_values(0)#获取第一列的值
执行结果:
[u'\u5e8f\u53f7', u'\u503c'] #第一行的值“序号”,“值”[u'\u5e8f\u53f7', u'username', u'password']#第一列的值“序号”,“username”,“password”
6.i代表行号,j代表列号;获取第i行j列的值(以下两种方法都可以)
x = table.cell_value(0,0)#获取1行1列的值print x
或者
x = table.cell(0,0).valueprint x
执行结果:
序号
7.循环行遍历列表数据
hs = table.nrows #先获取行数for i in range(0,hs):#遍历打印所有行数据 print table.row_values(i)
结果:
[u'\u5e8f\u53f7', u'\u503c']#第一行[u'username', u'MTbaby']#第二行[u'password', u'Mtbaby-csdn']#第三行
三、示例展示
看一下如何利用 xlrd 来实现 python selenium2 自动化测试参数化。
我们将excl中的数据进行百度搜索,搜索值与期望值进行对比,从而验证测试结果。
将以下数据保存在test.xlsx文件中
代码实现
#-*- coding:utf-8 -*-from selenium import webdriverimport timeimport HTMLTestRunnerimport xlrdimport unittestclass BDtestdate: def __init__(self,path): self.path = path def load_data(self): #打开excel文件 excel = xlrd.open_workbook(self.path) # 获取第一个工作表 table = excel.sheet_by_name('Sheet1') #获取行数 nrows = table.nrows # 从第二行开始遍历数据,存入一个list中 test_list = [] for i in range(1,nrows): test_list.append(table.row_values(i)) # 返回读取的数据列表 return test_listclass BDtest(unittest.TestCase): u"""百度首页搜索测试用例""" def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(10) self.url = u"https://www.baidu.com/" self.path = u'test.xlsx' #这就是你的excel文件地址 def BDsearch(self): driver = self.driver print u"开始百度搜索" # 加载测试数据 test_excel = BDtestdate(self.path) data = test_excel.load_data() print data # 循环参数化 for d in date: #打开百度首页 driver.get(self.url) #验证标题 self.assertEqual(driver.title,u"百度一下,你就知道") time.sleep(2) driver.find_element_by_id('kw').clear() # 参数化 搜索词 driver.find_element_by_id('kw').send_keys(d[1]) time.sleep(2) driver.find_element_by_id('su').click() # 验证搜索结果标题 time.sleep(2) self.assertEqual(driver.title,d[2]) time.sleep(2) def tearDown(self): self.driver.quit()if __name__ == '__main__': testunit = unittest.TestSuite() testunit.addTest(BDtest('BDsearch')) # 定义报告输出路径 htmlPath = r"F:\pycharm-workspace\selenium\mt.html" #在当前目录或者你自己喜欢的目录新建一个html文件,用于展示你的测试报告 fp = open(htmlPath, "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"百度测试",description=u"测试用例结果") runner.run(testunit) fp.close()
html页面展示:
阅读全文
0 0
- 基于 Excel 参数化你的 Selenium2 测试代码
- 基于Selenium2和TestNG的自动化测试
- 基于Selenium2和TestNG的自动化测试
- 基于Selenium2和TestNG的自动化测试
- 基于Selenium2和TestNG的自动化测试
- 基于Selenium2和TestNG的自动化测试
- 基于Python selenium2的测试环境的搭建与安装
- 基于Selenium2与Python的自动化测试环境配置搭建
- 【自动化测试与持续集成】基于Selenium2和TestNG的自动化测试的实现
- Selenium2 中测试https协议的网站
- selenium2.0数据驱动测试的应用
- Selenium2自动化测试的元素定位
- 基于Selenium2与Python自动化测试环境搭建
- 基于Selenium2与Python自动化测试环境搭建
- 基于Selenium2与Python自动化测试环境搭建
- 基于Selenium2 与Python自动化测试环境搭建
- 3.Selenium2 自动化测试实战-基于Python语言-模块调用
- 9.Selenium2 自动化测试实战-基于Python语言-键盘事件
- Codeforces Round #430 (Div. 2) A. Kirill And The Game
- 交互设计新手路要怎么走
- action和action之间传数据
- SQOOP from Oracle Connection reset error
- Java线程池(ThreadPoolFactory)构造参数总结
- 基于 Excel 参数化你的 Selenium2 测试代码
- synchronized同步代码块
- Codeforces-809C Find a car(分治)
- mac 下终端访问文件出现“Permission Denied”解决方案
- [LintCode]96.链表划分
- POJ-1742:Coins(多重背包二进制求法)
- Eclipse上Maven环境配置使用
- AESTool
- Spring自定义Listener(监听器)的使用