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:

No comments:

Post a Comment