TechChase
Skip to content


Perl Automation – Win32::GuiTest part-I

This article describes how Perl Win32::GuiTest can be use for automated testing of GUI application. Win32::GuiTest module provides set of methods which is used for manipulation of window handles, controls, classes and more. It identifies windows by text, class and parent window attributes.

Before going to write automated testing scripts, we have familiar with the Perl Win32::GuiTest methods and how they work. Following are some examples of Perl Win32::GuiTest methods to find out window control.

1. GetActiveWindow

This method returns an active window attached to a thread, which created a window identified by function’s input parameter $hwnd.

  • Syntax

$hwnd = GetActiveWindow ($hwnd);

$hwnd – Is a window handle of any of windows belonging to a thread for which we want to find active window

2. GetChildWindows

This method returns window handles of all windows, which are children (direct or indirect i.e. grand-children, grand-grand-children etc) of a window with a handle given as an input parameter.

Syntax

@hwnds = GetChildWindows( $hwnd );

$hwnd – Is a handle of a window, for which we want to find out children.

For more information about Perl Win32::GuiTest methods my earlier post of Perl automation

Configuration testing setup

Before executing a test, we have to create the testing environment setup and install required software and utilities. As configuration setup part we have to install the following.

Required setup

  1. Active Perl – Active Perl to be installed in the machine where test to be run .It includes PPM (Perl Package manager), for installing Perl extension modules
  2. Win32::GuiTest – This Perl module for automation is used to create interface with the application. It provides a set of methods for manipulating window handles, controls and to simulate user input.
  3. WinSpy – This Utility helps to find out different properties of application.

Optional setup
Win32GuiTest.exe* – This is an optional GUI test utility. This includes script recording application has been written to use with Win32::GuiTest.
Software to store test data – Excel or any other utilities for test data storage.
Ddatabase – For better maintenance and performance of test , can use some open source database like MYSQL for test data storage.

Main features of using Perl for Automation

  • Perl is an open source software, no license cost to be spent which gives better ROI
  • When neither of the commercial test tools work for testing , Using Perl we can developed our customize script and perform our test
  • Perl has lots of built-in functions (Modules) and other functions like C , MSDN are easily interface with Perl
    Most important features of Perl is it is executable in all platforms and easily portable than other tools like Shell, PHP,Python, Ruby, Tcl Tools.
    Perl is capable to handle most advanced regular expression.
    Perl allows using all system calls including those necessary for networking
    Perl is significantly faster than other scripting languages
    Object-oriented techniques of Perl is very helpful for development

How PERL GUI automation works

The following section of this article will explain step by step approach for automating a well known GUI application . Here I will consider as an example of a very common application, which is very familiar to us, Google Talk.

Launch the Google Talk Messenger available in Win32 systems. Following figure displays the layout of the application.

Google Talk Messenger
Google Talk Messenger

In the following section explain how can automatically login to the messenger with a simple Perl program .

With the help of WinSpy++ utility we can find out the control of the application. Following figure shows the look of WinSpy++ full screen.

WinSpy++ Windows Control Spy
WinSpy++ Windows Control Spy

Now click on the “More” button marked as red and it will display as the following.

WinSpy++ Window Controls More Infomation

WinSpy++ Window Controls More Infomation

On the right side of WinSpy ,look the tree where we can see the our application name “googletalk.exe” .Now double click on “googletalk.exe” and it will show details about the messenger.See the window handle of the messenger, marked as red above.

Now ,We will learn how a simple Perl code find out the handle of the GUI application. Save and execute the following code and look the output of the program returned the messenger handle.

use Win32::GuiTest qw( FindWindowLike );
use strict;
my @Mhnds = FindWindowLike( undef, “^Google Talk” );
if( !@Mhnds ){
die “Cannot find Messenger Runningn”;
}else{
printf( “handle of Messenger is %xn”, $Mhnds[ 0 ] );
}

Save the above script as Goole_tk.pl and execute it . If Google Talk is running we get handle of the Messenger Window.

Perl Automation Script Execution

Perl Automation Script Execution

FindWindowLike returns a list of window handles. Window handle uniquely identifies a window in the system. In any time, there could be only one window with the given window handle, which is a long number.

Window handle of Gtalk Messenger is 10176 (Decimal). Note that it matches the window handle shown by WinSpy on the picture above. Remember – WinSpy shows it in hexadecimal notation. For perl, the default notations are decimal. Now,we have found the window handle , so we have control on the application through the handle.Now we have to move focus to the Messenger window and then send appropriate value to username and password text boxes.

SetForegroundWindow( $whnds[ 0 ] );

SendKeys( “Username” );

SendKeys(“{TAB}”);

SendKeys(“Password”);

SetForegroundWindow takes handle of the window as an argument and bring the window in foreground. The SendKeys function of Perl Win32::GuiTest sends thevalue to the testboxes on the the window.

Now set focus on the signin button and click on the same.Do not worry ,following Perl code does everything for us.

First , with the help of WinSpy++ ,find out the control id of the signin button and store in a variable.

my $ctl_signin_btn = 1; # Control handle of signin_button

SetForegroundWindow( $Mhnds[ 0 ] );

PushChildButton( $Mhnds[ 0 ], $ctl_signin_btn ); #

Following Perl script perform the login process to Gtalk Messenger automatically.

use Win32::GuiTest qw( FindWindowLike );
use strict;
my @Mhnds = FindWindowLike( undef, “^Google Talk” );
if( !@Mhnds ){
die “Cannot find Messenger Runningn”;
}else{
printf( “handle of Messenger is %xn”, $Mhnds[ 0 ] );

}

SetForegroundWindow( $whnds[ 0 ] );

SendKeys( “Username” );

SendKeys(“{TAB}”);

SendKeys(“Password”);

my $ctl_signin_btn = 1; # Control handle of signin_button

SetForegroundWindow( $Mhnds[ 0 ] );

PushChildButton( $Mhnds[ 0 ], $ctl_signin_btn ); #

Save and execute this script to signin into Gtalk Messenger.

This was a very simple and common example of Perl Automation.Yes , To automate GUI application with a simple Perl script,it is very simple and straight forward. Next we will learn to automate little complex application .For some application , we have to face some challenges to automate the application . In case of the toolbar , WinSpy can not idenfy the buttons or tab seperately , it shows whole toolbar as a single window. So we can’t follow the simple way to automate the application having toolbar . So in this case we have apply some more sophisticated techniques and brainstorming.Every problem have solution, my next article will bring the tips and tricks to automate more challenging application.

Digg This
Reddit This
Buzz This
Vote on DZone
Share on a Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Posted in Perl Automation.

Tagged with , , .

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Parneet says

    hi, I tried to execute the code where you try to find the file handle of google talk window…its throwing error…unrecognized character x93 after undef..
    please help



Some HTML is OK

or, reply to this post via trackback.