DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Browser Automation Using Perl LWP

09.28.2007
| 8539 views |
  • submit to reddit
        // This is a sample code used to measure reports response times on a OAS application.
#!/usr/bin/perl
#
# LWP connection to the Datamart Portal
# Timing of the main reports
#

use strict;
require LWP::UserAgent;

my $ua = LWP::UserAgent->new;

sub isodate() {
        my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
        $mon++; # 0-based index
        $year = $year + 1900;
        my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);
        return $date;
}

sub datamart_login {
        my ( $user, $pass ) = @_;
        my $time_begin=time();
        my $url='http://daprd:7782/portal/page?_pageid=37,134413,37_134422&_dad=portal&_schema=PORTAL';
        my $req = HTTP::Request->new( GET => $url );
        my $resp = $ua->request($req);
        my $loginform = $resp->content ;
        if ( $loginform !~ /Entrez votre nom utilisateur/ ) {
                die isodate()." Failed to get the logon page of the Web Site\n";
        } else {
                my $locale="";
                my ($v) = $loginform =~ /NAME=\"v\" value=\"(.+)\"/;
                my ($site2pstoretoken) = $loginform =~ /NAME=\"site2pstoretoken\" value=\"(.+)\"/;
                my ($submiturl) = $loginform =~ /form method=\"POST\" action=\"(.*?)\"/;
                $resp = $ua->post( $submiturl,
                   [
                     ssousername => $user,
                     password => $pass,
                     v => $v,
                     locale => $locale,
                     site2pstoretoken => $site2pstoretoken
                   ],
                );
                $resp = $ua->get($url);
                $resp = $ua->get($url);
                if ( $resp->content !~ /Crit..res de recherche/ ) {
                        die isodate()." Failed to get the main page of Portal\n";
                }
        }
        print join(";",isodate(),"Time to log on the Portal",time()-$time_begin,$url)."\n";
}

sub datamart_testurl {
        my ($label,$url,$expected)=@_;
        my $time_begin=time();
        my $resp;
        $resp = $ua->get($url);
        $resp = $ua->get($url) if $resp->content !~ /$expected/;        # We try two times !
        if ( $resp->content !~ /$expected/ ) {
                print STDERR isodate()." Test Failed on $label. $expected not found in response.\n";
                print STDERR $resp->as_string;
        } else {
                print join(";",isodate(),$label,time()-$time_begin,$url)."\n";
        }
}

$ua->timeout(1200);
$ua->cookie_jar({});
$ua->agent( 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' );
#push @{ $ua->requests_redirectable }, 'POST';

# >>>> Main code here

datamart_login("xxxx","xxxx");

open FH,"/exploit/scripts/appli/check_datamart.ini" or die "Unable to open check_datamart.ini";
while (<FH>) {
        chomp();
        my ($report,$expected,$url) = split /;/;
        datamart_testurl($report,$url,$expected);
}
close FH;

# <<<< Main code here