Still Life

A Series of Mental Snapshots

Posts Tagged ‘Watir’

Taking action on JavaScript Popups with Ruby/Watir in Firefox and Internet Explorer

Posted by Steve on July 12, 2010

A long time ago I did a post on catching JavaScript popups, but have a much better way of catching them.

These JavaScript popups cause trouble as they interrupt the page from fully loading, causing Watir to wait (as the page is waiting), which means the next command in your script will never be reached. Previous work arounds to this were to use watirs built in click_no_wait, but I have that to be extremely temperamental and did not always work depending on which element the click was being performed on.

The new and improved method is to have a completely separate process that runs in the background and is continually checking for JavaScript pop ups. AutoIt commands are used to first locate the pop-up and then depending on what text or title is present in the pop up and different action can be performed on it. Unfortunately the same code cannot be used for both IE and FF due to the fact that the AutoIt controls cannot perform the same actions on IE pop-ups as it can on FF pop-ups.  I have included the code for both below:

clickPopupsIE.rb

require 'win32ole'
begin
    autoit WIN32OLE.new('AutoItX3.Control')
    loop do
       autoit.ControlClick("Windows Internet Explorer",'', 'OK')
       autoit.ControlClick("Security Information",'', '&Yes')
       autoit.ControlClick("Security Alert",'', '&Yes')
       autoit.ControlClick("Security Warning",'', 'Yes')
       autoit.ControlClick("Message from webpage",'', 'OK')
       sleep 1
    end
rescue Exception > e
    puts e
end

clickPopupsFF.rb

require 'win32ole'
websiteName = "w3schools.com"
begin
    autoit = WIN32OLE.new('AutoItX3.Control')
    loop do
       autoit.winActivate("The page at http://#{websiteName} says:")
       autoit.Send("{ENTER}") if(autoit.WinWait("The page at http://#{websiteName} says:",'',2) == 1)
    end
rescue Exception => e
    puts e
end

These two scripts can then be called from any of your other Watir scripts using the following two functions scripts:

require 'win32/process'
def callPopupKillerFF
    $pid = Process.create(:app_name => 'ruby clickPopupsFF.rb', :creation_flags => Process::DETACHED_PROCESS).process_id
end

def callPopupKillerIE
    $pid = Process.create(:app_name => 'ruby clickPopupsIE.rb', :creation_flags => Process::DETACHED_PROCESS).process_id
end

def killPopupKiller
    Process.kill(9,$pid)
end

As you can see above you do need to require one more ruby gem, ‘win32/process’, this is used to run the popup clicker as a separate process that runs in the background. Once you have those functions in place you can simply call:

callPopupKillerIE #Starts the IE popup killer
#Some watir code that results in a popup#
killPopupKiller #Kills the popup killer process, so that you do not end up with 5 of them running!

Well there you have it, a robust and effective popup killer for both IE and FF. If you have any questions let me know!

–Steve

Advertisements

Posted in IE Automation/Watir/Ruby, Testing | Tagged: , , , , , , , , , , , , , , | 12 Comments »

4th Year Project: Scraping Pages!

Posted by Steve on October 27, 2008

The first step to my project was to get the main information that I was going to display on my web page. As mentioned in the previous post, the two pages I want information from are the schedule of classes and the undergraduate calendar. I used Ruby to accomplish this, if you want more info on scraping with Ruby drop me a comment.

The information I needed to pull from the schedule of classes was the basics for each class including the department, the class name and number, the enrollment cap and total, what day/time it was schedule for and the instructor.  A quick inspection of the page demonstrated that it was very poorly design, no information had tagged IDs or anything useful in that way. After a little digging and with the help of this command:

ie.show_tables

I was able to ascertain that the schedule of classes page had 2 tables, with the second table containing ALL of the desired information on the page; now if this isn’t bad web design I don’t know what is!

Alright I was getting somewhere, the information is in a table, now it was just a matter of finding the right logic to catch the information I wanted. Again after a little investigation I found that the department name would appear in the first column of a given row, and after I caught the department name there was a concrete pattern to where the rest of the information I wanted was placed. The logic I ended up using (and excuse the lack of indenting, wordpress is being a pain) was:


size.times do

if(classArray[count][0] == dept)

Write desired information to an excel form (for the time being)

end

count = count+1 #Increase count so that it checks the next row

end

So this basically iterates through each department page and pulls down all of the information I need! I apply very similar logic to pull down all of the information from the Undergraduate calendar. As things stand I have all of the current information available to students in an excel sheet, this is only temporary, I will shortly be putting it in to a MySQL database so that I could start putting it with the Django framework!

As far as next steps, well that would be to start learning Python/Django and getting a development environment up to play with, should be fun!

–Steve

Posted in Fourth Year Project | Tagged: , , , , , , , , , , , | Leave a Comment »

IE Automation with Ruby: Catching pop-up Windows

Posted by Steve on September 29, 2008

Firstly I need to define what I mean my pop-up Windows. The pop-up windows that cause trouble are not internet explorer based windows, they are actually ‘Windows’ windows, (sorry if thats confusing). The ones that I am referring to are the ones that come up when, for example, you click on a download link. These are inherently a pain because of the fact that they are not IE windows. Luckily there is a pretty simple work around that i have come up with.

Ruby has access to the WIN32OLE library , which is basically like an API for windows applications. What you can do is use this library  to catch these pop up windows. Below is the code that you’ll need to run in a Ruby script:

require ‘win32ole’                                #Loads the win32ole library
wsh = WIN32OLE.new(Wscript.Shell) #For more info click here
wsh.AppActivate(‘Connect’)                #Focuses on a given application based on its Title

At this point you can manipulate the window, for example, with a SendKeys command:

wsh.SendKeys(“%{F4}”) #This would close the program with Alt-F4

