Friday, October 2, 2009

Watir QA Automation: Testing web links with Watir

Testing web links with Watir


Before we start testing links, here are some of the common formats for the links used

Simplest for of the link is


<a href="url">Link text</a>


Here are various examples of the links that will be used in this tutorial to explain how Watir can access the link on web page using various link properties/attributes


<a href="http://www.somedomain.com/">Visit somedomain!</a>
<a href="http://www.somedomain.com/" target="_blank">Visit somedomain in new window!</a>
<a href="http://www.somedomain.com/" target="_top">Go to top!</a>


Anchors


<a name="label">Any content</a>
<a href="#label">Any content</a>
<a href="#tips">Jump to the Tips Section</a>
<a href="http://www.somedoamin.com/someurl.htm#tips">Jump to the Tips Section</a>


You can also use an image as a link:


<a href="http://someurl/somepage.html"><img border="0" src="buttonnext.gif" width="65" height="38"></a>
<a href="http://someurl/somepage.html"><img src="images/button.jpg" border="0" alt=""></img></a> The button is really a link





This is a mail link:


<a href="mailto:someone@somedomain.com?subject=Hello%20again">Send Mail</a>
<a href="lhttp://someurl/somepage.html" class="link_class_1">test1</a>


<a href="http://someurl/somepage.html" name="link_name">Link Using a name</a>
<a href="lhttp://someurl/somepage.html" title="link_title">Link Using a title</a>


Accessing link using link text


assert(browser.link(:text, "test1").exists?)


Accessing link using link text with regular expression


assert(browser.link(:text, /TEST/i).exists?)


Accessing link using link url


assert(browser.link(:url, /someurl.html/).exists?)
assert(browser.link(:url, "specificurl.html").exists?)


Accessing link using link id


assert(browser.link(:id, "link_id").exists?)


Accessing link using link id with regular expression


assert(browser.link(:id, /_id/).exists?)


Accessing link using link name


assert(browser.link(:name, "link_name").exists?)


Accessing link using link name and regular expression


assert(browser.link(:name, /_n/).exists?)


Accessing link using link title


assert(browser.link(:title, "link title").exists?)


Accessing link using link title and regular expression


assert_false(browser.link(:title, /title/).exists?)


Accessing link using link index


assert(browser.link(:index, 1).exists?)


Clicking the link



browser.link(:text, "test1").click
browser.link(:url, /link.html/).click
browser.link(:index, 1).click


Querying link properties


browser.link(:index, 1).href
browser.link(:index, 1).value
browser.link(:index, 1).innerText
browser.link(:index, 1).name
browser.link(:index, 1).id
browser.link(:index, 1).disabled
browser.link(:index, 1).type
browser.link(:index, 1).class_name
browser.link(:index, 7).title
rowser.links[7].innerText


Link iterator


browser.links.length
index = 1
browser.links.each do |link|
assert_equal(browser.link(:index, index).href , link.href )
assert_equal(browser.link(:index, index).id , link.id )
assert_equal(browser.link(:index, index).name , link.name )
assert_equal(browser.link(:index, index).innerText , link.innerText )
index+=1
end


Accessing links in frames


browser.frame("linkFrame").link(:text, "test1").exists?
browser.frame("linkFrame").link(:index, 1).href


Iterating thru links in frames


count =0
browser.frame("linkFrame").links.each do |l|
count+=1
end


Display all links in the page


browser.showLinks