Perl mechanize script no form defined - forms

I'm getting an error No form defined at cqSubmitter.pl at line 33 which is the second set_fields method. Other times I get an Error POSTing http://micron.com Internal Server Error at line 39 , which corresponds to the last click_button line.
I'm not really sure what's going on, and why it's saying no form defined? The first half of the code which includes the first click_button method works fine and saves the correct page, but when I try set_fields for the second time, it errors out.
Anyone familiar with the Mechanize package realize what's going on here?
use Data::Dumper;
use HTTP::Request::Common qw(GET);
use WWW::Mechanize;
#Prepopulated information
my $types_ = "";
my $dept_ = "";
my $group_ = "";
#Create new WWW::Mechanize object
my $mech = WWW::Mechanize->new( 'ssl_opts' => { 'verify_hostname' => 0 } );
my $url = "http://f2prbrequest";
#Fetch URL or Die Tryin'
$mech ->get($url);
$fname = "user";
$pswd = "password";
#Login to ClearQuest form using credentials
$mech->set_fields(
USER => $fname
,PASSWORD => $pswd
);
$mech->click_button(
name => 'Submit'
);
#Set fields and actually fill out ClearQuest Form
$mech->set_fields(
types => $types_
,dept => $dept_
,group => $group_
);
$mech->click_button(
name => 'submit1'
);
$mech->save_content("clearQuestFilled.html");

Related

Can't call method "header" on an undefined value at WWW/Mechanize.pm line 2566

I am just doing a testing using WWW::Mechanize module on Facebook, when I try to run the code below, it return me an error
Can't call method "header" on an undefined value at C:/Strawberry/perl/vendor/lib/WWW/Mechanize.pm line 2566.
#!/usr/bin/perl -w
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
# Connect to server
$mech->get( "https://www.facebook.com" );
$mech->success or die $mech->response->status_line;
# Log into server
$mech->field('email', 'xxx#xxx.com');
$mech->field('pass', 'xxxxxxx');
$mech->click_button(value => 'Log In');
Your page is opening in some other language other than English. That's why you are getting that error. If you will open the page in English forcefully then the error will disappear. Try below address:
$mech->get( "https://en-gb.facebook.com/" );
or, you may click directly on instance of HTML::Form::SubmitInput obtained using this:
$mech->current_form()->find_input( undef, 'submit');
or, as there is only one click button in the form you can use click with no arguments.
$mech->click()
or as suggested by #Borodin you can directly use(as the email and password field aren't translated):
$mech->submit_form( with_fields => {
email => 'xxx#xxx.com',
pass => 'xxxxxxx'
}
);

WWW::Mechanize follows a second redirect after six seconds

I am using Perl with the WWW::Mechanize module to submit a form to a webpage and save the result to a file. I know how to submit forms and save the data, but I can't save data after this six-second redirection.
After the form is submitted, the page is redirected to a page that says
Results should appear in this window in approximately 6 seconds...
and it is redirected again to the page with the result I want. My script can follow the first redirection, but not the second, and there is no link says something like "click here if not redirected".
Here is my script
use WWW::Mechanize;
my $mech = WWW::Mechanize->new(autocheck => 1);
$mech->get( "http://tempest.wellesley.edu/~btjaden/TargetRNA2/index.html");
$result = $mech->submit_form(
form_number => 1,
fields => {
text => 'Escherichia coli str. K-12 substr. MG1655',
sequence => '>RyhB' . "\n" .
'GCGATCAGGAAGACCCTCGCGGAGAACCTGAAAGCACGACATTGCTCACATTGCTTCCAGTATTACTTAGCCAGCCGGGTGCTGGCTTTT',
}
);
$mech->save_content(result);
What you need to do is extract the redirect URL and ran it manually:
Try this:
use WWW::Mechanize;
my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->get( "http://tempest.wellesley.edu/~btjaden/TargetRNA2/index.html");
$result = $mech->submit_form(
form_number => 1,
fields =>
{
text => 'Escherichia coli str. K-12 substr. MG1655',
sequence => '>RyhB GCGATCAGGAAGACCCTCGCGGAGAACCTGAAAGCACGACATTGCTCACATTGCTTCCAGTATTACTTAGCCAGCCGGGTGCTGGCTTTT',
}
);
my $content = $mech->content;
my $url1 = 'http://tempest.wellesley.edu/~btjaden/cgi-bin/';
my ($url2) = $content =~ /URL=(targetRNA2\.cgi?.+)?">/;
$mech->get($url1.$url2);
$mech->save_content(result);
WWW::Mechanize and meta refresh
Does the "6 seconds" contain something line the line below? [You may use save_content method of WWW::Machenize to save page to file]
<meta http-equiv="refresh" content="5; url=http://example.com/">
YES=>
Take a look at sources of WWW::Mechanize::Plugin::FollowMetaRedirect.
It shows how WWW::Mechanize may follow meta refresh with redirect.
It may quite likely solve your problem.

How to test for a redirect in Mojolicious?

