Watir常用语法集

一. 如何控制IE浏览器
1.1 使用Watir工具,需要在脚本中加上

require 'rubygems'

require 'watir'


1.2 打开一个新的IE窗口

在创建的同时直接转到页面

ie = Watir::IE.start "http://www.maymay.test/login/"

或者

ie = Watir::IE.new

ie.goto("http://www.maymay.test/login/")


1.3 控制已经打开的指定IE窗口

ie = Watir::IE.attach(:url,'http://www.maymay.test/login/')

ie.maximize


1.4 关闭IE窗口

ie.close


1.5 关闭所有打开的IE窗口

i=1

for i in 1..10

  Watir::IE.each do |ie|

  sleep 1

  ie.close

  end

  i=i+1

end


二. 元素定位与控制
2.1 控制超级链接

源代码如:

<ahref="http://pragmaticprogrammer.com/titles/ruby/">ickaxe</a>

这样的代码,可以有两种方法来控制

使用文本属性(Text Attribute)

ie.link(:text, "ickaxe").click

使用url属性

ie.link(:url,"http://pragmaticprogrammer.com/titles/ruby/").click


2.2 控制复选框checkbox

源代码如:

<input type = "checkbox" name = "checkme"value = "1">

可以用两种方法进行控制:

使用name属性:

ie.checkbox(:name, "checkme").set#选中

ie.checkbox(:name, "checkme").clear#清空

使用name属性和value属性:

ie.checkbox(:name, "checkme","1").set#选中

ie.checkbox(:name, "checkme","1").clear #清空


2.3 控制单选框radio

源代码如:

<input type = "radio" name = "clickme" id= "1">

可以用两种方法进行控制:

使用name属性:

ie.radio(:name, "clickme").set#选中

ie.radio(:name, "clickme").clear#清空

使用name属性和value属性:

ie.radio(:name, "clickme","1").set#选中

ie.radio(:name, "clickme","1").clear#清空


2.4 控制选择框

源代码如:

"<select name = "selectme" > <optionname=1> <option name=2>Web Testing<option name=3>in Ruby <optionname=4>is fun </select>"

选择其中的一个属性:

ie.select_list( :name , "selectme").select("isfun")

清空属性:

ie.select_list( :name, "selectme").clearSelection


2.5 文本框中输入文本

可以通过name属性和id属性来进行识别和操作:

如源代码:

<input type = "text" name = "typeinme">

填充数据:

ie.text_field(:name,"typeinme").set("WatirWorld")

清空数据:

ie.text_field(:name, "typeinme").clear


2.6 buttons 按钮

源代码为:

<input type = "button" name = "clickme"value = "Click Me">

可以使用name属性和value属性:

ie.button(:name, "clickme").click
#
利用name属性

ie.button(:value, "Click Me").click
#
利用value属性


2.7 Forms 表单中的按钮

源代码为:

<form action = "submit" name ="submitform" method="post"><input type ="submit" value = "Submit"></input></form>

可以通过name或者value属性来表示

ie.button(:name, "submitform").click

ie.button(:value, "Submit").click


2.8 Forms 表单中的图片按钮

如源代码为:

<form action = "submit" name = "doitform"method="post"><input type="image" src ="images/doit.gif" name = "doit"></form>

可以通过name属性来表示

ie.button(:name, "doit").click


2.9 Forms 表单没有按钮进行提交

如源代码为:

<form action = "login" name = "loginform"method="get"><input name="username"type="text"></input></form>

可以通过提交forms表单本身的name,action和method来实现

ie.form(:name, "loginform").submit

ie.form(:action, "login").submit


2.10 框架和嵌套框架
代码如:
<frameset cols="*,*">  
<frame src="menu.htm" name="menu">
<frame src="main.htm" name="main">
</frameset>

用ie.show_frames可以打印出当前页面框架的数量和名称:

irb(main):009:0> ie.show_frames
there are 2 framesframe  
index: 1 name: menuframe  
index: 2 name: main
=> 0..1

Watir允许通过名称属性来访问框架,如:

ie.frame(:name, "menu")

如果要访问menu框架中的一个超链接<a href="index.htm">Click Menu Item</a>,可以

ie.frame(:name, "menu").link(:text, "Click Menu Item").click

嵌套框架

ie.frame(:name, "frame").frame(:name, "nested_frame")


三. 创建标准测试用例,用if和断言验证结果
3.1 创建测试用例

