Solved

Perl HTML Submit Form Data - Testing

Posted on 2011-03-10
8
424 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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 213 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

772 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

11 Experts available now in Live!

Get 1:1 Help Now