Testing Using PERL

PERL:Practical Extraction and Reporting Language.

There are lot many things you can do with PERL while testing your application, 

PERL is great language to play with (my personal choice), trust me you will again fall in love with your work once you start working with PERL.

In this section, tester can find how effectively and efficiently they can use perl for their testing their applications.

Please see, i am new blogger and my only purpose here is to make Testing more intresting and
All things i am putting here is based on the things i worked on.

I will keep updating  this blogs with more things on PERL .

If you are testing a client server application, You might be interacting with server logs, to check what request client is sending and what response server is sending back to client.

Some times logs are too big to go through all of it and checking for some special message.

You can obviously use some Editor's search option but that  will be tedious job to do.

Here, i will first show you how to find a particular pattern/text in log file.

*********************************************************
#PERL script to find a text/Pattern in log file


$Han = "handler";

open(Han,"logs") or die "Unable to open logfile:$!\n";
#Here logs is the name of file you want to open.
while(<Han>){
print if /\bMSG\b/i;
# searching for text "MSG"  in our log file.
}
close(Han);
************************************************************
copy -paste the above code in text file and name it as per your choice(i am giving search.pl) with extension for file as ".pl"
The output you will get will show  all lines containing text "MSG". 

Now if you want to modify it more, so that your output should show how many times that text is present , you can use like this:

******************************************************************
#PERL script to search for a text/Pattern and print the number of occurrences of text/pattern


my $val = "filename.trace";
chomp ($val);
my $count=0;
open (FilHan, "$val") || die "wrong filename";
while ($val = <FilHan>)
{
  while ($val =~ /\bError\b/ig)
  {
        ++$count;
  }
}
print "Total instances present: $count\n\n";
close (FilHan);



Output will be:
Total instances present: 489
****************************************************************


Lets see how we can test  webpage using PERL script, written below is the script that will open a webpage and insert your details.


#PERL script to open webpage and fill the login fields
 use Win32::IEAutomation;
        # Creating new instance of Internet Explorer
        my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1);
        # Site navigation
        $ie->gotoURL('http://www.gmail.com');
        $ie->getTextBox('name:', "Email")->SetValue("give_your_username_here");
        $ie->getTextBox('name:', "Passwd")->SetValue("give_your_password_here");
        # Finding button and clicking it
        $ie->getButton('name:', "signIn")->Click;
  *********************************************************


Sometimes, during testing you need to get information from Server,It will be tedious to open putty and Telnet the server every time and execute the command , script below will help in executing the command onto server and get response,




**********************************************************
#PERL script to telnet a server and execute the command
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die',
Prompt => '/\$ $/i');
$telnet->open('www.provideiphere.com');
$telnet->login('username', 'password');
print $telnet->cmd('who');


**********************************************************








Lets see how to compare two huge files and find out
1).Common between two files
2).Unique in both files.


**********************************************************
#PERL script to find common items between two files and also those which are present in one file only.
@both = ();

@second = ();
open FH1, "firstfilename.dat" or die $!;
while(<FH1>)
{
# each line has become a key
$first{$_} = undef;
}
close FH1;
open FH2, "secondfilename.dat" or die $!;
while(<FH2>)
{
if(exists($first{$_}))
{
push(@both, $_);
delete $first{$_};
}
else
{
push(@second, $_);
}
}
close FH2;
#$, = "\n";
print "in Both\n";
print @both;
print "only in second file\n";
print @second;
print "only in first file\n";
print keys(%first);

**********************************************************




Please feel free to post your comments for any doubts.


happy testing :)


Keep watching for more stuff on testing with PERL.    
     





Comments

Popular Posts