Still Life

A Series of Mental Snapshots

Archive for the ‘Scripting’ Category

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

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

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 »

Basic Scripting of VM operations on ESX Server Using vmware-cmd

Posted by Steve on June 12, 2008

I’m writing a little more about ESX scripting because I had a lot of trouble tracking down some of this information, and if it had been all in one spot I would have saved a lot of time and trouble! I will be going over a few basic commands, as well as a few errors that I found. All of this was done on a ESX 3.5 machine.

This all started recently when I needed to reproduce a bug to show that it actually was an ESX specific issue. I learned quite a bit about ESX scripting from this experience, and hopefully some of the knowledge I am about to impart upon the vast interwebs helps someone out!

Now in starting the main command, that can be entered from anywhere in the command, that you will be using is vmware-cmd. Entering ‘vmware-cmd‘ will provide the following output:

Usage: /usr/bin/vmware-cmd <options> <vm-cfg-path> <vm-action> <arguments>
/usr/bin/vmware-cmd -s <options> <server-action> <arguments>

Options:
Connection Options:
-H <host> specifies an alternative host (if set, -U and -P must also be set)
-O <port> specifies an alternative port
-U <username> specifies a user
-P <password> specifies a password
General Options:
-h More detailed help.
-q Quiet. Minimal output
-v Verbose.

Server Operations:
/usr/bin/vmware-cmd -l
/usr/bin/vmware-cmd -s register <config_file_path>
/usr/bin/vmware-cmd -s unregister <config_file_path>
/usr/bin/vmware-cmd -s getresource <variable>
/usr/bin/vmware-cmd -s setresource <variable> <value>

VM Operations:
/usr/bin/vmware-cmd <cfg> getconnectedusers
/usr/bin/vmware-cmd <cfg> getstate
/usr/bin/vmware-cmd <cfg> start <powerop_mode>
/usr/bin/vmware-cmd <cfg> stop <powerop_mode>
/usr/bin/vmware-cmd <cfg> reset <powerop_mode>
/usr/bin/vmware-cmd <cfg> suspend <powerop_mode>
/usr/bin/vmware-cmd <cfg> setconfig <variable> <value>
/usr/bin/vmware-cmd <cfg> getconfig <variable>
/usr/bin/vmware-cmd <cfg> setguestinfo <variable> <value>
/usr/bin/vmware-cmd <cfg> getguestinfo <variable>
/usr/bin/vmware-cmd <cfg> getproductinfo <prodinfo>
/usr/bin/vmware-cmd <cfg> connectdevice <device_name>
/usr/bin/vmware-cmd <cfg> disconnectdevice <device_name>
/usr/bin/vmware-cmd <cfg> getconfigfile
/usr/bin/vmware-cmd <cfg> getheartbeat
/usr/bin/vmware-cmd <cfg> gettoolslastactive
/usr/bin/vmware-cmd <cfg> getresource <variable>
/usr/bin/vmware-cmd <cfg> setresource <variable> <value>
/usr/bin/vmware-cmd <cfg> hassnapshot
/usr/bin/vmware-cmd <cfg> createsnapshot <name> <description> <quiesce> <memory>
/usr/bin/vmware-cmd <cfg> revertsnapshot
/usr/bin/vmware-cmd <cfg> removesnapshots
/usr/bin/vmware-cmd <cfg> answer

These are all the different commands you can run with vmware-cmd. This in itself is only somewhat useful because for the more detailed commands, such as getconfig and setconfig, it does not tell you what variables to enter. I will go over a few of the ones that I have played with next.

The first useful command is:
vmware-cmd -l

This lists out the location of all of the VMs’ .vmx files on the ESX server. This is very useful as you will need this when you run any of the vmware-cmd commands. As stated above the syntax for the command is:
vmware-cmd <options> <vm-cfg-path> <vm-action> <arguments>

Now onto a few of the commands, there are the ones that are pretty self explanatory,
Remove All VM Snapshots: vmware-cmd <cfg> removesnapshots
Check if VM has any Snapshots: vmware-cmd <cfg> hassnapshot
Create A Snapshot[note everything but the name is optional]: vmware-cmd <cfg> createsnapshot <nameofsnapshot> <descriptionofsnapshot> <quiesce> <memory>
Revert to the previous snapshot: vmware-cmd <cfg> revertsnapshot
Start a VM: vmware-cmd <cfg> start <powerop_mode>
Get the state of the VM: vmware-cmd <cfg> getstate

