4

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

Sanx
  • 223
  • 1
  • 6
  • 17
  • 2
    What kind of redirect? Mech will follow HTTP redirects automatically, unless you tell it not to. If this is a Meta-tag or Javascript redirect you'll need to reverse-engineer it and construct the equivalent HTTP request yourself. Or use a browser-based system like Selenium. – friedo Apr 29 '11 at 19:48

2 Answers2

3

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.

Gaurav
  • 1,888
  • 1
  • 18
  • 23
  • thanks, I try your suggestion, but it does redirect a servlet, is still not working, Any other ideas? – Sanx May 03 '11 at 17:19
  • Hmm. Could you let us see the redirect code in the source code to web_page_b.html, perhaps by uploading it to http://gist.github.com/? – Gaurav May 04 '11 at 18:37
0

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.

snoofkin
  • 8,725
  • 14
  • 49
  • 86