I want to test a page with a form which, when submitted, will redirect to the resulting page for the submitted item.
My Mojolicious controller contains:
sub submit_new {
my $self = shift;
my $new = $self->db->resultset('Item')->new( {
title => $self->param('title'),
description => $self->param('description'),
} );
$new->insert;
# show the newly submitted item
my $id = $new->id;
$self->redirect_to("/items/$id");
}
The test script for this controller contains:
use Test::More;
use Test::Mojo;
my $t = Test::Mojo->new('MyApp');
my $tx = $t->ua->build_form_tx('/items/new/submit' => $data);
$tx->req->method('POST');
$t->tx( $t->ua->start($tx) )
->status_is(302);
My issue is that it stops with the 302 status. How do I proceed with the redirect so I can verify the resulting item page?
Set the matching setting from Mojo::UserAgent:
$t->ua->max_redirects(10)
Also, you don't need to build the form post manually:
$t->post_form_ok('/items/new/submit' => $data)->status_is(...);
Reference:
http://mojolicio.us/perldoc/Test/Mojo#ua
http://mojolicio.us/perldoc/Mojo/UserAgent#max_redirects
http://mojolicio.us/perldoc/Test/Mojo#post_form_ok
You could also (and probably should) test the content of the landing page to which the successful login has redirected :
my $tm = '' ; # the test message for each test
my $t = Test::Mojo->new('Qto');
$t->ua->max_redirects(10);
my $app = 'foobar';
my $url = '/' . $db_name . '/login' ;
$tm = "a successfull result redirects to the home page";
my $tx = $t->ua->post( $url => 'form' => {'email' =>'test.user#gmail.com', 'pass' => 'secret'});
ok ( $tx->result->dom->all_text =~ "$app home" , $tm );
the docs for how-to get the content with mojo dom
the mojo dom selector syntax doc link
an example testing script with login testing

Connecting keeps closing?

so i'm having a problem trying to automatically login to a internal website. I'm able to send a post request but in the response I always get the Header Connection: close. I've tried to pass is through the post request but it still seems to respond with Connection: close. I want to be able to navigate through the website so I need the Connection: keep-alive so that i can send more request. Could anyone tell me what I'm doing wrong? here's the code:
#usr/bin/perl
#NetTelnet.pl
use strict; use warnings;
#Sign into cfxint Unix something...
use Net::Telnet;
# Create a new instance of Net::Telnet,
my $telnetCon = new Net::Telnet (Timeout => 10,
Prompt => '/bash\$ $/') or die "Could not make connection.";
my $hostname = 'cfxint';
# Connect to the host of the users choice
$telnetCon->open(Host => $hostname,
Port => 23) or die "Could not connect to $hostname.";
use WWW::Mechanize;
my $mech = WWW::Mechanize->new(cookie_jar => {});
&login_alfresco;
sub login_cxfint {
#get username and password from user
my $CXusername = '';
my $CXpassword = '';
# Recreate the login
# Wait for the login: message and then enter the username
$telnetCon->waitfor(match => '/login:/i');
# this method adds a \n to the end of the username, it mimics hitting the enter key after entering your username
$telnetCon->print($CXusername);
# does the same as the previous command but for the password
$telnetCon->print($CXpassword);
#Wait for the login successful message
$telnetCon->waitfor();
}
sub login_alfresco{
my $ALusername = '';
my $ALpassword = '';
$mech->get('http://documents.ifds.group:8080/alfresco/faces/jsp/login.jsp');
my $res = $mech->res;
my $idfaces = '';
if($res->is_success){
my $ff = $res->content;
if($ff =~ /id="javax.faces.ViewState" value="(.*?)"/){
$idfaces = $1;
}
else {
print "javax.faces /Regex error?\n";
die;
}
}
print $idfaces, "\n";
#Send the get request for Alfresco
$mech->post('http://documents.ifds.group:8080/alfresco/faces/jsp/login.jsp',[
'loginForm:rediretURL' =>,
'loginForm:user-name' => $ALusername,
'loginForm:user-password' => $ALpassword,
'loginForm:submit' => 'Login',
'loginForm_SUBMIT' => '1',
'loginForm:_idcl' => ,
'loginForm:_link_hidden_' => ,
'javax.faces.ViewState' => $idfaces], **'Connection' =>'keep-alive'**);
$res = $mech->res;
open ALF, ">Alfresco.html";
print ALF $mech->response->as_string;
if($res->is_success){
my $ff = $res->content;
if($ff =~ /id="javax.faces.ViewState" value="(.*?)"/){
$idfaces = $1;
}
else {
print "javax.faces /Regex error?\n";
die;
}
}
print $idfaces, "\n";
#Logout
$mech->post('http://documents.ifds.group:8080/alfresco/faces/jsp/extension/browse/browse.jsp', [
'browse:serach:_option' => '0',
'browse:search' => ,
'browse:spaces-pages' => '20',
'browse:content-pages' => '50',
'browse_SUBMIT' => '1',
'id' => ,
'browse:modelist' => '',
'ref'=>'',
'browse:spacesList:sort' => ,
'browse:_idJsp7' => ,
'browse:sidebar-body:navigator' => ,
'browse:contentRichList:sort' => ,
'browse:act' => 'browse:logout',
'outcome' => 'logout',
'browse:panel' => ,
'javax.faces.ViewState' => $idfaces,])
}
You can enable keep-alive by using a connection cache:
use LWP::ConnCache;
...
$mech->conn_cache(LWP::ConnCache->new);
All that header means is that the connection will be closed upon completion of the request, instead of being kept open for possible further requests. This is perfectly normal and should not interfere with sending the request.
EDIT: If you're sending a Connection:Keep-Alive and the server is still responding with Connection:Close, then the server configuration needs to be changed. The default for HTTP/1.1 is persistent connections, so the server must explicitly be configured to send Connection:Close. See Section 8 of RFC2616.

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
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.
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.