So, I'm using nightmare js, and I like to simulate a login procedure, to do so I use nightmarejs like that
function testiiing(){
nightmare
.goto('http://localhost:4200/login')
.type('#name', 'test')
.type('#pwd', 'test')
.click('#log')
.evaluate(function() {
return //something
})
.then(function(result) {
console.log(result);
})
.then(function() {
console.log('done');
})
.catch(function(error){
console.error('an error has occurred: ' + error);
});
}
The thing is I'd like to change the "//something" into something that would return me "name=test&pwd=test" (so the ajax post request), can anybody help me or tell me if it's possible at all?
no experience with nightmare js but since you've tagged jQuery, simply use .serialize()
https://api.jquery.com/serialize/
The
.serialize()method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as<input>,<textarea>, and<select>:$( "input, textarea, select" ).serialize();
Example:
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
console.log( $( this ).serialize() );
});
This should do precisely what you are looking for.
For your specific example, also look into .serializeArray()
I know this is old but this was tricky for me too. This is what I am using and it works:
test(){
let selector = "#twd";
return this.nightmare
.goto("https://time.is/")
.inject("js", "jquery.js") //actually injects jquery, which doesn't exist on the site
.evaluate((selector) =>{
return new Promise((resolve, reject) =>{
// resolve(document.querySelector(selector).innerHTML)
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts",
type: "POST",
data: {foo: "bar"},
})
.then(response =>{
resolve(response) //resolve the promise from browser and resume execution in node
})
})
}, selector) //can only pass in one param
.then(result =>{
let x = result;
})
}
//result = {id: some number}
Notice selector, it must be one paramter, or an object if you want more key values. It's not actually used in this crappy example, but it's just a crappy example.
And for whatever reason, the js style inside the evaluate callback could handle es6 style code.
Also, the internal promise chain is completely different from the outside one. Everything within evaluate is running in electron NOT your node server. But once you resolve, it picks back up in node.