Watir Study Notes/Ruby

Source: Internet
Author: User
 
Development test cases)
1. Open the editor
2. Use. RB as your file extension
3. Write "require 'watir'" in the first sentence of the test file to ensure that the watir tool is accessible.
4. Open the browser and go to the application to be tested
5. interact with it and design your testcase
6. Use the watir method in the test script
7. Verification results

Interacting with a web page)
When you use watir to develop a test script, you can send a message to the object on the webpage to interact with it.

Ie. text_field (: name, "Q"). Set ("bluescorpio ")
Ie. Button (: value, "click me"). Click

Watir syntax)
1. To use the watir tool, you must add it to the script
Require 'watir'

2. Create an IE test instance
Ie = watir: IE. New
Or directly go to the page while creating
Ie = watir: IE. Start ("http: // mytestsite"
Watir uses the start method to create a browser instance at the same time and go to a page.

3. page navigation
Ie. Goto ("http: // mytestsite"

4. Manipulate web page objects
4.1 hyperlink
4.1.1 click a hyperlink using the text attribute
Ie. Link (: text, "Pickaxe"). Click
The corresponding HTML code is:
<A href = "http://pragmaticprogrammer.com/titles/ruby/";> pickaxe </a>
4.1.2 click a hyperlink using URL attributes
Ie. Link (: URL, "http://pragmaticprogrammer.com/titles/ruby/";). Click
The corresponding HTML code is:
<A href = "http://pragmaticprogrammer.com/titles/ruby/";> test site </a>

4.2 check box
4.2.1 use name attribute setting check box
Ie. checkbox (: name, "checkme"). Set
4.2.2 clear the check box using the name attribute
Ie. checkbox (: name, "checkme"). Clear
4.2.3 use name and value attributes to set check boxes
Ie. checkbox (: name, "checkme", "1"). Set
4.2.4 clear the check box using the name and value Attributes
Ie. checkbox (: name, "checkme", "1"). Clear
The corresponding HTML code is:
<Input type = "checkbox" name = "checkme" value = "1">

4.3 single region
4.3.1 use the name attribute to set a ticket
Ie. Radio (: name, "clickme"). Set
4.3.2 clear a ticket using the name attribute
Ie. Radio (: name, "clickme"). Clear
4.3.3 use the name and ID attributes to set a ticket
Ie. Radio (: name, "clickme", "1"). Set
4.3.4 clear a ticket using the name and ID attributes
Ie. Radio (: name, "clickme", "1"). Clear
The corresponding HTML code is:
<Input type = "radio" name = "clickme" id = "1">

4.4 drop-down list
4.4.1 use the name attribute and value to set the drop-down list
Ie. select_list (: name, "selectme"). Select ("is fun ")
4.4.2 clear the drop-down box using the name attribute and Value
Ie. select_list (: name, "selectme"). clearselection
The corresponding HTML code is:
<Select name = "selectme"> <option name = 1> <option name = 2> Web Testing <option name = 3> in ruby <option name = 4> is Fun </ select>

4.5 enter data on the web page

4.5.1 set the input content using the attributes in the text input box
Ie. text_field (: name, "typeinme"). Set ("watir world ")
4.5.2 clear text input box
Ie. text_field (: name, "typeinme"). Clear
The corresponding HTML code is:
<Input type = "text" name = "typeinme">

4.6 submit data from the web page
4.6.1 button
4.6.1.1 click the button through value or title attribute
Ie. Button (: value, "click me"). Click
4.6.1.2 click the button through the name attribute
Ie. Button (: name, "clickme"). Click

The corresponding HTML code is:
<Input type = "button" name = "clickme" value = "Click me">

4.6.2 form
4.6.2.1 buttons in the form
Use value or title attribute
Ie. Button (: value, "Submit"). Click
The corresponding HTML code is:
<Form action = "Submit" name = "submitform" method = "Post"> <input type = "Submit" value = "Submit"> </input> </form>

4.6.2.2 image buttons in the form
Use this property
Ie. Button (: name, "doit"). Click
The corresponding HTML code is:
<Form action = "Submit" name = "doitform" method = "Post"> <input type = "image" src = "images/doit.gif" name = "doit"> </ form>

4.6.2.3 form without buttons
Watir can submit a form by identifying it by its name, action and method attributes.
You can submit a form using the name, action, and method attributes.
Ie. Form (: name, "loginform"). Submit
Ie. Form (: Action, "login"). Submit
The corresponding HTML code is:
<Form action = "login" name = "loginform" method = "get"> <input name = "username" type = "text"> </input> </form>

4.6.3 framework
Ie. show_frames can print the number and name of frames on the current page.
Watir allows access to the framework through the name attribute, such as IE. Frame ("menu ")
To access a hyperlink in the menu framework, you can use IE. Frame ("menu"). Link (: text, "click menu item"). Click

4.6.4 nested framework
Ie. Frame ("frame1"). Frame (: name, "nested_frame ")

4.6.5 new window
Some Web applications will pop up a new window or open a new window. You can use the attach method to access and control the new window. Access by marking the URL or title of the new window.
Ie2 = watir: IE. Attach (: URL, 'HTTP: // mytestsite ')
Ie3 = watir: IE. Attach (: title, 'test new window ')
You can also use a regular expression.
Ie4 = watir: IE. Attach (: title,/test new /)
Note: Do not allocate the new window to your IE variable. It is best to give the new window a different name.

5. Verification results
A better way is to test the verification point in the test case.
5.1 objects exist
Use the watir method contains_text
Ie. 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 use test: Unit assertions
5.2.1 test: Unit is required
Require 'test/unit'

5.2.2 create a test instance
Class tc_mytest <test: Unit: testcase
... Fill in test case methods here...
End

5.2.3 method for creating test cases
In the test class, the following method must be declared:
Def test_mytestcase
Fill in method body with watir code and assertion here
End

The method must start with test, and Ruby will run the test case randomly. If you need to execute the test in sequence, you need to add letters or numbers after the test to force the execution in sequence, such as "test_a_mytest"

Define the test method class:
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...
End
End

5.2.4 Use assertions
Watir supports assertions by overwriting watir in an asert.
Assert (ie. contains_text ("reached test verification point .")

5.2.5setup and teardown
Def setup
Fill in code that will run before every test case here...
End
Def teardown
Fill in code that will run after every test case here...
End

6. tips and tricks
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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.