SamCG has asked for the wisdom of the Perl Monks concerning the following question:

Sagacious Monks,

I don't have access to the back end database but I have been given a username and password to log on to the Cognos reporting system.

Naturally, I have a number of issues with this system -- it doesn't give the previous business day as a default unless someone's previously selected it, it doesn't give any facility for emailing or saving certain reports, and I can't alter parameters without physically opening the thing up and typing them in.

This is most annoying.

I'd like to use WWW::Mechanize or some other device to log in and perform some tasks, particularly getting and saving reports. However, I encounter errors on trying to log in.

I've looked at the source, and I know my username and password are correct. Here's the simple test code I've tried:
#! perl -w use strict; use WWW::Mechanize; my $url = 'http://crnprdweb/CRN/cgi-bin/cognos.cgi?b_action=xts.run&m= +portal/main.xts'; my $mech = WWW::Mechanize->new(); $mech->get( $url ); print $mech->status, "\n"; print $mech->success; my $content = $mech->content; $mech->submit_form( form_number =>1, fields => { CAMUsername => 'blahuser', CAMPassword => 'blahpassword', } ); $content = $mech->content; print $content;
Here are my errors. Notice I changed to use the form number instead of the name on discovering that didn't work. Commenting out the submit, I still get a status of 401 -- Unauthorized, though I'm just trying to get the base page.
H:\script>perl mechtest.pl 401 There is no form named "pform" at mechtest.pl line 12 Can't call method "value" on an undefined value at C:/Perl/site/lib/WW +W/Mechaniz e.pm line 571. H:\script>perl mechtest.pl 401 There is no form numbered 1 at mechtest.pl line 12 Can't call method "value" on an undefined value at C:/Perl/site/lib/WW +W/Mechaniz e.pm line 571.
I'm wondering if this is specific to Cognos, and whether I should try automating IE instead. HTML and javascript isn't exactly my strength (I can scratch out a simple page of html, but I don't work with it extensively), so I've considered that it may be the nature of the submit button as well (though WWW::Mechanize indicates its "submit" function doesn't actually rely on the button, it's not clear to me what it does rely on). So, here's the code for that button (sans the actual function code).
<a href="javascript:doSubmit()" onblur="javascript:setFocus('')" onfoc +us="javascript:setFocus('ok')" onmouseover="window.status='';return t +rue;" onmouseout="window.status='';return true;"><span class="command +ButtonActive">OK</span></a>
Can anyone give me some advice on how to get around these errors, or whether I'd be better advised to automate the browser instead of using Mech? Obviously I haven't gotten very far yet, and don't know what other errors I might encounter.

Replies are listed 'Best First'.
Re: WWW::Mechanize Cognos web
by monarch (Priest) on Feb 02, 2006 at 00:37 UTC
    If your authentication is Basic where the browser pops up a window asking for your username and password then you can provide these by creating a child class of WWW::Mechanize (or LWP::UserAgent) as follows:
    { package MyMech; use vars qw(@ISA); @ISA = qw(WWW::Mechanize); sub new { my $self = WWW::Mechanize::new(@_); $self->agent("www-mechanize/$WWW::Mechanize::VERSION"); $self; } # this routine gets called when your browser # would otherwise be asked to provide a # username and password sub get_basic_credentials { my ($self, $realm, $uri) = @_; print( STDERR " - providing auth to realm \"$realm\"\n" ); return( $username, $password ); } }

    You just then use your new class instead.

    my $mech = MyMech->new(); $mech->get( $url ); print $mech->status, "\n"; print $mech->success;

    Note this technique not only works with web sites using Basic but company web proxies that stop and ask you for a username and password before proceeding (it appears this could be the case for this particular thread).

    Update:
    - Added readmore tags..
    - Added comment about company proxies