This clearly has it’s limitations because during this time you cannot be doing things on your computer, because the AppActivate would fail. I am still looking for a lower level at which I can address this problem.

Now everyone likes to see code at work, so I have written a quick script that goes to the Notepad++ downloads page, clicks the download link, and then closes the pop-up download window. As a quick side note, if you do not already use notepad++ I highly recommend it!

require ‘win32ole’
require ‘watir’

wsh = WIN32OLE.new(‘Wscript.Shell’)

ie= Watir::IE.new
ie.goto(“http://notepad-plus.sourceforge.net/uk/site.htm”)
ie.frame(:name, “index”).link(:text, “Download”).click #Good example of how to execute a link in a Frame
ie.frame(:name, “index”).link(:text, “Download Notepad++ executable files”).click

sleep 20 #need to wait for source forge to load it is slow
ie1 = Watir::IE.attach(:title, /Source/)
ie1.link(:id, “showfiles_download_file_pkg0_1rel0_2”).click

wsh.AppActivate(“File Download – Security Warning”) #Focuses on the pop up window
wsh.SendKeys(“%{F4}”) #Sends the alt-F4 command to the window to close it

Watir::IE.close_all #Closes all open IE windows

So if you are interested try the script out, I did try it out myself to make sure it worked, but let me know if you have any difficulties!

–Steve

Posted in IE Automation/Watir/Ruby, Testing | Tagged: , , , , , , , , , , , | 3 Comments »

Watir: Dealing with JavaScript and Frames

Posted by Steve on July 22, 2008

I came across this difficulty when I was trying to automate a webpage that was primarily built in Javascript; this webpage also always had the same href (or web address) and also was built with many frames.  The problem that I ran into was that I could find the links with the IE developer toolbar to get their ids, but whenever I tried to access them, I could not, I was given an error message saying that they did not exist. At this point in time I thought the issue I had was with javascript, but I was incorrect!

A lot of hunting on the interwebs led me, ironically, back to the watir main documentation, where I discovered that my issue was really with frames! When a website has frames, you need to specify what frame the link is in to actually access it, for example:
ie.frame(“main”).link(:id,”UW_CO_JOBTITLE_HL$”).click

Or in general terms:
ie.frame(“FRAMENAME”).link(:id, “LINKID”).click

And there we have, you can now access links that are in frames, hopefully this saves someone all of the hunting that I had to do! This is also a really good example of how difficult it is to find a solution to something when you are not sure what the problem was; I thought the problem was with JavaScript, so I was searching for that, but it was in fact as stated above with the frames!

–Steve

Posted in IE Automation/Watir/Ruby | Tagged: , , , , , , , | 2 Comments »

Driving IE With Watir and Ruby – An Automation Adventure

Posted by Steve on July 14, 2008

In my current employment I will be building an automation framework for a web application that we have. The front door approach to this is to simulate a user, this means the need of a way to drive Internet Explorer. This is where Watir comes in.

I figured I could not explain watir as succinctly as those who developed it, therefore here is a little blurb from the ruby web page (http://wtr.rubyforge.org/):

Watir is a simple open-source library for automating web browsers. It allows you to write tests that are easy to read and easy to maintain. It is optimized for simplicity and flexibility.

Watir drives browsers the same way people do. It clicks links, fills in forms, presses buttons. Watir also checks results, such as whether expected text appears on the page.

Watir is a Ruby library that works with Internet Explorer on Windows. Watir is currently being ported to support Firefox and Safari.

Like other programming languages, Ruby gives you the power to connect to databases, read data files, export XML and structure your code into reusable libraries. Unlike other programing languages, Ruby is concise and often a joy to read.

Watir stands for “Web Application Testing in Ruby”. It is pronounced water.

So there you have Watir is the exact tool that I will be needing. The first thing to get up and running is to install ruby, which is easily done from the ruby home page. Once ruby is installed, the Watir ‘gem’ can be installed form a command line prompt by typing: “gem install watir” . You will get a prompt at the end telling you that the install was succesful. You will need one final tool,the internet developer tool bar. This is a tool bar that gets installed in IE that allows you to inspect almost every element on a web page, this is not only useful but close to necessary for the scripting work.

Alright, ruby and watir are in and we’re ready to go. There are two ways to get going, an interactive way and a scripting way. The interactive ruby can be started from the command line by entering: irb . The second way is by  creating a *.rb file and then running that file from the command line by entering: ruby *.rb . I personally find that I use the irb interactive form to test things, which I then put into the *.rb scripts.

Below is a very simple watir script that will open an IE browser, navigate to google, click a link and then close the browser:

require ‘watir’

ie = Watir::IE.new
ie.goto(“www.google.ca”)
ie.button(:name, “btnG”).click
ie.close

The first line tells ruby that we are using the watir gem. We then set a variable, ie, to a new instance of an internet explorer browser the .goto command then navigates to a given web page. The syntax for manipulating an element on a page is as follows, ie.ElementType(:Attribute, ValueOfAttribute).action . Finally ie.close, closes the browser. This may seem a little confusing at first, but there of course is a good cheat sheet and that is located here: http://pettichord.com/watirtutorial/docs/watir_cheat_sheet/WTR/Cheat%20Sheet.html

There we have it, a brief introduction to watir, enough to get you up and going. I will be posting more as I learn, I have a few things to back post as I have already begun my automation framework. If you have any questions about ruby/watir, feel free to ask and I will do my best to help you out.

–Steve

Was this quick tutorial useful? Is there anyway that I could improve the way the information is presented? If so I’d love to know, drop me a comment and I’ll try to incorporated it into future posts!

Posted in IE Automation/Watir/Ruby | Tagged: , , , , , , , , , | 1 Comment »