Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

Perl HTML Submit Form Data - Testing

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
kindaprog
Asked:
kindaprog
  • 3
  • 2
  • 2
  • +1
7 Solutions
 
notjamesCommented:
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
 
Mark BradyCommented:
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
 
notjamesCommented:
@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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mark BradyCommented:
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
 
FishMongerCommented:
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
 
FishMongerCommented:
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
 
FishMongerCommented:
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
 
kindaprogAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now