Watir-WebDriver自动化测试学习指导_王勇.docx_第1页
Watir-WebDriver自动化测试学习指导_王勇.docx_第2页
Watir-WebDriver自动化测试学习指导_王勇.docx_第3页
Watir-WebDriver自动化测试学习指导_王勇.docx_第4页
Watir-WebDriver自动化测试学习指导_王勇.docx_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Watir-WebDriver博客:/Javame/支持哪些浏览器?几乎所有的浏览器: 比如Firefox, Chrome 和IE,除了Safari。支持网页上哪些元素?watir-webdriver支持所有的HTML元素运行模式是什么?Watir-WebDriver是基于ruby开发web驱动框架自动化测试框架根据不同业务开发相应自动化用例,由Ruby测试框架统一调用分析展示。封装工具类,实现耦合页面元素attribute_value获取当前控件的属性Value = ie.link(:id=xxx).attribute_value(href)rand_select 随机选择select list中的某一项ie.select_list(:name=).rand_selectpopupwin点击弹窗上的确定按钮ie.popupwin.button(:name=确定).clicksikuli_image点击图片控件ie.sikuli_image(:image=1.png).clickie.sikuli_image(:image=1.png;2.png).click#可以指定多张图片来识别double_click双击事件ie .sikuli_image(:image=1.png).double_clickright_click右击事件exist?判断用户元素是否存在edit = ie.text_field(:name,username) if edit.exist?() #The highlightededit.flash ie.text_field(:name, password).set(pwd)ie.button(:class, x-login-submit).clickendendText Fieldsrequire watir-webdriverb = Watir:Browser.start bit.ly/watir-webdriver-demot = b.text_field :id = entry_0t.exists?t.set your namet.valueSelect Lists Combosrequire watir-webdriverb = Watir:Browser.start bit.ly/watir-webdriver-demos = b.select_list :id = entry_1s.select Rubys.selected_optionsRadiosrequire watir-webdriverb = Watir:Browser.start bit.ly/watir-webdriver-demor = b.label(:text = What is ruby?).parent.radio :value = A gemr.exists?r.setr.set?Checkboxesrequire watir-webdriverb = Watir:Browser.start bit.ly/watir-webdriver-democ = b.label(:text = What versions of ruby?).parent.checkbox :value = 1.9.2c.exists?c.setc.set?Buttonsrequire watir-webdriverb = Watir:Browser.start bit.ly/watir-webdriver-demobtn = b.button :value, Submitbtn.exists?btn.clickLinksrequire watir-webdriverb = Watir:Browser.start bit.ly/watir-webdriver-demol = b.link :text = Google Docsl.exists?l.clickDivs & Spansrequire watir-webdriverb = Watir:Browser.start bit.ly/watir-webdriver-demod = b.div :class = ss-form-desc ss-no-ignore-whitespaced.exists?d.texts = b.span :class = ss-powered-bys.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-blue checked).radios1 复选框 ? 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) (保存文件的弹出窗口) 图片 ? ie.image(:src=/word3a_nor.gif/).click/doclickback后退ie.backforward前进ie.forwardrefresh刷新页面ie.refresh在Watir-WebDriver中处理frame是非常简单的,就跟处理其他页面元素一样:b.frame(:id = content_ifr).send_keys hello world文件的下载最简单最好的处理文件下载对话框的方式就是完全的避免对话框弹出。可以在代码里告诉浏览器自动的将文件下载到指定目录,然后在测试用例中访问该目录进行验证。Firefoxdownload_directory = #Dir.pwd/downloadsdownload_directory.gsub!(/, ) if Selenium:WebDriver:Platform.windows?profile = Selenium:WebDriver:Firefox:Profile.newprofilebrowser.download.folderList = 2 # custom locationprofilebrowser.download.dir = download_directoryprofilebrowser.helperApps.neverAsk.saveToDisk = text/csv,application/pdfb = Watir:Browser.new :firefox, :profile = profile关于Firefox的所有配置项可以通过在地址栏中输入about:config进行查看。If you want to know a way to work out the file types (eg. application/pdf) then you can read the following blog post for an step by step guide. 如果你想知道如何处理特定类型的文件,请阅读这篇博文。Chromedownload_directory = #Dir.pwd/downloadsdownload_directory.gsub!(/, ) if Selenium:WebDriver:Platform.windows?profile = Selenium:WebDriver:Chrome:Pmpt_for_download = falseprofiledownload.default_directory = download_directoryb = Watir:Browser.new :chrome, :profile = profile浏览器新窗口当一个新的浏览器窗口打开时,你可以使用use方法来处理这个新窗口。browser.window(:title = annoying popup).use do browser.button(:id = close).clickendJS弹出框在web应用中,JavaScript对话框是十分常见的。Watir-WebDriver内建了处理这些对话框的方法,并且可以返回对话框中显示的内容。首先,加载这个扩展:require watir-webdriver/extensions/alertsJAVASCRIPT ALERTSbrowser.alert do browser.button(:value = Alert).clickend #= the alert messageJAVASCRIPT CONFIRMSbrowser.confirm(true) do browser.button(:value = Confirm).clickend #= the confirm messageJAVASCRIPT PROMPTmpt(hello) do browser.button(:value = Prompt).clickend #= :message = foo, :default_value = bar 可选方法如果你使用上面的方法时遇到了麻烦,你可以自行覆盖JavaScript functions,这样一来原来应该显示的对话框就可以在触发时不显示了。# 使alert方法返回空browser.execute_script(window.alert = function() )# 使prompt返回特定的字符串,用来模拟用户的输入browser.execute_script(mpt = function() return my name)# 使prompt方法返回null用来模拟用户点击了Cancel(取消)按钮browser.execute_script(mpt = function() return null)# 使confirm方法返回true用来模拟用户点击了OK(确定)按钮browser.execute_script(window.confirm = function() return true)# 使confirm方法返回false用来模拟用户点击了Cancel(取消)按钮browser.execute_script(window.confirm = function() return false)页面性能Watir-WebDriver-Performance gem 提供在访问页面的同时进行页面性能度量的功能,其使用的是W3C页面性能度量指标。这是一个完美的捕获响应性能指标的解决方案,其使用方法非常直观和简单,不过目前只支持Chrome和IE9l浏览器。require watir-webdriverrequire watir-webdriver-performanceb = Watir:Browser.new :chrome10.times do b.goto http:/17 load_secs = b.performance.summary:response_time/1000 puts Load Time: #load_secs seconds.end 其统计结果如下:Load Time: 3.701 seconds.截屏Watir-WebDriver内建的截图功能很赞也很好用。browser.driver.save_screenshot screenshot.pngThe great thing about this is it gives you a screen shot of the entire page, not just above the fold. 截图功能最棒的地方在于它能捕获到整个页面,而不是屏幕上显示的那部分。如果你正在使用Cucumber,那么你可以简单的将下面的代码添加到env.rb文件中,这样你可以在html的报告中插入截图:After do |scenario| browser.driver.save_screenshot screenshot.png embed screenshot.png, image/pngend模拟特殊按键使用.send_keys方法可以模拟特殊的键盘按键(比如shift),其参数是你所需要模拟的按键的符号表示(symbolic)。b.send_keys :enter也可以这样做:b.element.send_keys :control, a, :backspace你还可以修改click方法的行为,使得点击可以配合按键一起进行:b.element.click(:shift, :control)支持的按键键名列表如下::null:cancel:help:backspace:tab:clear:return:enter:shift:left_shift:control:left_control:alt:left_alt:pause:escape:space:page_up:page_down:end:home:left:arrow_left:up:arrow_up:right:arrow_right:down:arrow_down:insert:delete:semicolon:equals:numpad0:numpad1:numpad2:numpad3:numpad4:numpad5:numpad6:numpad7:numpad8:numpad9:multiply:add:separator:subtract:decimal:divide:f1:f2:f3:f4:f5:f6:f7:f8:f9:f10:f11:f12:meta:command富文本编辑器有两种方法可以通过Watir-WebDriver向所见即所得编辑器(应该指的是富文本编辑器)中输入文字:定位编辑器所在的iFrame,然后使用.send_keys方法(缺点是浏览器必须在前台运行)在浏览器上执行javascript,通过js脚本去设置编辑器的值CKEditorrequire watir-webdriverb = Watir:Browser.new :firefoxb.goto /demob.execute_script(CKEDITOR.instanceseditor1.setData(hello world);)b.frame(:title = Rich text editor, editor1, press ALT 0 for help.).send_keys hello world againTinyMCE Editorrequire watir-webdriverb = Watir:Browser.newb.goto /tryit/full.phpb.execute_script(tinyMCE.get(content).execCommand(mceSetContent,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论