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

 

 


 

你可能感兴趣的:(IE,url,Class,browser,login,testing)