Solved

Perl HTML Submit Form Data - Testing

Posted on 2011-03-10
8
442 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

830 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