分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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
defsetup
puts "starting a testcase..."
end
deftest_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
deftest_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
defteardown
puts "just finish running a testcase!"
end
end
Whyare 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
Howto 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" optionif you don't want the browser to be visible. ex. myTest.rb -b
WATIR supports command-lineoptions:
-b (background) Run InternetExplorer invisibly -f (fast) By default, WATIR types slowly and pauses brieflybetween
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 UCenterHome")
ie3 = Watir::IE.attach(:title,/陈能技/)
if ie3.contains_text("内容")
puts "Pass 2!"
end