Those are some of the simple useful ones, now here are a few that are a little finicky.
Stopping a VM: vmware-cmd <cfg> stop <powerop_mode>
Resetting a VM: vmware-cmd <cfg> reset <powerop_mode>

Here the <powerop_mode> is the tricky option. There are three options:
Soft: VMware tools need to be installed and running, therefore there are many cases when this will not work!
Hard: Performs the operation no matter what
Trysoft: Trys a soft and if that fails proceeds to a Hard. This is the best one in my opinion

get/setconfig:
vmware-cmd <cfg> setconfig <variable> <value>
vmware-cmd <cfg> getconfig <variable>

Setting or getting a configuration is a bit confusing at first because it asks for a variable, where is this variable coming from!?!? Luckily its actually pretty simple, you can set/getconfig on any of the settings in the .vmx file. There unfortunately is an issue with ESX 3.x ; some of the parameters that you set, only get set if you reset the hostD service by entering: service mgmt-vmware restart . This really is only a workaround as running this command will reset all of the connections that the ESX server has with the VMs, and it does take some time for it to come back up.

There you have it, this should be enough info to get you started on some ESX scripting. If you have any questions on how some of these commands work feel free to drop me a comment, and I’ll do my best to help you out. Below are also a few resources that I found helpful, but keep in mind if you are running ESX 3.x some things may work differently!

Useful References
Guide for driving the ESX server via Command line – http://www.rtfm-ed.eu/docs/vmwdocs/esx3.x-vc2.x-serviceconsole-guide.pdf
VMware Scripting API – http://wiki.eclab.byu.edu/plugin/attachments/ScriptingAPI/Scripting_API_215.pdf

Posted in ESX Scripting | Tagged: , , , , , , , , , | 10 Comments »

Taking Screenshots From VMWare ESX Command line using vmdumper

Posted by Steve on June 11, 2008

VMdumper is something that apparently does not exist, well according to google at least. But fortunately,  I have learned of, and now am sharing some of the usefulness of this little tool in VMware.

The main thing that I have used it for is to take a .png screenshot of a virtual machine on that ESX server. This has a whole bevy of usefulness. Specifically for me, I was trying to catch a specific error that was occurring when my VMs were booting in a specific way and that left no trace if itself in any log files, the only way to know it happens is to look at the screen, thus a screenshot was in order!

The vmdumper command can be accessed on your ESX server by connecting via SSH and navigating to the following directory:
/usr/lib/vmware/bin/

From here you can run ./vmdumper which will provide the following output:
./vmdumper: [options] <world id|–listVM> <unsync|sync|vmx|vmx_force|samples_on|samples_off|nmi|screenshot|backtrace>
-f: ignore vsi version check
-h: print friendly help message
-l: print information about running VMs

These are the options available to you using this VMdumper tool! So moving on, you know where the tool is, and you want to take a screenshot! To take a screen shot, you need to know the VMID. The VMID is unfortunately dynamic, but have no fear another command is here. The vmdumper will list all of the information you need with the following command:
/usr/lib/vmware/bin/vmdumper –listVM

This will list out all of the VMs that are on your ESX server in an uglyish format, but it is pretty simple to extract the desired information, that is the VMID with the following command:
VMID=`/usr/lib/vmware/bin/vmdumper –listVM | grep VMsDisplayName | awk ‘/vmid/ {print $1}’ | awk -F= ‘ {print $2}’`

Basically what is happening is that this command is that the list of VMS is piped out and the grep command finds the line that has the desired VMID with the use of the VM’s display name. The first awk command pulls out the whole vmid filed, and the second awk pulls just the vmid number, and finally it is assigned to the variable VMID!

Now the screenshot command can be done from any directory, you just need to execute the following command:
/usr/lib/vmware/bin/vmdumper VMID screenshot

The screenshot will be deposited where that VMs .vmx file is located and will be labeled vmdumper.png!

There you have it, a screenshot of a running VM from the command line that you can run from a bash shell script! One final time here are the commands you’ll need to run.
VMID=`/usr/lib/vmware/bin/vmdumper –listVM | grep ${VMS[counter]} | awk ‘/vmid/ {print $1}’ | awk -F= ‘ {print $2}’` #Gets the VMID
/usr/lib/vmware/bin/vmdumper $VMID screenshot #take the screenshot

Hopefully this helps someone out, I know it made my day a lot easier!

–Steve

Post Script:
This is the first time I have posted a sort of How To, or purely information related post, feel free to drop me a line on the formatting or if there is any info that should be included that is not!

Posted in ESX Scripting | Tagged: , , , , , , , , , | 2 Comments »