Watir-WebDriver

                      —— 软件测试的自动化时代 QQ群:160409929

支持哪些浏览器?

几乎所有的浏览器: 比如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_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")                                            

                            if edit.exist?() 

                                     #The highlighted

                                     edit.flash                         

                                     ie.text_field(:name, "password").set(pwd)            

                                     ie.button(:class, "x-login-submit").click 

                            end

                   end

Text Fields

require 'watir-webdriver'

b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'

t = b.text_field :id => 'entry_0'

t.exists?

t.set 'your name'

t.value

Select Lists – 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

Radios

require 'watir-webdriver'

b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'

r = b.label(:text => 'What is ruby?').parent.radio :value => 'A gem'

r.exists?

r.set

r.set?

Checkboxes

require 'watir-webdriver'

b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'

c = b.label(:text => 'What versions of ruby?').parent.checkbox :value => '1.9.2'

c.exists?

c.set

c.set?

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 => 'Google Docs'

l.exists?

l.click

Divs & Spans

require 'watir-webdriver'

b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'

d = b.div :class => 'ss-form-desc ss-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-blue checked").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) (保存文件的弹出窗口)

图片

?  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 "hello world"

文件的下载

最简单最好的处理文件下载对话框的方式就是完全的避免对话框弹出。

可以在代码里告诉浏览器自动的将文件下载到指定目录,然后在测试用例中访问该目录进行验证。

Firefox

download_directory = "#{Dir.pwd}/downloads"

download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?

profile = Selenium::WebDriver::Firefox::Profile.new

profile['browser.download.folderList'] = 2 # custom location

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'进行查看。

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. 如果你想知道如何处理特定类型的文件,请阅读这篇博文。

Chrome

download_directory = "#{Dir.pwd}/downloads"

download_directory.gsub!("/", "\\") if  Selenium::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 => "annoying popup").use do

  browser.button(:id => "close").click

end

JS弹出框

在web应用中,JavaScript对话框是十分常见的。

Watir-WebDriver内建了处理这些对话框的方法,并且可以返回对话框中显示的内容。首先,加载这个扩展:

require "watir-webdriver/extensions/alerts"

JAVASCRIPT ALERTS

browser.alert do

  browser.button(:value => 'Alert').click

end #=> 'the alert message'

JAVASCRIPT CONFIRMS

browser.confirm(true) do

  browser.button(:value => 'Confirm').click

end #=> 'the confirm message'

JAVASCRIPT PROMPT

browser.prompt('hello') do

  browser.button(:value => 'Prompt').click

end #=> { :message => 'foo', :default_value => 'bar' }

可选方法

如果你使用上面的方法时遇到了麻烦,你可以自行覆盖JavaScript functions,这样一来原来应该显示的对话框就可以在触发时不显示了。

# 使alert方法返回空

browser.execute_script("window.alert = function() {}")

# 使prompt返回特定的字符串,用来模拟用户的输入

browser.execute_script("window.prompt = function() {return 'my name'}")

# 使prompt方法返回null用来模拟用户点击了Cancel(取消)按钮

browser.execute_script("window.prompt = 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-webdriver'
require 'watir-webdriver-performance' b = Watir::Browser.new :chrome 10.times do b.goto 'http://17test.info' 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.png'

The 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/png'

end

模拟特殊按键

使用.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脚本去设置编辑器的值

CKEditor

require 'watir-webdriver'

b = Watir::Browser.new :firefox

b.goto 'http://ckeditor.com/demo'

b.execute_script("CKEDITOR.instances['editor1'].setData('hello world');")

b.frame(:title => 'Rich text editor, editor1, press ALT 0 for help.').send_keys 'hello world again'

TinyMCE Editor

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, 'hello world' );")

b.frame(:id => "content_ifr").send_keys 'hello world again'

QA

