segment-boneyard / nightmare

A high-level browser automation library.
https://open.segment.com
19.54k stars 1.08k forks source link

Looping through dynamically created form fields to add text #1525

Open mikenewyn opened 5 years ago

mikenewyn commented 5 years ago

I know this is similar to some other issues but I think I am trying to do something slightly different. I have an app(similar to MadLibs) that has a number of input fields that need to be filled out, then hit a submit button. The fields are created by different "stories" and therefor have different number of fields depending on the story.

Is there a way to loop it through the dynamically created fields? Right not it is just hard-coded and because of that, it is failing since each story has a different number of inputs.

var Nightmare = require("nightmare");

var nightmare = Nightmare({ show: true });

nightmare .goto("http://localhost:8080/test") .type("#input0", "Goku") .wait(500) .type("#input1", "hardened") .wait(500)
.type("#input2", "K-Mart") .wait(500) .type("#input3", "wanderlust") .wait(500) .type("#input4", "Pokeball") .wait(500) .type("#input5", "stupidly") .wait(500) .type("#input6", "thick") .wait(500) .type("#input7", "boil") .wait(500) .type("#input8", "screwed") .wait(500) .type("#input9", "rubber") .wait(500) .type("#input10", "42") .wait(500) .type("#input11", "wanted") .screenshot("UserSave.png") .click("#submitButton") .wait(1000) .type("#userNameBox", "New User") .screenshot("BeforeSave.png") .click("#saveButton") .wait(1000) .screenshot("AfterSave.png") .wait(3000) .end() .then(function() { console.log("Took a screenshot of the inputs, resulting story and of the return page"); }) .catch(function(error) { console.error("Search failed:", error); });

scottie commented 5 years ago

Use evaluate to run javascript code on the page and return results.