开发者

Capture form fields and repopulate the form with them

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 开发者_如何学Gotool like greasemonkey might be able to do something like this.


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.


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];
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