Showing posts with label watir. Show all posts
Showing posts with label watir. Show all posts

Monday, February 16, 2009

An ingenious solution for handling Javascript popups in Watir

So I've been struggling to figure out how to elegantly and properly handle popups on Watir. I've read and tried all five solutions listed on http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups but still couldn't find one that works well for the version of Ruby/Watir I am using.

I'm using Ruby 1.8.6 and Watir 1.6.2, BTW.

Then I came across a blog post that proposes to override the javascript alert and confirm functions. Read the whole thread from google groups here.

I was stoked !!! Even more so that it worked beautifully.

What I did was to encapsulate it into a def which accepts the IE instance created by watir.


def replace_js_popups(ie)
ie.document.parentWindow.execScript("window.confirm=function(){return true}");
ie.document.parentWindow.execScript("window.alert=function(){return true}");
end


Now, right after going to a page, I would call on "replace_js_popups" to override both confirm and alert, like so.


ie = Watir::IE.new
ie.goto(url_with_popup_here)
replace_js_popup


Of course, this is not a fool proof solution. By replacing the alert and confirm functions with a function that always returns true, you will need to always expect the popups to return a value of true in your automated tests.

Monday, January 12, 2009

More Watir ....

A few blog posts ago I narrated how I installed Ruby and Watir on Windows and posted my first test script.

Getting started is usually the hardest part of the learning experience. The reason being that you don't know where or how to get started. Luckily there is a plethora of resources and lots of good samaritans on the web sharing their experiences and solutions on forums and on their personal blogs.

Today I'm going to enumerate the resources I found on the web in the order that helped me get started with Watir.

http://wtr.rubyforge.org/index.html
For an overview of what Watir is and possibly how and why you need Watir

http://www.ruby-lang.org/en/downloads/
Before Watir, you must install Ruby.

http://wiki.openqa.org/display/WTR/Tutorial
This tutorial has everything you need to know on how to install Watir and its prerequisites. It also contains everything you need to know to get the basics down and start writing simple test scripts.

http://www.rubycentral.com/book/

Pretty soon you'll need to learn more advanced stuff like objects and classes. The online book is a great resource for this.

http://rasta.rubyforge.org/index.html
At some point you may want to organize your test scripts. One framework I found that works is Rasta.

http://rubyonwindows.blogspot.com/2007/03/automating-excel-with-ruby-application.html
Some people may not like the restrictions imposed by Rasta and would like to setup their own framework with MS Excel. Ruby is perfectly capable of manipulating Excel. VB for application developers will feel at home with Ruby.