Showing posts with label safariwatir. Show all posts
Showing posts with label safariwatir. Show all posts

Monday, October 19, 2009

Watir Tutorial: File fields, file selector/chooser test automation

As defined in HTML specifications, file field provides a means for users to attach a file to the form's contents. It is generally rendered by text field and an associated button which when clicked invokes a file browser to select a file name. The file name can also be entered directly in the text field. Just like type=text you can use the size attribute to set the visible width of this field in average character widths. You can set an upper limit to the length of file names using the maxlength attribute. Some user agents support the ability to restrict the kinds of files to those matching a comma separated list of MIME content types given with the ACCEPT attribute e.g. accept="image/*" restricts files to images. Further information can be found here http://www.w3.org/TR/html401/interact/forms.html#h-17.4


Here is the list of common file field attributes










AttributeTypeDescription
nameCDATAName of the field
valueCDATAStores the value of the field when set, or used to set the default value
disabled(disabled)Will display the file field as disabled
readonly(readonly)User cannot set the file field, will only display the preset value
sizeCDATALength of the file field visible on the web form/page
maxlengthNUMBERMax chars allowed for file fields
altCDATAshort description
tabindexNUMBERposition in tabbing order
acceptContentTypesList of MIME types for file upload


Before we start testing file fields, here are some of the common example of how file fields are used:


Simplest form of the file field is


<input type ='file' value='some value'/>


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


<input type='file' name='file1' value='uploadtest.html' class='file_class'/>
<input type ='file' id='file2' value='uploadtest.html'/>
<input type ='file' name='disabled' value='uploadtest.html' disabled='disabled'/>
<input type="file" name="datafile" size="40"/>
<input type="file" ... style="color:#fff; background:#030"/>
<input type='file' name='readonly' value='uploadtest.html' readonly='readonly'/>
<input type='file' name='file3' value="" class='file_class'/>
<input type=file name=photo size=20 accept="image/*"/>




File fields can appear with the forms or outside the form.

Here is the sample of file field within the form


<form name='testform' method='get' action='test.html'>
<input type='file' name='file3' value='uploadtest.html'/>
<input name="upload" type="submit" value="upload"/>
</form>


You can also access file fields using :beforeText and :afterText attributes, where file field appears before/after certain targeted text.


<input type='file' name='beforetest' value='uploadtest.html'/>This Text After

This Text Before<input type='file' name='aftertest' value='uploadtest.html'/>



Accessing file field using file field id



<input type ='file' id='file_field_id' value='uploadtest.html'/>


browser.file_field(:id, "file_field_id").exists?


Accessing file field using file field id with regular expression



browser.file_field(:id, /_id/).exists?


Accessing file field using file field name



<input type='file' name='file field_name' value='uploadtest.html' class='file_class'/>


browser.file_field(:name, "test1").exists?
browser.file field(:name, "file field_name").exists?


Accessing file field using file field name and regular expression


browser.file field(:name, /_name/).exists?


Accessing file field using file field index


browser.file field(:index, 1).exists?


Querying file field properties


browser.file_field(:index, 1).value
browser.file_field(:index, 1).name
browser.file_field(:index, 1).id
browser.file_field(:index, 1).disabled
browser.file_field(:index, 1).type
browser.file_field(:index, 1).class_name


Get all file fields in the browser

browser.file_fields


Get number of file fields in the page

browser.file_fields.length


File field iterator


browser.file_fields.length
index = 1
browser.file fields.each do |file_field|
assert_equal(browser.file_field(:index, index).id, file_field.id )
assert_equal(browser.file_field(:index, index).name, file_field.name )
index+=1
end


Accessing file fields in forms


browser.form(:id, "test_form_id").file_field(:id, "test1").exists?
browser.form(:name, "test_form_id").file_field(:index, 1).exists?



Accessing file fields in frames


browser.frame(:id, "Frame").file_field(:id, "test1").exists?
browser.frame(:id, "Frame").file_field(:index, 1).exists?


Iterating thru file fields in frames


count =0
browser.frame(:id, "Frame").file_fields.each do |ff|
....
count+=1
end


OR


browser.file_fields.each { |f| puts f.to_s }


Goto the nth file field on the page

browser.file_fields[n].to_s


Print the details of the file field.


browser.file_fields(:index, n).to_s


This will list type, id, name, value and whether the file field is disabled.

Friday, October 16, 2009

Watir Tutorial: Web links test automation

In this tutorial, we will explore various options to verify and automate web links testing. I tried to add code examples/samples for both HTML source for links and Watir source code to test those scenarios. If you are looking for something specific not covered in this tutorial, drop me an email at shrikantwagh@sbcglobal.net.

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