基于Ruby的Watir-WebDriver自动化测试框架的更多相关文章

  1. 基于ruby的watir自动化测试 笔记二

    基于ruby的watir自动化测试 笔记一的补充版,新增加了些特殊的控件捕获方法.还在更新中.... attribute_value 获取当前控件的属性 Value = ie.link(:id=> ...

  2. 基于Selenium+Python的web自动化测试框架

    一.什么是Selenium? Selenium是一个基于浏览器的自动化测试工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.S ...

  3. 基于Python的HTTP接口自动化测试框架实现

    今天我们来讲一下基于Python的HTTP接口自动化测试框架的实现,范例如下: 一.测试需求描述 对服务后台一系列的http接口功能测试. 输入:根据接口描述构造不同的参数输入值 输出:XML文件 e ...

  4. 基于ruby的watir自动化测试 笔记一

    基于Ruby的watir-webdriver自动化测试方案与实施(五)   基于Ruby的watir-webdriver自动化测试方案与实施(四)   基于Ruby的watir-webdriver自动 ...

  5. 基于Java+Selenium的WebUI自动化测试框架(一)---页面元素定位器

    对于自动化测试,尤其是UI的自动化测试.是很多做黑盒功能测试的同学,入门自动化测试一个最为直观的或者说最容易理解的途径之一. 对于手工测试和自动化测试的优劣,网上有很多论述,在这里不作展开讨论.但是, ...

  6. 基于Jmeter和Testlink的自动化测试框架研究与实施

    关于测试框架搭建的详细过程,会在另一篇文章中详细介绍:http://www.cnblogs.com/leeboke/p/6145977.html 摘 要 目前基于Jmeter的接口自动化测试框架,大多 ...

  7. 基于Java+Selenium的WebUI自动化测试框架(十四)-----使用TestNG的Sample

    到目前为止,我们所写的东西,都是集中在如何使用Selenium和Java来定位和读取元素.那么,到底如何具体开展测试,如何实现参数化,如何实现判定呢?下面,我们来看看Java应用程序的测试框架吧. 当 ...

  8. 关于基于python2.7的unity自动化测试框架GAutomator测试环境的搭建(源码网盘下载地址:https://pan.baidu.com/s/1c2TXwtU)

    关于基于python 2.7的unity自动化测试框架GAutomator测试环境的搭建 百度云盘链接(思维图学习资料):https://pan.baidu.com/s/1dFWExMD 准备工作(具 ...

  9. 基于Java+Selenium的WebUI自动化测试框架(八)-----读取元素(XML文件)

    我们继续回到自动化测试框架的主线上来,在前面的文章中,我们定义一个页面元素的主要参数有:路径,找寻方式,等待时间,名称,这个四个参数.另外,我们还需要考虑一个问题,就是网站的页面. 举个例子来说,如果 ...

  10. 基于Java+Selenium的WebUI自动化测试框架(十三)-----基础页面类BasePage(Excel)

    前面,我们讲了如何使用POI进行Excel的“按需读取”.根据前面我们写的BasePageX,我们可以很轻松的写出来基于这个“按需读取”的BasePage. package webui.xUtils; ...

随机推荐

  1. 编写简单的ramdisk(无请求队列)

    最近在研究块设备驱动的编写,看了赵磊大牛的<写一个块设备驱动>,受益匪浅,虽然能看懂里面说的,但动手写写代码还是能加深理解的,下面实现的ramdisk写的很简单,如果有错误,欢迎大牛们指正 ...

  2. 分析MariaDB初始化脚本mysql_install_db

    在初始化MySQL的过程中经常会碰到各种问题,如 FATAL ERROR: Could not find ./bin/my_print_defaults ERROR: Can't create/wri ...

  3. mysql乐观锁总结和实践--转

    原文地址:http://chenzhou123520.iteye.com/blog/1863407 上一篇文章<MySQL悲观锁总结和实践>谈到了MySQL悲观锁,但是悲观锁并不是适用于任 ...

  4. 自制 移动端 纯原生 Slider滑动插件

    在Google搜关键字“slider”或“swiper”能找到一大堆相关插件,自己造轮子是为了能更好的理解其中的原理. 给这个插件取名为“veSlider”是指“very easy slider”非常 ...

  5. 【SQLServer】DBHelper即C#数据库底层封装

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  6. 符合我公司GIS开源解决方案的探讨

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 这一周,我对GIS开源解决方案中涉及到的开源软件以及相关技术 ...

  7. .NET正则表达式基础入门(二)

    量词 正则表达式的量词用于表明前面的子表达式需要匹配的次数.阅读本章前,建议先下载我于CSDN上传的示例代码,下载无需分数,下载链接. 1.量词的一般形式 "{n}"," ...

  8. 【原创】Silverlight DataGrid对核心控件DataGrid的任意单元格进行获取和设置分析。

    前几天,公司同事有个需求需要对系统中的DataGrid控件的指定单元格(如图,申请人ID)进行禁用设置,尝试了很多次总是 整行整列的 禁用 没实现效果. 网上资料较少,没找到解决措施. 尽管silve ...

  9. combox

    通过combox控件本身的item添加了选项后,该控件在启动后SelectedIndex默认值是-1,所以最好是在窗体加载的时候初始化城SelectedIndex=0 另外如果是窗体加载时item从数 ...

  10. C标准头文件<signal.h>

    信号即异常,或者理解为中断,一个进程接收到一个信号,如果没有处理机制,就会按照默认的处理方式进行处理,而默认的处理方式通常是终止当前进程或忽略该信号.当然,程序也可以编写相应的处理信号的函数,一旦接收 ...