1.Class类必须以大写字母开头后面紧跟< Test::Unit::TestCase

2.def 方法必须以test开头
3. 当一个类里面有多个方法时,ruby默认会随机运行测试案例,如果需要顺序执行,需要在test后加上字母或数字来强迫它顺序执行,比如“test_a_mytest”

4.测试用例模型:

require 'rubygems'

require 'watir'

require 'test/unit'

class TC_myTest < Test::Unit::TestCase

  def test_ myTestCase

    #用例内容

  end

  def test_anotherTestCase

    #用例内容

  end

  def test_aTestCase

     #用例内容

  end

end


5.应用举例

require 'rubygems'
require 'watir'
require 'test/unit'

class GoogleHomePage < Test::Unit::TestCase
  def test_jointest
    i=1
    j=1
    for i in 1..10
      for j in 1..10
        Watir::IE.each do |ie|
        ie.close
        end
        j=j+1
      end
      ie = Watir::IE.start "http://www.google.com.tw/"
      ie.text_field(:id, "lst-ib").set "site:www.jointest.org"
      ie.button(:value, "Google 搜索").click
      ie.link(:text, "自动化测试论坛- JoinTest - Powered by Discuz!").click
      i=i+1
    end
  end
end

3.2 if 判断和断言
1. 用if判断事物真假

#判断当前页面是否存在“欢迎您...”这几个连续的字(字符串)

if ie.contains_text("欢迎您...")

#判断当前页面是否存在

“name“等于“loginname”的控件

if ie.text_field(:name,"loginname").exists?


#判断当前页面是否存在
“value “等于“登录”的控件

if ie.button(:value , "登录").exists?


#判断当前页面是否存在
“立即购买”这几个连续的字(字符串)

if ie.text.include?("立即购买")


2.断言事物的真假

#断言当前页面的标题“ie.title“等于"会员登录/注册",为真继续,为假抛出错误

assert_equal(ie.title, "会员登录/注册")


#断言当前页面存在 “name“等于“loginname”的控件,为真继续,为假抛出错误

assert(ie.text_field(:name,"loginname").exists?)


#断言当前页面存在 “name“等于“loginname”的控件,为假继续,为真抛出错误

assert_false(ie.text_field(:name,"loginname").exists?)


#断言当前页面存在 “value ”等于“登录”的控件,为真继续,为假抛出错误

assert(ie.button(:value , "登录").exists?)


#断言当前页面存在“立即购买“这几个连续的字,为真继续,为假抛出错误

assert(ie.text.include?("立即购买"))


四. 常见问题及解决方案

我们经常会碰到,且在网络上至今没有很好的解决方案的三个问题,我研究了几个解决方案,验证了上百次,次次能成功执行,现分享给大家!

4.1 关闭所有IE

关于关闭所有打开的IE不能关闭完全的问题,用一个循环搞定

i=1

for i in 1..10

  Watir::IE.each do |ie|

    ie.close

  end

  i=i+1

end


4.2 JS 弹出窗口处理

用一个近似死循环的循环搞定

autoit = WIN32OLE.new('AutoItX3.Control')

while ie.link(:text , "删除").exists?

  ie.link(:text , "删除").click_no_wait

  ret = 1

  while ret==1

    #切换

    #autoit.Send('{TAB}')

    #取消

    #autoit.Send('{ESC}')

    #删除

    autoit.Send('{ENTER}')

    #第一个''内容为弹出层标题,第二个''内容为提示框包含的内容,可以为空,9表示检查次数

    ret = autoit.WinWait('来自网页的消息', '你确认删除吗', 9)

  end

end


4.3 不能set中文的问题(新手建议方案一)

方案一:可以用value代替set,举例如下
ie.text_field(:name, "loginname").set"中文账号"
ie.text_field(:name, "loginname").value = "中文账号"


方案二:
1.当前所有class文件编码格式统一选GB2312(或者GBK)
2.所有ruby class文件代码前面加
require 'win32ole'
WIN32OLE.codepage = WIN32OLE::CP_ACP

方案三:
1.当前所有class文件编码格式统一选GB2312(或者GBK)
2.
C:\Ruby187\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir文件下
win32ole.rb里面的

WIN32OLE.codepage = WIN32OLE::CP_UTF8

改成
WIN32OLE.codepage = WIN32OLE::CP_ACP
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值