Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Get ajax request with nightmare js

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?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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()

about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!