My introduction to Ruby has been via WATiR. Around 2 years ago Howard introduced me to WATiR whilst working at Microsoft SDC on a project for the BBC. We were both very excited and at the time we were using freeride and Hansleman's WatirMaker to create simple navigation tests of the web site to help increase the time it takes for a developer to get to a certain part of a large enterprise web site.
Well things in this space have been moving at a rather fast pace. The introduction of the WATiR web recorder which unfortunately is nolonger available for download and the release of Ruby In Steel by Sapphire Steel allowing the Microsoft developer to build Ruby app's without moving from the comfort of your Visual Studio surroundings. However, there are some issues when moving from the freeride to the Ruby In Steel as there are some language inconsistencies which can be painful. So I thought that it was time for a change.... So I have ditched the Ruby In Steel and dived into Instant Rails using RadRails as my IDE of choice. RadRails is an absolute joy to use its lightweight and intuitive to use.
So the WatirMaker used in conjunction with RadRails allows for a great experience for getting to grips with the WATiR API.. The whole experience is great when you want to quickly navigate to a page and then manually test the functionality you have implemented, but what happens when you want to test that the correct information is being displayed on the page ??
Now this is when it starts to get really exciting for testers and developers. WATiR provides methods to allow you to access HTML elements by ID and then drill into these elements. For example say you have a <span> element with an image that has a unique id buried in its name and some alt text that includes a price and some textual information. I can then setup some variables in my WATiR script and stash the values associated with the <span> and the alt text. More than likely when you select the element, the unique id is stashed away in the query string form a request for a page where the content should be. By doing this I can now interrogate the query string (Ruby has too many string operators to list here...) and validate that the id in the query string is correct and when the page loads I can examine another <span> on the destination page and examine the text validating that the expected and actual results along with the image. Great !!!. Why well I have lots of these on a page and because the tests are dynamic this means that I can use the sane test script to test different permutations.
So all was well in the land of WATiR and the team was all happy until the login pages were created and WATiR test needed to be created to test that the ASP.Net validators were working correctly. ASP.Net validators are controlled by CSS and hence always present on the page so the previous mechanism for accessing text does not work as the 1.4.1 version does not understand custom attributes. Enter WATiR 1.5.1 which includes a new method called attribute_value allowing you to access a <span> element and then interrogate the custom attribute. Allowing you to write a fine grained test and validate different valid and invalid inputs into the required fields. So peace was found again in WATiR world....
I have added the code for the custom attributes as an attachment. The example code for the span and alt text can be found below.
In the next post I will dig into some best practices for designing and managing WATiR test scripts.
for i in 0..0
#puts 'this gets the image for the 1st name'
image = @IE0.image( :id, itemimage [ i ])
imagesrc = image.src
puts '--------------------------------------------------'
alt = image.alt
#this gets the text for the title and name
assert(alt)
#puts alt
#parse the alt text so that we can suck out the name and title text
alt = alt.split(' - ')
#the name title
nameTitle = alt[1]
#puts nameTitle
#the title name
titleName = alt[0]
#puts titleName
puts imagesrc
#puts '1st image'
if imagesrc.include? "insert_string_token_here"
croppedImageSource3 = imagesrc.slice!(/names.*.j/)
#puts croppedImageSource3
croppedImageSource2 = croppedImageSource3.chop
#puts croppedImageSource2
croppedImageSource1 = croppedImageSource2.chop
#puts croppedImageSource1
croppedImageSource = croppedImageSource1.chop
#puts croppedImageSource
else
croppedImageSource = imagesrc.slice!(/Images.*.JPG/)
croppedImageSource
end
puts croppedImageSource
#click the image and load up the product page
image.click
#puts '2nd image'
#get the src of the 2nd image
nameInfoIm = @IE0.image( :id, @productpage.productmainimage)
nameInfoImage = nameInfoIm.src
#puts nameInfoImage
if nameInfoImage.include? "musicnet"
croppednameSource3 = nameInfoImage.slice!(/names.*.j/)
#puts croppednameSource3
croppednameSource2 = croppednameSource3.chop
#puts croppednameSource2
croppednameSource1 = croppednameSource2.chop
#puts croppednameSource1
croppednameSource = croppednameSource1.chop
#puts croppednameSource
else
croppednameSource = nameInfoImage.slice!(/Images.*.JPG/)
#puts croppednameSource
end
assert_equal(croppedImageSource, croppednameSource)
puts 'matched images'
#puts croppednameSource
#puts 'match title name'
prodpagetitle = @IE0.link( :id, @productpage.producttitle)
assert(prodpagetitle)
prodtitle = prodpagetitle.text()
puts prodtitle
assert_equal(prodtitle, titleName)
puts 'matched title name'
#puts 'match name name'
prodpagename = @IE0.span( :id, "ctl00_contentPlaceHolder_nameInfoHeader_nameInfoDataList_ctl00_nameName")
assert(prodpagename)
prodname = prodpagename.text()
assert_equal(prodname, nameTitle)
@IE0.link( :id, @header.homeTab).click
#@IE0.back
end
@IE0.close