值得收藏的Watir笔记

开发测试案例(Developing Test Cases)

1.打开编辑器 2.以.rb为你的文件扩展名 3.在测试文件的第一句写上“require 'watir'”,确保可以访问Watir工具。 4.打开浏览器并转到要测试的应用 5.与之交互并设计你的testcase 6.在测试脚本中使用Watir方法 7.验证结果 与网页交互(Interacting With a Web Page)当使用Watir开发测试脚本的时候,通过给网页上的对象发送消息来与之交互。 ie.text_field(:name , "q").set("bluescorpio") ie.button(:value , "Click Me").click Watir语法(Watir Syntax)

1.使用Watir工具,需要在脚本中加上 require 'watir'

2.创建一个IE的测试实例 ie = Watir::IE.new 或者在创建的同时直接转到页面 ie = Watir::IE.start("http://mytestsite") Watir使用start方法同时创建一个浏览器实例并转到一个页面。

3.页面导航 ie.goto(http://mytestsite)

4.操纵Web页面对象

4.1超链接 4.1.1使用Text属性点击超链接 ie.link(:text , "Pickaxe").click 对应的HTML代码为: Pickaxe 4.1.2使用URL属性点击超链接 ie.link(:url , "http://pragmaticprogrammer.com/titles/ruby/").click 对应的HTML代码为: Test Site

4.2复选框 4.2.1使用name属性设置复选框 ie.checkbox(:name, "checkme").set 4.2.2使用name属性清除复选框 ie.checkbox(:name, "checkme").clear 4.2.3使用name和value属性设置复选框 ie.checkbox(:name, "checkme", "1").set 4.2.4使用name和value属性清除复选框 ie.checkbox(:name, "checkme", "1").clear 对应的HTML代码为:

4.3单选框 4.3.1使用name属性设置单选框 ie.radio(:name, "clickme").set 4.3.2使用name属性清除单选框 ie.radio(:name, "clickme").clear 4.3.3使用name和id属性设置单选框 ie.radio(:name, "clickme", "1").set 4.3.4使用name和id属性清除单选框 ie.radio(:name, "clickme", "1").clear 对应的HTML代码为:

4.4下拉框 4.4.1使用name属性和值来设置下拉框 ie.select_list( :name , "selectme").select("is fun") 4.4.2使用name属性和值来清除下拉框 ie.select_list( :name , "selectme").clearSelection 对应的HTML代码为: Web Testingin Rubyis fun

4.5在Web页面中输入数据 4.5.1使用文本输入框的那么属性设置输入内容 ie.text_field(:name, "typeinme").set("Watir World") 4.5.2清空文本输入框 ie.text_field(:name, "typeinme").clear 对应的HTML代码为:

4.6从Web页面上提交数据 4.6.1按钮 4.6.1.1通过值或标题属性点击按钮 ie.button(:value, "Click Me").click 4.6.1.2通过name属性点击按钮 ie.button(:name, "clickme").click 对应的HTML代码为: 4.6.2表单 4.6.2.1表单中的按钮使用value或标题属性 ie.button(:value, "Submit").click 对应的HTML代码为:

4.6.2.2表单中的图片按钮使用那么属性 ie.button(:name, "doit").click 对应的HTML代码为: 4.6.2.3没有按钮的表单 Watir can submit a form by identifying it by its name, action and method attributes. 可以通过name、action以及method属性来提交表单 ie.form(:name, "loginform").submit ie.form(:action, "login").submit 对应的HTML代码为:

4.6.3框架 ie.show_frames可以打印出当前页面框架的数量和名称 Watir允许通过名称属性来访问框架,如ie.frame("menu") 如果要访问menu框架中的一个超链接,可以ie.frame("menu").link(:text, "Click Menu Item").click 4.6.4嵌套框架 ie.frame("frame1").frame(:name, "nested_frame") 4.6.5新窗口一些Web应用会弹出新窗口或打开一个新窗口,可以使用attach方法来访问并控制新窗口。通过标示新窗口的URL或者title来访问。 ie2 = Watir::IE.attach(:url, 'http://mytestsite') ie3 = Watir::IE.attach(:title, 'Test New Window') 也可以使用正则表达式 ie4 = Watir::IE.attach(:title, /Test New/) 注意:不要把新窗口分配到你的ie变量,最好给新窗口一个不同的名字

5.验证结果比较好的方法是在测试案例中假如验证点 5.1对象存在使用Watir方法contains_text ie.contains_text("Reached test verification point.") if ie.contains_text("Reached test verification point.") puts: "Test passed. Page contains the text: Reached test verification point." else puts: "Test failed! Page didn't contain text: Reached test verification point." end 5.2使用test::unit Assertions 5.2.1需要test::unit require 'test/unit' 5.2.2创建测试实例 class TC_myTest < Test::Unit::TestCase ...fill in Test Case methods here... end 5.2.3创建测试用例方法在测试类中,需要声明象下面的方法: def test_myTestCase fill in method body with Watir code and assertion here end 方法必须以test开头,ruby会随机运行测试案例,如果需要顺序执行,需要在test后加上字母或数字来强迫它顺序执行,比如“test_a_mytest” 定义测试方法的类: class TC_myTest < Test::Unit::TestCase def test_ myTestCase Watir code and assertion here... end def test_anotherTestCase Watir code and assertion here... end def test_aTestCase Watir code and assertion here... end end 5.2.4使用Assertions Watir通过在一个asert覆写Watir方法支持assertions。 assert(ie.contains_text("Reached test verification point.") 5.2.5Setup and Teardown def setup fill in code that will run before every test case here... end def teardown fill in code that will run after every test case here... end

6.Tips and Tricks Running Tests With the Browser Not Visible Run the tests with a "-b" option if you don't want the browser to be visible. ex. myTest.rb -b

[@more@]的

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9934490/viewspace-967238/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9934490/viewspace-967238/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值