Tuesday, October 19, 2010

How to print time stamp in Watir and calculate time difference

So you want to print the time at which a particular event occurred in your script to the output console/file. Here is how you can do it:

puts "Cannot publish occurred at Time Stamp: #{Time.now}"

To find the time difference in Watir all you have to do is define a variable to read the current time:

timeStart=Time.now

And then print out the difference after storing it in another variable

elapsed= Time.now - timeStart
puts "Elapsed[#{elapsed}]"

Easy, aint it!

~joviano_dias@hotmail.com

Monday, October 18, 2010

Watir for load testing? .. Or JMeter instead

Recently someone asked me if load testing was possible in Watir. I thought about it and envisaged the possibility of doing this, then the thought crossed my mind whether watir is really ideal for Load testing... Lets see,

- Ruby does have great multithreading capabilities which should make parallel processing easy, but then how will it compete with giants in load testing like Rational Performance Tester, Load Runner and VSTS. Well, it just would not be able to at this nascent stage since the others are commercially supported and Watir isn't and the development is solely in the hands of open source development.


- Watir does not have a result reporting mechanism like the other tools mentioned with the fancy graphs and the out of the box support for ramp up of users, creating multiple threads, latency reporting and throughput tests etc.

- Watir is for functional testing and not a load testing tool and the development emphasis is on getting things right for functional testing.

- I then did a google search and one interesting thread I got was at http://www.ruby-forum.com/topic/183983. If the above doesn't convince you. Perhaps this will ;)


Conclusively, Watir is just not meant for load testing , just like we need a truck not a car for carrying heavy loads, lame example I know :P

Okay so what do I suggest??
I suggested that he work with JMeter, no licensing issues, out of the box load testing. It worked great. Ill cover my demo on it in another post perhaps...

~joviano_dias@hotmail.com

Friday, July 30, 2010

Solved (eval):3:in `invoke': unknown property or method `readOnly' (WIN32OLERuntimeError)

I used to run my Waitr script in order to set the text field with a value and get the error:

(eval):3:in `invoke': unknown property or method `readOnly' (WIN32OLERuntimeError)

The problem had a simple solution.

These were the two elements in my html source.
<td valign="middle" align="center" id="textValue">
<input type="text" name="textValue" id="textValue">

I wanted to set the textfield to a specific value and my line in my Watir code looked like this
ie.text_field(:id,"textValue").set("awww")

This resulted in Watir trying to set the first element with the value as both had the same id(it had a recognition problem), which was not actually a textfield and threw the error:

