基于Ruby的watir-webdriver自动化测试方案与实施(三)
2014-12-15 11:33
495 查看
接着基于Ruby的watir-webdriver自动化测试方案与实施(二)
/article/5055976.html
继续......
编写脚本首先要学会捕获元素,接下来就要学习页面元素的捕获。
?ie.button(:name=>"",:id=>"",:index=>n,:type=>"").click
?ie.button(:name=>"",:id=>"",:index=>n,:type=>"").doclick
输入框
?ie.text_field(:name=>"").set"变量"
?ie.text_field(:name=>"").value取text_field值不是用text而是value!
下拉框
?ie.select_list(:name=>"").select"下拉框值"
?ie.select_list(:name=>"").select"#1"#表示第一项内容
?ie.select_list(:name=>"").rand_select
?ie.select_list(:name=>"").getSelectedItems|getAllContents->返回Array
单选框
?ie.radio(:id=>"",:name=>"",:index=>n).set(选中当前radio)
?ie.radio(:id=>"",:name=>"",:index=>n).clear(取消选中当前radio)
ie.div(:class=>"iradio_minimal-bluechecked").radios[1]
复选框
?ie.check_box(:id=>"",:name=>"",:index=>n).set(true|false)(true表示选中,false表示不选中)
?ie.check_box(:id=>"",:name=>"",:index=>n).clear(取消选中当前checkbox)
链接
?ie.link(:text=>"").click/doclick
?ie.link(:text=>"").href(返回当前link指向的链接)
cell(TD标签,用时一般需要先找到上层控件如table、div等)
?ie.table(:class=>"",:index=>n).cell(:class=>"",:index=>n).text
?ie.table(:index=>n).rows行列.text(行、列从1开始)
?ie.div(:class=>"",:index=>n).cell(:class=>"",:index=>n).text
span
?ie.table(:id=>"").span(:class=>"").text
弹出框
?ie.popupwin.get_static_text(返回当前提示框的文本)
?ie.popupwin.button(:name=>"确定").click/doclick(前一个点击按钮必须用doclick)
?ie.file_dialog(:index=>1/2).set_file(file_path_download,true)(保存文件的弹出窗口)
alert
图片
?ie.image(:src=>/word3a_nor.gif/).click/doclick
back
后退
ie.back
forward
前进
ie.forward
refresh
刷新页面
ie.refresh
在Watir-WebDriver中处理frame是非常简单的,就跟处理其他页面元素一样:
可以在代码里告诉浏览器自动的将文件下载到指定目录,然后在测试用例中访问该目录进行验证。
关于Firefox的所有配置项可以通过在地址栏中输入'about:config'进行查看。
Ifyouwanttoknowawaytoworkoutthefiletypes(eg.application/pdf)thenyoucanreadthefollowingblogpostforanstepbystepguide.如果你想知道如何处理特定类型的文件,请阅读这篇博文。
Watir-WebDriver内建了处理这些对话框的方法,并且可以返回对话框中显示的内容。首先,加载这个扩展:
ViewCode
可选方法
如果你使用上面的方法时遇到了麻烦,你可以自行覆盖JavaScriptfunctions,这样一来原来应该显示的对话框就可以在触发时不显示了。
#使alert方法返回空
browser.execute_script("window.alert=function(){}")
#使prompt返回特定的字符串,用来模拟用户的输入
browser.execute_script("window.prompt=function(){return'myname'}")
#使prompt方法返回null用来模拟用户点击了Cancel(取消)按钮
browser.execute_script("window.prompt=function(){returnnull}")
#使confirm方法返回true用来模拟用户点击了OK(确定)按钮
browser.execute_script("window.confirm=function(){returntrue}")
#使confirm方法返回false用来模拟用户点击了Cancel(取消)按钮
browser.execute_script("window.confirm=function(){returnfalse}")
其统计结果如下:
LoadTime:3.701seconds.
Thegreatthingaboutthisisitgivesyouascreenshotoftheentirepage,notjustabovethefold.截图功能最棒的地方在于它能捕获到整个页面,而不是屏幕上显示的那部分。
如果你正在使用Cucumber,那么你可以简单的将下面的代码添加到env.rb文件中,这样你可以在html的报告中插入截图:
也可以这样做:
你还可以修改click方法的行为,使得点击可以配合按键一起进行:
支持的按键键名列表如下:
ViewCode
定位编辑器所在的iFrame,然后使用.send_keys方法(缺点是浏览器必须在前台运行)
在浏览器上执行javascript,通过js脚本去设置编辑器的值
CKEditor
继续......
编写脚本首先要学会捕获元素,接下来就要学习页面元素的捕获。
页面元素
attribute_value
获取当前控件的属性Value=ie.link(:id=>'xxx’).attribute_value("href")
rand_select
随机选择selectlist中的某一项ie.select_list(:name=>’’).rand_select
popupwin
点击弹窗上的‘确定’按钮ie.popupwin.button(:name=>"确定").click
sikuli_image
点击图片控件ie.sikuli_image(:image=>"1.png").click ie.sikuli_image(:image=>"1.png;2.png").click#可以指定多张图片来识别
double_click
双击事件ie.sikuli_image(:image=>"1.png").double_click
right_click
右击事件exist?
判断用户元素是否存在edit=ie.text_field(:name,"username") ifedit.exist?() #Thehighlighted edit.flash ie.text_field(:name,"password").set(pwd) ie.button(:class,"x-login-submit").click end end
TextFields
require'watir-webdriver' b=Watir::Browser.start'bit.ly/watir-webdriver-demo' t=b.text_field:id=>'entry_0' t.exists? t.set'yourname' t.value
SelectLists–Combos
require'watir-webdriver' b=Watir::Browser.start'bit.ly/watir-webdriver-demo' s=b.select_list:id=>'entry_1' s.select'Ruby' s.selected_options #返回选中的选项 putsb.select(:name,'sex').selected_options
Radios
require'watir-webdriver'
b=Watir::Browser.start'bit.ly/watir-webdriver-demo'
r=b.label(:text=>'Whatisruby?').parent.radio:value=>'Agem'
r.exists?
r.set
r.set?
Checkboxes
require'watir-webdriver'
b=Watir::Browser.start'bit.ly/watir-webdriver-demo'
c=b.label(:text=>'Whatversionsofruby?').parent.checkbox:value=>'1.9.2'
c.exists?
c.set
c.set?
#选择checkbox
b.checkbox(:name,'check_me').settrue
#清除选择
b.checkbox(:name,'check_me').setfalse
Buttons
require'watir-webdriver'
b=Watir::Browser.start'bit.ly/watir-webdriver-demo'
btn=b.button:value,'Submit'
btn.exists?
btn.click
Links
require'watir-webdriver'
b=Watir::Browser.start'bit.ly/watir-webdriver-demo'
l=b.link:text=>'GoogleDocs'
l.exists?
l.click
Divs&Spans
require'watir-webdriver'
b=Watir::Browser.start'bit.ly/watir-webdriver-demo'
d=b.div:class=>'ss-form-descss-no-ignore-whitespace'
d.exists?
d.text
s=b.span:class=>'ss-powered-by'
s.exists?
s.text
实例
按钮?ie.button(:name=>"",:id=>"",:index=>n,:type=>"").click
?ie.button(:name=>"",:id=>"",:index=>n,:type=>"").doclick
输入框
?ie.text_field(:name=>"").set"变量"
?ie.text_field(:name=>"").value取text_field值不是用text而是value!
下拉框
?ie.select_list(:name=>"").select"下拉框值"
?ie.select_list(:name=>"").select"#1"#表示第一项内容
?ie.select_list(:name=>"").rand_select
?ie.select_list(:name=>"").getSelectedItems|getAllContents->返回Array
单选框
?ie.radio(:id=>"",:name=>"",:index=>n).set(选中当前radio)
?ie.radio(:id=>"",:name=>"",:index=>n).clear(取消选中当前radio)
ie.div(:class=>"iradio_minimal-bluechecked").radios[1]
复选框
?ie.check_box(:id=>"",:name=>"",:index=>n).set(true|false)(true表示选中,false表示不选中)
?ie.check_box(:id=>"",:name=>"",:index=>n).clear(取消选中当前checkbox)
链接
?ie.link(:text=>"").click/doclick
?ie.link(:text=>"").href(返回当前link指向的链接)
cell(TD标签,用时一般需要先找到上层控件如table、div等)
?ie.table(:class=>"",:index=>n).cell(:class=>"",:index=>n).text
?ie.table(:index=>n).rows行列.text(行、列从1开始)
?ie.div(:class=>"",:index=>n).cell(:class=>"",:index=>n).text
span
?ie.table(:id=>"").span(:class=>"").text
弹出框
?ie.popupwin.get_static_text(返回当前提示框的文本)
?ie.popupwin.button(:name=>"确定").click/doclick(前一个点击按钮必须用doclick)
?ie.file_dialog(:index=>1/2).set_file(file_path_download,true)(保存文件的弹出窗口)
alert
ifb.alert.exists?
puts"ok"
else
puts"no"
end
putsb.alert.text
#b.alert.ok
b.alert.close
b.close
图片
?ie.image(:src=>/word3a_nor.gif/).click/doclick
back
后退
ie.back
forward
前进
ie.forward
refresh
刷新页面
ie.refresh
在Watir-WebDriver中处理frame是非常简单的,就跟处理其他页面元素一样:
b.frame(:id=>"content_ifr").send_keys"helloworld"
文件的下载
最简单最好的处理文件下载对话框的方式就是完全的避免对话框弹出。可以在代码里告诉浏览器自动的将文件下载到指定目录,然后在测试用例中访问该目录进行验证。
Firefox
download_directory="#{Dir.pwd}/downloads"
download_directory.gsub!("/","\\")ifSelenium::WebDriver::Platform.windows?
profile=Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList']=2#customlocation
profile['browser.download.dir']=download_directory
profile['browser.helperApps.neverAsk.saveToDisk']="text/csv,application/pdf"
b=Watir::Browser.new:firefox,:profile=>profile
关于Firefox的所有配置项可以通过在地址栏中输入'about:config'进行查看。
Ifyouwanttoknowawaytoworkoutthefiletypes(eg.application/pdf)thenyoucanreadthefollowingblogpostforanstepbystepguide.如果你想知道如何处理特定类型的文件,请阅读这篇博文。
Chrome
download_directory="#{Dir.pwd}/downloads"
download_directory.gsub!("/","\\")ifSelenium::WebDriver::Platform.windows?
profile=Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download']=false
profile['download.default_directory']=download_directory
b=Watir::Browser.new:chrome,:profile=>profile
浏览器新窗口
当一个新的浏览器窗口打开时,你可以使用'use'方法来处理这个新窗口。browser.window(:title=>"annoyingpopup").usedo
browser.button(:id=>"close").click
end
JS弹出框
在web应用中,JavaScript对话框是十分常见的。Watir-WebDriver内建了处理这些对话框的方法,并且可以返回对话框中显示的内容。首先,加载这个扩展:
ViewCode
可选方法
如果你使用上面的方法时遇到了麻烦,你可以自行覆盖JavaScriptfunctions,这样一来原来应该显示的对话框就可以在触发时不显示了。
#使alert方法返回空
browser.execute_script("window.alert=function(){}")
#使prompt返回特定的字符串,用来模拟用户的输入
browser.execute_script("window.prompt=function(){return'myname'}")
#使prompt方法返回null用来模拟用户点击了Cancel(取消)按钮
browser.execute_script("window.prompt=function(){returnnull}")
#使confirm方法返回true用来模拟用户点击了OK(确定)按钮
browser.execute_script("window.confirm=function(){returntrue}")
#使confirm方法返回false用来模拟用户点击了Cancel(取消)按钮
browser.execute_script("window.confirm=function(){returnfalse}")
页面性能
Watir-WebDriver-Performancegem提供在访问页面的同时进行页面性能度量的功能,其使用的是W3C页面性能度量指标。这是一个完美的捕获响应性能指标的解决方案,其使用方法非常直观和简单,不过目前只支持Chrome和IE9l浏览器。require'watir-webdriver'
require'watir-webdriver-performance'
b=Watir::Browser.new:chrome
10.timesdo
b.goto'http://17test.info'
load_secs=b.performance.summary[:response_time]/1000
puts"LoadTime:#{load_secs}seconds."
end
其统计结果如下:
LoadTime:3.701seconds.
截屏
Watir-WebDriver内建的截图功能很赞也很好用。browser.driver.save_screenshot'screenshot.png'
Thegreatthingaboutthisisitgivesyouascreenshotoftheentirepage,notjustabovethefold.截图功能最棒的地方在于它能捕获到整个页面,而不是屏幕上显示的那部分。
如果你正在使用Cucumber,那么你可以简单的将下面的代码添加到env.rb文件中,这样你可以在html的报告中插入截图:
Afterdo|scenario|
browser.driver.save_screenshot'screenshot.png'
embed'screenshot.png','image/png'
end
模拟特殊按键
使用.send_keys方法可以模拟特殊的键盘按键(比如shift),其参数是你所需要模拟的按键的符号表示(symbolic)。b.send_keys:enter
也可以这样做:
b.element.send_keys[:control,'a'],:backspace
你还可以修改click方法的行为,使得点击可以配合按键一起进行:
b.element.click(:shift,:control)
支持的按键键名列表如下:
ViewCode
富文本编辑器
有两种方法可以通过Watir-WebDriver向所见即所得编辑器(应该指的是富文本编辑器)中输入文字:定位编辑器所在的iFrame,然后使用.send_keys方法(缺点是浏览器必须在前台运行)
在浏览器上执行javascript,通过js脚本去设置编辑器的值
CKEditor
require'watir-webdriver'
b=Watir::Browser.new:firefox
b.goto'http://ckeditor.com/demo'
b.execute_script("CKEDITOR.instances['editor1'].setData('helloworld');")
b.frame(:title=>'Richtexteditor,editor1,pressALT0forhelp.').send_keys'helloworldagain'
TinyMCEEditor
require'watir-webdriver'
b=Watir::Browser.new
b.goto'http://tinymce.moxiecode.com/tryit/full.php'
b.execute_script("tinyMCE.get('content').execCommand('mceSetContent',false,'helloworld');")
b.frame(:id=>"content_ifr").send_keys'helloworldagain'
QA
相关文章推荐
- 基于Ruby的watir-webdriver自动化测试方案与实施(二)
- 基于Ruby的watir-webdriver自动化测试方案与实施(四)
- 基于Ruby的watir-webdriver自动化测试方案与实施(一)
- 基于Ruby的watir-webdriver自动化测试方案与实施(五)
- 基于Ruby的Watir-WebDriver自动化测试方案
- ruby+selenium-webdriver一步一步完成自动化测试-----一个测试用例
- ruby+selenium-webdriver一步一步进行自动化测试----学习总结陈述
- Windows环境搭建Web自己主动化測试框架Watir(基于Ruby)
- ruby+selenium-webdriver一步一步进行自动化测试------面向对象编程
- 基于ruby的watir自动化测试 笔记二
- JUnit框架学习----基于webdriver自动化测试脚本的完成
- ruby+selenium-webdriver一步一步完成自动化测试-----多个测试用例
- jruby+watir-webdriver+cucumber自动化测试环境配置
- ruby+selenium-webdriver一步一步完成自动化测试(2)—–一个测试用例
- ruby+selenium-webdriver一步一步完成自动化测试-----准备篇
- ruby+selenium-webdriver一步一步完成自动化测试----生成测试报告
- ruby+selenium-webdriver一步一步完成自动化测试(6)—-生成测试报告
- ruby+selenium-webdriver一步一步完成自动化测试-----准备篇
- Cucumber + Watir webdriver + Ruby 功能自动化测试
- 基于ruby的watir自动化测试 笔记一