|
|
|
Robin, I have include a text of a perl script I have written as an example of Automating the IE web browser. Henry Wasserman and myself were looking for a way to Automate the browser for testing since we are in the Quality Assurance group. After a lot of research we found perl was excellent at this. I figured this may be of interest to others. Please feel free to post this on your site if you feel that it will be useful. Enjoy Thank you Rob Marchetti
use strict;
use Win32::OLE qw(EVENTS);
#==============================================================================
# Automation Example - by Rob Marchetti/Henry Wasserman
# revised version 1.0b
#
# This is an example of how to perform automation using I.E.
# It launches I.E. goes to dice and ask for a job title to search for
# It does not actually save any jobs, this is purely to used as an example
# I have included some print statements for debugging
#
#==============================================================================
my @Documents;
my $notcomplete = 1;
my $URL = "http://www.dice.com/jobsearch/index.html"; #site you want to search
#
#
$Win32::OLE::Warn = 0;
# Launch IE aand Grap an instant of the I.E. browser
my $IEbrowser = Win32::OLE->new('InternetExplorer.Application'); #or die $!;
$IEbrowser->{left} = 0;
$IEbrowser->{top} = 0;
$IEbrowser->{height} = 725;
$IEbrowser->{width} = 650;
$IEbrowser->{menubar} = 0;
$IEbrowser->{toolbar} = 0;
$IEbrowser->{statusbar} = 1;
$IEbrowser->{visible} = 1;
$IEbrowser->navigate($URL); #site you want to search
#
#my $hnd = $IE->{HWND};
#print "Here's the Window handle: $hnd\n";
## Wait until IE is ready:#
WaitForBusy();
#Lookey here Objects!
my $IEDocument = $IEbrowser->{Document};
# Tell someone where your going to
print "\nConnecting to dice: " . $IEDocument->URL . "\n";
my @Docs = ();
push @Docs, $IEbrowser;
# Ask for a Job title to search on
print "\nEnter a Job Title to Search: ";
# Grab the job title from what the user types in ( no error checking yet)
my $sname = ;
# Enter the Job Title into the text box on Dice
SetEditBox(\@Docs, "query", "$sname");
#Click the submit button
ClickFormButton(\@Docs,"Search");
# the the user what you are searching for!
print "Searching Dice for $sname jobs......\n";
#Quit IE an Open Jobs.txt
print "Search complete!";
# Wait for IE to load and get the documents
sub WaitForBusy
while ($IEbrowser->{Busy} == 1) {
#print "Here's Busy: " . $IE->{Busy} . "\n";
sleep(0.4);
}
#print "Leaving WaitForBusy\n";
}
# Wait for a DocumentComplete
sub WaitForDocumentComplete { #$Document
my $i;
eval {
print "Cleaning out \@Documents\n";
@Documents = ();
#print "Shifting in new DOM\n";
my $IEDocument = shift @_;
while ($notcomplete) {
if (defined $IEDocument->all->length)
for (my $i=0; $i<$IEDocument->all->length; $i++)
print $IEDocument->all($i)->tagName . "\n";
if (defined $IEDocument->all($i))
$IEDocument->all($i)->tagName;
}
}
};
if ($@) {
if ($@ =~ /undefined/) {
print "Exception: $@ \n";
#print "Calling WaitForDocumentComplete\n";
WaitForDocumentComplete($IEDocument);
} else
#print "Exception: $@\n";
}
}
$notcomplete = 1;
#print "Here are the URLs:\n";
my $counter = 0;
for (@Documents) {
$counter++;
#print "Url " . $counter . ":" . $_->Document->URL . "\n";
}
return @Documents;
}
sub Event {
my ($Obj,$Event,@Args) = @_;
my $IEURL;
#print "Event triggered: '$Event'\n";
#if ($Event =~ /^StatusTextChange$/) {
#my $Status = shift @Args;
#print "$Status\n";
#}
if ($Event =~ /^DocumentComplete$/) {
$IEURL = $IEbrowser->Document->URL;
my $ArgsURL = @Args[0]->Document->URL;
#print "$IEURL\n";
#print "$ArgsURL\n";
if ($IEbrowser->ReadyState() == 4) {
#print "Browser is ready\n";
#print "Setting \$notcomplete to 0\n";
$notcomplete = 0;
}
#print "pushing arg " . @Args[0]->Document->URL . "\n";
push @Documents, shift @Args;
}
}
# Find the text box on the page and enter your search text
sub SetEditBox { #\@Docs, $name, $value
my $Documents = shift @_;
my $name = shift @_;
my $value = shift @_;
my $Document;
my $i;
my $j;
my $k;
foreach $Document (@$Documents) {
$IEDocument = $Document->{Document};
#print "Here's the URL inside SetEditBox: " . $Document->Document->URL . "\n";
for ($i=0; $i<$IEDocument->all->length; $i++) {
#print "Outer for $i " . $IEDocument->all($i)->tagName . "\n";
if ($IEDocument->all($i)->tagName =~ /^FORM$/) {
my $form = $IEDocument->all($i);
for ($j=0; $j < $form->all->length; $j++)
if ($form->all($j)->getAttribute("name") =~ /^$name$/)
$form->all($j)->{value} = $value;
$form->all($j)->style->{backgroundColor} = "yellow"; # Highlight the edit box color to yellow
sleep 3;
return;
}
}
}
}
# Click the Search button
sub ClickFormButton { #\@Docs, $ButtonName
my $Documents = shift @_;
my $ButtonName = shift @_;
my $Document;
my $IEDocument;
my $i;
my $j;
my $k;
foreach $Document (@$Documents) {
$IEDocument = $Document->{Document};
for ($i=0; $i<$IEDocument->all->length; $i++) {
if ($IEDocument->all($i)->tagName =~ /^FORM$/)
my $form = $IEDocument->all($i);
for ($j=0; $j < $form->all->length; $j++) {
my $name = $form->all($j)->getAttribute("value") . "\n";
if ($form->all($j)->getAttribute("value") =~ /^$ButtonName$/)
$form->all($j)->click();
$form->all($j)->style->{backgroundColor} = "yellow";
sleep 5;
return;
}
}
}
}
$IEbrowser->Quit();