(eval):3:in `invoke': unknown property or method `readOnly' (WIN32OLERuntimeError)
HRESULT error code:0x80020006
Unknown name.
from (eval):3:in `readonly?'

Thus I had to find another unique recognition parameter and instead of 'id', and used 'name' instead

Changed my code in Watir to
ie.text_field(:name,"textValue").set("haha")

Now it worked just fine!

~joviano_dias@hotmail.com

Tuesday, July 27, 2010

Watir timeout : handle browser hang/stuck while loading isuue with watir in IE

Watir: 1.4.1
Ruby: 1.8.6

I used to get frequent conditions wherein when my watir script is running and the browser would hang indefinitely and load painfully slowly when changing links using goto, when navigating to another page, when submitting a form...

The timeout functionality as supplied in  http://watirmelon.com/2009/03/26/timing-out-watir-with-timeouttimeout/ which was a great resource for this article and the base for it.

My custom code to check if the page is loaded and if not, redo the action:
browser_loaded=0
while (browser_loaded == 0)
begin
 browser_loaded=1
 Timeout::timeout(10) do
  browser.goto(“http://mysite.com”) # goto or click a link
 end
 rescue Timeout::Error => e
  puts “Page load timed out: #{e}”
  browser_loaded=0
  retry
 end
end

This works fantastic, only problem is that when there are many pages to load, this entire code has to be inserted which substantially increases the code writeup.

To counter this, I decided to make a function and call this block anytime I have to jump to a page.

So now whenever I have any submit action or page load during which a browser hang is expected, I call it using the following.
sureLoadLink(30){browser.goto("http://mysite/")} 

Note that '30' used here is the timeout. The part inside the curly braces above is the one that I am executing with failproof execution.

Here is the called function. yield is the place where the called block code is substituted.
def sureLoadLink(mytimeout) 
 browser_loaded=0
 while (browser_loaded == 0)
   begin
   browser_loaded=1
   Timeout::timeout(mytimeout)  do
    yield
   end
   rescue Timeout::Error => e
    puts "Page load timed out: #{e}"
    browser_loaded=0
    retry
   end
 end
end

This worked great for page changed and navigation clicks.

However, when submitting forms and IE hanging with indefinite loading time when the form is being submitted, this had an issue. My problematic code which submitted a form looked like this...
#go to form
browser.text_field(:id,'discussion_subject').value="Subject"
browser.text_field(:id,'discussion_post').value="Post"
sureLoadLink(30){ browser.button(:id, 'createbutton').click }

The problem was that Watir would fill the form and prepare it for being submitted, but when the submission failed due to IE loading getting stuck, the sureLoadLink function would try to resubmit it. This would fail and throw a object not found error as no the page has moved on partially and the button is gone out of the scan area for Watir!

In order to handle this, I used the concept of a transaction. Redoing every action required to make a commit. Thus making sure that if submission failed, all required steps are repeated leading to form submit and not only the form submit action itself. This worked and the code snippet looked like this:
sureLoadLink(30){
 #go to form
 browser.text_field(:id,'discussion_subject').value="Subject"
 browser.text_field(:id,'discussion_post').value="Post"
 browser.button(:id, 'createbutton').click 
}

Thus managed to solve all issues! yay...

A final piece of working code would look like this:

sureLoadLink(30){
 #browser.goto("http://mysite")
 browser.text_field(:id,'content').value="Comment 10"
 browser.form(:id, "comment_form").submit
}


def sureLoadLink(mytimeout)
 browser_loaded=0
 while (browser_loaded == 0)
 begin
  browser_loaded=1
  Timeout::timeout(mytimeout)  do
   yield
  end
  rescue Timeout::Error => e
   puts "Page load timed out: #{e}"
   browser_loaded=0
   retry
  end
 end
end

I would also suggest the following as it made my timeout errors far less frequent.
  1. Upgrade your watir and ruby installation to the latest version (Watir 1.6.5).
  2. Download and update IE 8 with the latest updates.
~joviano_dias@hotmail.com

Watir 1.6.5 gem download with ruby setup

Starting off with watir isnt all that simple especially if you cannot find the setup required.

 Here is a complete guide to setting up watir.

1. Download ruby-one-click setup from RubyForge , take care that the gem you downloaded supports this version of Ruby. You will find the gem and ruby setup compatibility information at http://watir.com/installation/

2. Download the stable watir gem. I downloaded rubyinstaller-1.8.6-p398 in order to work with my watir 1.6.5 gems . [Common watir, firewatir and Watir] as both were compatible. Earlier I had installed ruby 1.9.1 but it dint work with watir 1.6.5 . I got the error "msvcrt-ruby18.dll not found" as the version required was much lesser.

3. Install ruby and keep all the gems in one folder.
4. Open command prompt[cmd from run] and then cd to the folder where the gems are located
5. Here run the commands:


D:\mygems> gem install commonwatir-1.6.5.gem
D:\mygems> gem install watir-1.6.5.gem
D:\mygems> gem install firewatir-1.6.5.gem


3. If you are behind a proxy, you will have issues finding the dependent gems. This will result in the error http://rubygems.org/ does not appear to be a repository

To resolve this issue, you will need proxy bypass until you have it install. If you have a machine that you can use as a proxy, you can specify it in command as:
SET HTTP_PROXY=http://user:secret@ip-addess:8080

~joviano_dias@hotmail.com