Watir基础使用实例

HelloWorld

require 'watir'

test_site = 'http://blog.csdn.net/testing_is_believing/'

# open the IE browser

ie = Watir::IE.new

# print some comments

puts "## Beginning of test"

puts "  "

puts "Step 1: go to the test site: " + test_site

ie.goto(test_site)

puts "  Action: entered " + test_site + " in the address bar."

 

 

google 测试例子

require 'watir'

ie = Watir::IE.start("http://www.google.cn")

ie.text_field(:name,"q").set("Watir")

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

 

 

link

require 'watir'

ie = Watir::IE.start("http://blog.csdn.net/testing_is_believing")

ie.link(:url , "http://blog.csdn.net/Testing_is_believing/category/647265.aspx").click

 

 

检查点

contains_text

require 'watir'

ie = Watir::IE.start("http://blog.csdn.net/testing_is_believing")

ie.link(:url , "http://blog.csdn.net/Testing_is_believing/category/647265.aspx").click

if ie.contains_text("HelloWorld - Watir")

   puts "Test passed. Page contains the text: HelloWorld - Watir"

else

   puts "Test failed! Page didn't contain text: HelloWorld - Watir"

end

 

 

assert

require 'watir'

require 'test/unit'

 

class TC_myTest < Test::Unit::TestCase

   

  def setup   

    puts "starting a testcase..."       

  end

 

  def test_1_blog

   

    ie = Watir::IE.new   

    ie.goto("http://blog.csdn.net/testing_is_believing")    

    ie.link(:url , "http://blog.csdn.net/Testing_is_believing/category/647265.aspx").click

    assert(ie.contains_text("HelloWorld - Watir"))

 

  end

 

  def test_2_google

  

   ie = Watir::IE.new  

   ie.goto("http://www.google.cn")

   ie.text_field(:name,"q").set("Watir")

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

 

  end

 

  def teardown

    puts "just finish running a testcase!"

  end

 

end

 

Why are my test cases running in the wrong order?

Subclassing Test::Unit::TestCase will make your tests run in alphabetical order. Therefore in the case of this code:

class SampleTest < Test::Unit::TestCase

  def test_login

    # login test code, etc

  end

 

  def test_account

    # account test code, etc

  end

end

The second test, test_account , will run before test_login .

To run the tests in the order they are defined in the suite, subclass Watir::TestCase instead.

class SampleTest < Watir::TestCase

  def test_login

    # login test code, etc

  end

 

  def test_account

    # account test code, etc

  end

end

 

How to run a specific test case?

ruby my_test_file.rb --name test_account

 

 


命令行运行

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

 

WATIR supports command-line options:

-b (background) Run Internet Explorer invisibly -f (fast) By default, WATIR types slowly and pauses briefly between

                   

actions. 
This switch removes the delays and sets WATIR

                   

to run at full speed. 
The set_fast_speed method of the

                   

IE object performs the same function; set_slow_speed

                   

returns WATIR to its default behaviour.

 

窗口处理

require 'watir'

ie = Watir::IE.start("http://blog.csdn.net/testing_is_believing")

ie.link(:url , "http://hi.csdn.net/!s/wall/to/Testing_is_believing").click

sleep(2)

ie2 = Watir::IE.attach(:url,'http://hi.csdn.net/space.php?do=wall&view=me&username=Testing_is_believing')

if ie2.contains_text(" 给自己留言 ")

    puts "Pass 1!"

end

 

ie.link(:url , "http://hi.csdn.net/!s/msg/to/Testing_is_believing").click

sleep(2)

#ie3 = Watir::IE.attach(:title," 陈能技 - CSDN 个人空间 - Powered by UCenter Home")

ie3 = Watir::IE.attach(:title,/ 陈能技 /)

if ie3.contains_text(" 内容 ")

    puts "Pass 2!"

end

 

 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值