Skip to content

Instantly share code, notes, and snippets.

@Eseperio
Last active February 13, 2018 17:14
Show Gist options
  • Save Eseperio/1264ae54ea08f8cb3a7676d191c00169 to your computer and use it in GitHub Desktop.
Save Eseperio/1264ae54ea08f8cb3a7676d191c00169 to your computer and use it in GitHub Desktop.
Creates I->submitForm method from existing form.
/**
* Parse all forms and returns a valid PHP code to use with codeception acceptance tests.
*/
var formFixtureGenerator = {
init: function () {
if (typeof jQuery !== "function") {
console.error("Jquery required");
}
let data = this.extract();
console.log("%c Form Fixture Generator tool", "background:#0C8DEA;color:white; text-transform:uppercase;" +
" font-size:20px; padding:5px 20px;");
console.log('%c-------------- BEGIN OF CODE --------------------------', "color:#0C8DEA");
console.log(this.formatOutput(data));
console.log('%c-------------- END OF CODE --------------------------', "color:#0C8DEA");
console.log('%c NOTE: All inputs has been collected. Checkboxes and radio inputs may be duplicated.' +
' This is ok, delete all you are not going to use',
"background:#eee;color: #333;font-style:italic")
},
extract: function () {
let forms = [];
$('form').each(function (e) {
let form = {'name': $(this).attr('id')};
let inputs = [];
$(this).find(':input').each(function (k, v) {
console.log("algo");
inputs.push({
'name': v.name,
'value': v.value
})
});
form['inputs'] = inputs;
forms.push(form);
});
return forms;
},
formatOutput: function (forms) {
const EOL = "\n";
let response = "";
$.each(forms, function (key, form) {
response += '$I->submitForm("form#' + form.name + '",[' + EOL;
$.each(form.inputs, function (k, v) {
if (v.name.trim() !== "" && v.value.trim() !== "") {
response += ' \'' + v.name + '\' => \'' + v.value + '\',' + EOL
}
})
response += EOL + ']);'+EOL;
})
return response;
}
}
formFixtureGenerator.init();
@Eseperio
Copy link
Author

What is this

This is a tool to generate valid php code to use in acceptance tests, in codeception. This code parses an existing form and its values and generates the code needed for $I->submitForm(...).

Why

Because making tests for an app is so boring.

USAGE:

Copy and paste the code into your browser console on the page that have the form or forms you want to generate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment