Watir基础使用实例

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:黑体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimHei; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@黑体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:1 135135232 16 0 262144 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} h2 {mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:2; font-size:16.0pt; font-family:Arial; mso-fareast-font-family:黑体; mso-bidi-font-family:"Times New Roman"; mso-font-kerning:1.0pt;} h3 {mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:3; font-size:16.0pt; font-family:"Times New Roman"; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --><!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} --> <!-- [endif]-->

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

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} --> <!-- [endif]-->

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、付费专栏及课程。

余额充值