开发测试案例(Developing Test Cases)1.打开编辑器2.以.rb为你的文件扩展名3.在测试文件的第一句写上“require 'watir'”,确保可以访问Watir工具。4.打开浏览器并转到要测试的应用5.与之交互并设计你的testcase6.在测试脚本中使用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代码为:<a href="http://pragmaticprogrammer.com/titles/ruby/">Pickaxe</a>4.1.2使用URL属性点击超链接ie.link(:url , "http://pragmaticprogrammer.com/titles/ruby/").click对应的HTML代码为:<a href="http://pragmaticprogrammer.com/titles/ruby/">Test Site</a> 4.2复选框4.2.1使用name属性设置复选框 ie.checkbox(:name, "checkme").set4.2.2使用name属性清除复选框ie.checkbox(:name, "checkme").clear4.2.3使用name和value属性设置复选框ie.checkbox(:name, "checkme", "1").set4.2.4使用name和value属性清除复选框ie.checkbox(:name, "checkme", "1").clear对应的HTML代码为:<input type = "checkbox" name = "checkme" value = "1"> 4.3单选框4.3.1使用name属性设置单选框ie.radio(:name, "clickme").set4.3.2使用name属性清除单选框ie.radio(:name, "clickme").clear4.3.3使用name和id属性设置单选框ie.radio(:name, "clickme", "1").set4.3.4使用name和id属性清除单选框ie.radio(:name, "clickme", "1").clear对应的HTML代码为:<input type = "radio" name = "clickme" id = "1"> 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代码为:<select name = "selectme" > <option name=1> <option name=2>Web Testing <option name=3>in Ruby <option name=4>is fun </select> 4.5在Web页面中输入数据 4.5.1使用文本输入框的那么属性设置输入内容ie.text_field(:name, "typeinme").set("Watir World")4.5.2清空文本输入框ie.text_field(:name, "typeinme").clear对应的HTML代码为:<input type = "text" name = "typeinme" > 4.6从Web页面上提交数据4.6.1按钮4.6.1.1通过值或标题属性点击按钮ie.button(:value, "Click Me").click4.6.1.2通过name属性点击按钮ie.button(:name, "clickme").click 对应的HTML代码为:<input type = "button" name = "clickme" value = "Click Me"> 4.6.2表单4.6.2.1表单中的按钮使用value或标题属性ie.button(:value, "Submit").click对应的HTML代码为:<form action = "submit" name = "submitform" method="post"><input type = "submit" value = "Submit"></input></form> 4.6.2.2表单中的图片按钮使用那么属性ie.button(:name, "doit").click对应的HTML代码为:<form action = "submit" name = "doitform" method="post"><input type="image" src = "images/doit.gif" name = "doit"></form> 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").submitie.form(:action, "login").submit对应的HTML代码为:<form action = "login" name = "loginform" method="get"><input name="username" type="text"></input></form> 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_textie.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 Assertions5.2.1需要test::unitrequire '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 hereend 方法必须以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... endend 5.2.4使用AssertionsWatir通过在一个asert覆写Watir方法支持assertions。assert(ie.contains_text("Reached test verification point.") 5.2.5Setup and Teardowndef setup fill in code that will run before every test case here...enddef teardown fill in code that will run after every test case here...end 6.Tips and TricksRunning Tests With the Browser Not VisibleRun the tests with a "-b" option if you don't want the browser to be visible. ex. myTest.rb -b Trackback: http://tb.donews.net/TrackBack.aspx?PostId=821918

评论