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:

Test 6: Get title using Mechanize


Test:
Get title of a webpage using Mechanize

Learning: 
Use WWW::Mechanize
Mechanize->new() with get to get content of a page


Solution:

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


output snapshot:

Test 5: Print meaning of status code received from server

Test:
Check http response code received from network for a webpage and display its meaning like 200 means OK

Learning: 
Use LWP::Simple
is_success($variable) is used for success cases.
is_error($variable) is used for error cases
status_message($variable) is used to get meaning of error code

Code received by network can be:
RC_CONTINUE
   RC_SWITCHING_PROTOCOLS
   RC_OK
   RC_CREATED
   RC_ACCEPTED
   RC_NON_AUTHORITATIVE_INFORMATION
   RC_NO_CONTENT
   RC_RESET_CONTENT
   RC_PARTIAL_CONTENT
   RC_MULTIPLE_CHOICES
   RC_MOVED_PERMANENTLY
   RC_MOVED_TEMPORARILY
   RC_SEE_OTHER
   RC_NOT_MODIFIED
   RC_USE_PROXY
   RC_BAD_REQUEST
   RC_UNAUTHORIZED
   RC_PAYMENT_REQUIRED
   RC_FORBIDDEN
   RC_NOT_FOUND
   RC_METHOD_NOT_ALLOWED
   RC_NOT_ACCEPTABLE
   RC_PROXY_AUTHENTICATION_REQUIRED
   RC_REQUEST_TIMEOUT
   RC_CONFLICT
   RC_GONE
   RC_LENGTH_REQUIRED
   RC_PRECONDITION_FAILED
   RC_REQUEST_ENTITY_TOO_LARGE
   RC_REQUEST_URI_TOO_LARGE
   RC_UNSUPPORTED_MEDIA_TYPE
   RC_INTERNAL_SERVER_ERROR
   RC_NOT_IMPLEMENTED
   RC_BAD_GATEWAY
   RC_SERVICE_UNAVAILABLE
   RC_GATEWAY_TIMEOUT
   RC_HTTP_VERSION_NOT_SUPPORTED

Solution:

#!/usr/bin/perl

use Test::More;
use LWP::Simple;
use HTTP::Status;

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

#URL under test
my @url = qw("http://www.google.com" "http://www.iamperl.com");

for($i=0;$i<@url;$i++)
{
my $content = mirror($url[$i],abc); 
$a = status_message($content);
if($content == 200)
{
ok(is_success($content), "Http response code is $content which 

means $a");
}
else
{
ok(is_error($content), "Http response code is $content which 

means $a");
}
}


output snapshot:

Test 4: Continue to test if response code is 200, otherwise exit test

Test:
Check http response code received from network for a webpage and continue furthur test case only if response code is 200


Solution:

#!/usr/bin/perl

use Test::More;
use LWP::Simple;

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

#URL under test
my $url = "http://www.google.com";

#getting content of webpage with http response code and store in file abc
my $content = getstore($url,abc); 

#get response code
(my $response)= $content =~ /(\d*)/;

if($response ne "200")
{
ok($response eq "200", "Http response code is not 200 showed 

$response");
exit;
}
ok($response eq "200", "Http response code is 200..we can continue of test");

#if below line is printed means 200 was received as response code and we are 

running next test case now
print "test case 2\n";


output snapshot:


Test 3: Check the http response code from network

Test:
Check http response code received from network for a webpage and display result



Solution:

#!/usr/bin/perl

use Test::More;
use LWP::Simple;

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

#URL under test
my $url1 = "http://www.google.com";
my $url2 = "http://www.iamperl.com";

#getting content of webpage with http response code
my $content1 = getprint($url1); 
my $content2 = getprint($url2); 

#get response code
(my $response1)= $content1 =~ /(\d*)/;
(my $response2)= $content2 =~ /(\d*)/;

ok($response1 eq "200", "Http response code showed $response1");
ok($response2 eq "200", "Http response code showed $response2");


output snapshot:


Monday, 29 September 2014

Test 2: Check content type and charset of webpage

Test:
Check content type and charset of webpage and display result

Learning: 
Use LWP::Simple
Use head($url) to get document headers. Returns the following 5 values if successful: ($content_type, $document_length, $modified_time, $expires, $server)


Solution:

#!/usr/bin/perl

use Test::More;
use LWP::Simple;

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

#URL under test
my $url = "http://www.google.com";

#getting header of webpage
my @content = head($url); 

#get content type
(my $content_type)= $content[0] =~ /(.*)\;/;

ok($content_type eq "text/html", "Content type showed $content_type");

#get charset
(my $charset)= $content[0] =~ /charset=(.*)/;

ok($charset eq "ISO-8859-1", "Charset showed $charset");


output snapshot:

Test 1: Check the title of webpage

Test:
Check the title of webpage and display result

Learning: 
Use LWP::Simple
It allows you to get content of any webpage by a simple code- get($url)


Solution:

#!/usr/bin/perl

use Test::More;
use LWP::Simple;

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

#URL under test
my $url = "http://www.google.com";

#getting source code of webpage
my $content = get($url); 

#Search string between <title> and</title>
(my $str)= $content =~ /<title>(.*)<\/title>/;

ok($str eq "Google", "Title of $url showed $str");


output snapshot: