In this article common task function in any software test automation framework (like data driven, keyword driven and Hybrid frameworks) like Perl csv file reading ,Perl excel file reading,writing log file and finding handle of GUI application
1. Writing message to a log file
This script open a logfile and write log message for future to debug the script.
This Perl script concatenate the local time of the server on front of the log message.
#Main
#
log_entry(logfinename,”Message to write”);
#——————————————————————————————–
# Subroutine Name : log_entry
# Description : Writing log information to log file
#———————————————————————————————-
sub log_entry {
my ($log_file,$message) = (@_);
open (LOG_FH, “>>$log_file”) || die “ERROR : Unable to open file : $log_filen”;
print LOG_FH localtime().”: $messagen” || die “ERROR (log_entry): Writing to $log_file : $!”;
close (LOG_FH);
}
2.Finding GUI application handle
This Perl script finds out the handle of any GUI application and print the same
# main
#
my $H_app=get_gui_handle(applicationname);
print ” the handle of the application is : $H_appn”;
#————————————————————————————-
# Subroutine Name : get_gui_handle
# Description : Find out the handle of an application
#————————————————————————————-
sub get_gui_handle
{
use Win32::GuiTest qw(FindWindowLike);
my (@H_GUI_APP,$App_name) ;
$App_name= shift;
sleep 1;
@H_GUI_APP = FindWindowLike( undef, $App_name );
if( @H_GUI_APP)
{
print “n The GUI Application $App_name is running and handle is : $H_GUI_APP[0]n”;
}
else{
print “n The GUI Application $App_name is not running n”;
}
return @H_GUI_APP;
}
3.Reading data from a csv file
Following Perl script read value from a CSV file(C:csv_read.csv for example)
and concatenate the value to a string
# Main
read_csv(‘C:csv_read.csv’);
#————————————————————————————
# Subroutine Name : read_csv
# Description : Reading data from csv file
#————————————————————————————-
sub read_csv
{
my ($filename,$cnt,$line,@csvdata,$value_string);
$filename = shift;
chomp($filename);
open (FP,”<$filename”) || die “Could not open $filenamen” ;
#Script header line
<FP>;
while(<FP>)
{
$line = $_;
chomp($line);
#Split the records into fields
#
@csvdata = split(/,/,$_);
$cnt = @csvdata;
for( my $i = 0; $i < $cnt; $i++)
{
# Truncate white space from start and end of the value
$csvdata[$i]=~s/^s+//g;
$csvdata[$i]=~s/s+$//g;
if($value_string)
{
$value_string = $value_string .”, “.$csvdata[$i];
}
else
{
$value_string= .$csvdata[$i];
}
}
}
print “n the valuse read from the file is : $value_stringn”;
close(FP);
}
4.Perl script is to read excel file
Following Perl script read value from a excel file(“C:\personnel_info.xlsx” for example)
and print value
use Win32::OLE;
readexcel_data();
#—————————————————————————–
# Subroutine : create_excel_object
# Description : Interface to Excel and create objects and finally
# : return sheet object to calling program
#—————————————————————————–
sub create_excel_object
{
my ($excelfile,$excel_obj,$book_obj,$sheet_obj);
$excelfile = shift;
eval {$excel_obj = Win32::OLE->GetActiveObject(‘Excel.Application’)};
die “Excel not installed” if $@;
unless (defined $excel_obj)
{
$excel_obj = Win32::OLE->new(‘Excel.Application’, sub {$_[0]->Quit;})
or die “Oops, cannot start Excel”;
}
print ” nExcel Application Object : $excel_objn”;
print ” nthe excel file name is : $excelfilen”;
$book_obj = $excel_obj->Workbooks->Open($excelfile);
print ” nEXcel File Opened : $book_objn”;
$excel_obj->{Visible} = 0;
$sheet_obj = $book_obj->Worksheets(1);
return $sheet_obj;
}
#————————————————————————
# Subroutine : readexcel_data
# Description : Read and excel file and fetched data
#————————————————————————-
sub readexcel_data
{
my ($value,$sheet);
my $value = undef;
$sheet = create_excel_object(“C:\personnel_info.xlsx”);
my $LastRow = $sheet ->UsedRange->Rows->{‘Count’};
my $LastCol = $sheet ->UsedRange->Columns->{‘Count’};
print ” Number of rows in the sheet : $LastRown”;
print ” Number of columns in the sheet : $LastColn”;
foreach my $row (3..$LastRow)
{
undef $value;
foreach my $col (1..$LastCol)
{
$value=$sheet->Cells($row,$col)->{Value};
print (” Row number : $row : “, $value);
}
$Sheet = undef;
}
}
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.

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.
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.