Selenium数据驱动之EXCEL

来源:互联网 发布:女神联盟坐骑升阶数据 编辑:程序博客网 时间:2024/05/21 22:45

使用Selenium-Webdriver自动化测试163邮箱登录,登录的数据来自于EXCEL表格,即使用EXCEL表格进行数据驱动

表格中第一列的数据为帐号,第二列的数据为密码,如下:

sz001testing002sz002testing003sz003testing004sz004testing005sz005testing006sz006testing007sz007testing008sz008testing009sz009testing010sz010testing011sz011testing012sz012testing013sz013testing014sz014testing015sz015testing016sz016testing017sz017testing018sz018testing019

 

实现的代码如下:

require 'rubygems'
require 'selenium-webdriver'
require 'win32ole'
dr = Selenium::WebDriver.for :ie
url='http://email.163.com/'
dr.navigate.to url
sleep 3
excel = WIN32OLE.new("excel.application")
filepath="F:\\RUBY\\script\\Data.xls" #路径用两斜杠
workbook = excel.workbooks.open(filepath)
worksheet=workbook.worksheets(1)
#worksheet=workbook.worksheets("sheet name") 打开表名
#读取excel文件
row=1
while worksheet.range("a#{row}").value
   #选择帐号,把第一列的值做为帐号
   userNameIpt = dr.find_element(:id => 'userNameIpt')
   userNameIpt.send_keys worksheet.range("a#{row}").value.to_s
   #选择密码,将第二列值做为密码
   pwdInput= dr.find_element(:id => 'pwdInput')
   pwdInput.send_keys worksheet.range("b#{row}").value.to_s
   #单击登录按钮
   btnSubmit= dr.find_element(:id => 'btnSubmit')
   btnSubmit.click
   row+=1
 end