Tuesday, 7 October 2014

Test 13: Check if requested is redirected to other url

Test:
Request for a URL and check if same URL was opened or it is redirected to some other URL

Learning: 
$response->request->uri; is used to get uri. It should be used with use LWP::UserAgent


Solution:

use LWP::UserAgent qw();

my $ua = LWP::UserAgent->new;
my $response = $ua->get('http://www.google.com');
$tmp=$response->request->uri;
if($tmp eq 'http://www.google.com')
{
print "no redirection\n";
}
else
{
print "redirected to $tmp\n";
}


output snapshot:

Test 12: Enter a keyword in search box, submit the form and check title of result page

Test:
Enter a value in a form of a page, submit the form and check title of resulting page

Learning: 
submit_form is used to submit a form


Solution:

use WWW::Mechanize;
use LWP::UserAgent;
my $m = WWW::Mechanize->new();
$m->get( "http://en.wikipedia.org/wiki/Main_Page" );
$m->submit_form(
    form_number => 1,
    fields    => { search  => 'honey', },
    button    => 'go'
);
$tmp=$m->title;
print "$tmp\n";


output snapshot:

Test 11: Find fields of all forms in a page

Test:
Find fields of all forms in a page

Learning: 
 param is used to get fields of a form


Solution:

use WWW::Mechanize;
use LWP::UserAgent;
my $m = WWW::Mechanize->new();
$m->get( "http://www.rediff.com/" );
@arr=$m->forms;
$count=1;
foreach (@arr)
{
my @inputfields = $_->param;
print "form $count fields are:\n";
$count++;
foreach(@inputfields)
{
print "$_\n";
}
  print "\n";      
  }


output snapshot:

Test 10: Find a url in webpage by keyword

Test:
Given a keyword, find all url having that keyword

Learning: 
 $m->find_all_links(
    tag => "a", text_regex => qr/keyword/i ); used to find keyword in url


Solution:

use WWW::Mechanize;
my $m = WWW::Mechanize->new();
$m->get( "http://www.google.com/" );

my @links = $m->find_all_links(
    tag => "a", text_regex => qr/history/i );


foreach(@links)
{
$tmp=$_->text;
print "$tmp\n";
$tmp=$_->url;
print "$tmp\n";
}


output snapshot:

Thursday, 2 October 2014

Test 9: Check that all links of webpage can be accessed successfully

Test:
Check that all links of webpage can be accessed successfully

Learning: 
get( $link->url ) will take care of relative URL also


Solution:

#!/usr/bin/perl
use Test::More;
use LWP::Simple;
use WWW::Mechanize;

#define number of planned test cases
plan tests => 700; 

my $m = WWW::Mechanize->new();
$m->get( "http://www.google.com/" );

ok( $m->success, 'Fetched OK' );

for my $link ( $m->links )
{
$m->get( $link->url );
print $link->text."\n".$link->url."\n";
ok( $m->success);
$m->back();
ok( $m->success, "Back successful");
print "\n";
}


output snapshot:

Tuesday, 30 September 2014

Test 8: Follow link by different methods

Test:
Follow links by different methods and check if correct link is opened

Learning: 
Use WWW::Mechanize
$m->follow_link to follow links of a page.
We will open google.com, then images link, then back and repeat few times.


Solution:

use Test::More;
use WWW::Mechanize;

#define number of planned test cases
plan tests => 8; 

my $m = WWW::Mechanize->new();
$m->get( "http://www.google.com/" );

#Calling a link by text
$m->follow_link(text=>'Images');
ok($m->title eq "Google Images", "pass");
$m->back();
ok($m->title eq "Google", "pass");

#Calling a link by number
$m->follow_link(n=>1);
ok($m->title eq "Google Images", "pass");
$m->back();
ok($m->title eq "Google", "pass");

#Calling a link by text regex
$m->follow_link(text_regex=>qr/Ima/);
ok($m->title eq "Google Images", "pass");
$m->back();
ok($m->title eq "Google", "pass");

#Calling a link by url regex
$m->follow_link(url_regex=>qr/img/);
ok($m->title eq "Google Images", "pass");
$m->back();
ok($m->title eq "Google", "pass");


output snapshot:

Test 7: Get all links from a page and display their text and url addresses

Test:
Get all links from a page and display their text and url addresses

Learning: 
Use WWW::Mechanize
$m->links to get all links of a page


Solution:

use WWW::Mechanize;
my $m = WWW::Mechanize->new();
$m->get( "http://www.google.com/" );
@arr=$m->links;
$count=1;
foreach (@arr)
{
print "link $count--";
$count++;
print $_->text."\n\t".$_->url."\n\n" ;
}


output snapshot: