views:

41

answers:

3

I am currently testing a large web form and would like to be able to easily populate the form with several different lots of test data without having to type them each time.

Is there a generic way to capture form inputs on a web page and have them repopulated on a different page load? I thought a tool like greasemonkey might be able to do something like this.

+2  A: 

The task you're describing is perfectly suited for an automated testing tool such as Selenium.

Selenium is a suite of tools to automate web app testing across many platforms.

Dolph
+1  A: 

If all the fields have proper id's then you ca use something like this:

//set the data 
var data = {
    field1 : "valueforfield1",
    field2 : "valueforfield2",
    ....
};

//populate the form
for (var prop in data) {
    if (data.hasOwnProperty(prop)){[
        var el = document.getElementById("el");
        if (el) {
            el.value = data[prop]; 
        }
    }
}

If they haven't got id's, or you prefer to use names then you can do this

var form = document.forms.formname;
var inputs = form.getElementsByTagName("input");
var selects = form.getElementsByTagName("select");
​var all = Array.prototype.concat.call(inputs, selects), i = all.length;

while (i--){
    var el = all[i];
    if (el.name) && el.name in data){
         el.value = data[el.name];
    }
}
Sean Kinsey
A: 

Thanks for the answers. I decided to use imacros browser plugin as I thought it best suited my problem on this occasion.

Joel Cunningham