Solved

Perl HTML Submit Form Data - Testing

Posted on 2011-03-10
8
429 Views
Last Modified: 2012-05-11
Situation: I have multiple websites with multiple forms each. I would like to create a script that will submit the form data to the server script so I don't have to open a browser and fill out the form to test. I know how to do this part...

My question is...

Is there an easy way to load the form fields and data into the script?

I was going to create a config file (tab delimited or something) with the form fields and data that I could parse and use to submit. Then I thought well I could save the page (after filling the form out) and parse the fields and values from it. This way I don't have to type everything... just the values into the form (once).

Is there any better ways to do this?
Are there possibly any cpan modules or anything that makes this type of testing easier??


Just looking for advice and more knowledge!

Thanks in advance!
0
Comment
Question by:kindaprog
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Accepted Solution

by:
notjames earned 144 total points
ID: 35105095
Not sure about CPAN modules. I'm sure there's something out there in the wild.

So I'm assuming you know the fields you want to fill out per form? This is pretty easy. You don't really need a complicated Perl script unless you want to find some way to automate determining the form fields and the values for those form fields and then submitting.

I recommend creating a shell script that calls wget with the params to each form for each site. You infer that the values will probably never change. So, creating a file that has the URI's with the accompanying fields and values would be easy to create and simply read in a looped fashion calling wget per line. IE

URI FILE...let's call it form_uris.txt

'http://myhost1.com/application1.cgi?field1=value1&field2=value2'
'http://myhost2.com/application2.cgi?field1=value1&field2=value2'
'http://myhost3.com/application3.cgi?field1=value1&field2=value2'

Your shell Script:
#!/bin/sh

INFILE='/path/to/form_uris.txt'
OUTFILE='/path/to/output.txt'

while read line
do
    wget $line >> $OUTFILE
    echo >> $OUTFILE # separate output with a \n
done < $INFILE

....

Anyway, this is just one of millions of ways to do this.
0
 
LVL 20

Assisted Solution

by:Mark Brady
Mark Brady earned 143 total points
ID: 35107554
I do this when I'm testing forms especially large ones.

Create a javscript function to fill your form out for you and on the pages <body> tag run that function.
<body onload="fillMyForm()">

Then in the head section

function fillMyForm()
{
document.getElementById('firstname').value = 'John'; // etc...etc...
}
0
 
LVL 1

Assisted Solution

by:notjames
notjames earned 144 total points
ID: 35113116
@elvin66: The only problem with your solution, though not a bad solution, is that I believe the OP was inferring the use of a back-end solution, while your solution requires a client-side solution.
0
 
LVL 20

Assisted Solution

by:Mark Brady
Mark Brady earned 143 total points
ID: 35113136
Fair comment. You can do the exact same thing using php. Create the form in a php variable and put values into the "value=" fields. When they echo the table it will be filled in.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 213 total points
ID: 35122586
Is the lack of need for basic authentication the only difference between this question and your other practically identical one a few weeks ago?

What do you need to do once the script does the submission?  Do you need to save the returned html in one or more files, as notjames is doing, or do you want the script to process the returned html, or do you simply need to know if the submission was successful?

Since you posted this question in the Perl area and specified Perl as the language of choice in the subject, I'll direct you to a Peerl solution.

The module I'd use for the submission is LWP::Simple
http://search.cpan.org/~gaas/libwww-perl-6.01/lib/LWP/Simple.pm

For now I'll make the same assumption as notjames did about the content of your config file.

#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;

my $config_file = '/path/to/config.txt';

open my $fh, '<', $config_file or die "failed to open <$config_file> $!";

while ( my $url = <$fh> ) {
    
    chomp $url;
    
    # if you want to save the returned page, use getstore() instead of head()
    my $rc = head($url);
    
    if ( is_success($rc) ) {
        print "successful: $url\n";
    }
    else {
        print "failed: $url";
    }
}
close $fh;

Open in new window

0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 213 total points
ID: 35122622
I forgot that the head() function doesn't return the status code, like most of the other functions.  So, if you use the head function, you'll need to adjust it a little.

my ($content_type, $document_length, $modified_time, $expires, $server)  = head($url);

And the test for success/failure would also need to be adjusted accordingly.
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 213 total points
ID: 35122631
Or instead of assigning the vars, you could do this:

while ( my $url = <$fh> ) {
    
    chomp $url;

    if ( head($url) ) {
        print "successful: $url\n";
    }
    else {
        print "failed: $url";
    }
}

Open in new window

0
 

Author Comment

by:kindaprog
ID: 36424883
I apologize. I thought I had already closed this question. I ended up using a Firefox add-on (iMacros).

I'm going to split the points evenly between all responses.

Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
By this time the large percentage of day-to-day transactions have shifted to mobile banking; here are some overriding areas QAs must investigate while testing mobile banking apps.  
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now