Sanx
Sanx

Reputation: 323

perl WWW::Mechanize, link redirect problem

I use WWW::Mechanize::Shell to test stuff.

my code is this:

#!/usr/bin/perl
use WWW::Mechanize;
use HTTP::Cookies;

my $url = "http://mysite/app/login.jsp";
my $username = "username";
my $password = "asdfasdf";
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_number(1);
$mech->field(j_username => $username);
$mech->field(j_password => $password);
$mech->click();
$mech->follow_link(text => "LINK A", n => 1);   
$mech->follow_link(text => "LINK B", n => 1);   

........................ ........................ ........................ etc, etc.

the problem is the next:

LINK B (web_page_b.html), make a redirect to web_page_x.html

if I print the contents of $mech->content(), display web_page_b.html

but i need to display web_page_x.html,to automatically submit a HTML form (web_page_x.html)

The question is:

How I can get web_page_x.html ?

thanks

Upvotes: 4

Views: 1459

Answers (2)

Gaurav
Gaurav

Reputation: 1918

Why don't you first test to see if the code containing the redirect (I'm guessing it's a <META> tag?) exists on web_page_b.html, then go directly to the next page once you're sure that that's what a browser would have done.

This would look something like:

  $mech->follow_link(text => "LINK B", n => 1);
  unless($mech->content() =~ /<meta http-equiv="refresh" content="5;url=(.*?)">/i) {
     die("Test failed: web_page_b.html does not contain META refresh tag!");
  }
  my $expected_redirect = $1;      # This should be 'web_page_x.html'
  $mech->get($expected_redirect);  # You might need to add the server name into this URL

Incidentally, if you're doing any kind of testing with WWW::Mechanize, you should really check out Test::WWW::Mechanize and the other Perl testing modules! They make life a lot easier.

Upvotes: 3

snoofkin
snoofkin

Reputation: 8895

In case it doesn't really redirect, then you better use regex with that follow_link method rather than just plain text.

such as:

$mech->follow_link(url_regex => qr/web_page_b/i , n => 1);  

same for the other link.

Upvotes: 0

Related Questions