Google

WATIR Tutorial :Important Notes


1. Single line commenting : Use # at start of the line.
Eg: #ie.link(:text=>/Inbox/,:index=>1 ).click

2. Multi line commenting : USe =begin at the start and =end at the end
Eg:
=begin
ie.link(:text=>/Inbox/,:index=>1).click
ie.link(:text, "Draft").click
ie.link(:text, "Sent").click
=end

3.
Wait Command : Sleep is the command used for wait while running the script.
Eg: Sleep 3

4. To Start an Internet Explorer
require "watir'

brow=Watir::IE.new #creating instance of IE and assigning to a variable
OR
require 'watir'
include Watir

brow=IE.new # include will load the module

5. To Navigate specified URL
ie=Watir::IE.new
ie.goto("
http://thequalitytesting.blogspot.com")
OR
ie=Watir::IE.start("
http://thequalitytesting.blogspot.com")

6. Broswer Actions using WATIR

ie.html # returns html content of the page.

ie.text # returns text on the page.

ie.status # returns status of the page.

ie.title # returns the title of the page.

ie.back # similar to clicking browser back button.
ie.maximize # to minimize.
ie.minimize # to maximize.

ie.bring_to_front # to bring the browser front in all windows.

$HIDE_IE = true # to hide the browser running.

7. WATIR with Javascript functions

To execute javacript functionalites, in Watir "fire_event" should be used.
Eg: a id="netAdvertisingPromoterLink" onclick="return false" href="#">Advertising Survey on Share Market.
In above HTML code onclick is javascript functionality which can be executed using,
ie.link(:id,"netAdvertisingPromoterLink").fire_event("onclick")

8. Highlighting Web Elements
In Watir we can use "flash" method to highlight the objects on the page. This is used mainly to identity specific web element when we have many web elements of same type/name.

Eg:
require 'watir'
ie.link(:text,/Beginners Testing Guide/).flash

9. Regular Expression in WATIR

Regular Expressions also known as RegEx. They are mainly used to Search/Match elements by giving specific strings from the set of strings in the Search Pattern. These are also used to escape Characters/Strings.
^ is the regex matches start of line.
ie.link(:text,/^Beg/).flash

$ is the regex matches end of line.
brow.link(:text,/uide.$/).flash

. matches any character
.* will matches any string and any number of times.
brow.link(:text,/B./).flash

/ (backward slash) is used to escape special characters.
Eg : \/^$.+*?()[]\{\},

matches either of two.
Eg: ab

[] matches a range of characters.
Eg: [a-z0-9]
[^] matches a negation of range of characters.
Eg : [^A-Z]
?= matches positive lookahead
?! matches negative lookahead

No comments: