How to run steps/functions in nightwatch test sequentially?

4,306 views
Skip to first unread message

Monogeoo

unread,
Jan 25, 2015, 2:57:50 AM1/25/15
to nightw...@googlegroups.com
I am new to nightwatch and nodejs. I want to execute the following functions in a nightwatch test sequentially, but it doesn't. The function second gets executed before the function first has completed. 

module.exports = {
 
"Test end-to-end new incident flow" : function(client) {
   
var async = require('async');
   
var incident_log = require("crypto").randomBytes(32).toString('hex');
   
var incident_name;
    async
.series([
     
function first(callback) {
        console
.log('first');
       
var incident_name;
        client
         
.page.admin_home().fetchPage()
         
.page.admin_home().login()
         
.page.admin_home().clickIncidentsTab()
         
.page.incidents_home().verifyPage()
         
.page.incidents_home().clickNewButton()
         
.page.new_incident().verifyPage()
         
.page.new_incident().createIncident(incident_log)
         
.page.incident_detail().verifyPage()
         
.page.incident_detail().getIncidentName(function(result) {
            incident_name
= result.value
            console
.log(result.value)
         
});
        callback
(null, 'first');
     
},
     
function second(callback) {
        console
.log('second');
       
var sfdb = require('sfdb.js')
       
var query = "SELECT Id, Name FROM Incident WHERE Name like '%"+incident_name+"%'";
        sfdb
.query(client.globals, query, function(data) {
          console
.log(require('util').inspect(data))
       
});
        callback
(null, 'second');
     
}
   
],
   
function(err, results) {
      console
.log(results)
   
});
    client
.end();
 
}
};

Here is the actual output from the test:

Running:  Test end-to-end new incident flow
 

first
second
[ 'first', 'second' ]
{ totalSize: 0, done: true, records: [] }
 Element <#Login> was visible after 72 milliseconds.
 Testing if element <#Login> is visible.
 Element <img.chatter-photo> was visible after 6776 milliseconds.
 Testing if attribute title of <span.chatter-avatarSmall> equals "Panda Tester".
 Element <#AllTab_Tab> was visible after 17 milliseconds.
 Element <.pbTitle> was visible after 1202 milliseconds.
 Testing if element <.pbTitle> contains text: "Recent Incidents".
 Element <.pbButton> was visible after 80 milliseconds.
 Testing if element <.pbButton> is present.
 Element <.pageDescription> was visible after 1045 milliseconds.
 Testing if element <.pageDescription> contains text: "New Incident".
 Element <.mainTitle> was visible after 4920 milliseconds.
 Testing if element <.mainTitle> contains text: "Incident Detail".
 Element <h2.pageDescription> was visible after 20 milliseconds.
I
-0240

OK
. 14 total assertions passed. (25.724s)


Since the function second gets executed while the function first is still running; hence, the database query returns:

{ totalSize: 0, done: true, records: [] }

If the function second gets executed once the function first has completed, the database query should return:

{ totalSize: 1, done: true, records: [Id: 'xxxxxx', Name: 'I-0240'] }


What am I done wrong? What is the proper way to execute steps/functions in nightwatch test sequentially?

Please help and thanks!




Monogeoo

unread,
Jan 25, 2015, 6:35:32 PM1/25/15
to nightw...@googlegroups.com
I tried the multi-steps approach documented in http://nightwatchjs.org/guide#write-tests, and it seems to solve my problem. Below is my code. I am new to nightwatch and nodejs so I am not quite sure my solution is good enough. Please let me know if there are better ways than this. Thanks!

var incident_log = require("crypto").randomBytes(32).toString('hex');
var incident_name;
module.exports = {
 
"Test end-to-end new incident flow - Step 1" : function(client) {
    console
.log('first');

    client
     
.page.admin_home().fetchPage()
     
.page.admin_home().login()
     
.page.admin_home().clickIncidentsTab()
     
.page.incidents_home().verifyPage()
     
.page.incidents_home().clickNewButton()
     
.page.new_incident().verifyPage()
     
.page.new_incident().createIncident(incident_log)
     
.page.incident_detail().verifyPage()
     
.page.incident_detail().getIncidentName(function(result) {
        incident_name
= result.value
        console
.log(result.value)
     
});

 
},
 
"Test end-to-end new incident flow - Step 2" : function(client) {
    console
.log('two');

   
var sfdb = require('sfdb.js')
   
var query = SELECT Id, Name FROM Incident WHERE Name like '%"+incident_name+"%'"
;

    sfdb.query(client.globals, query, function(data) {
      console.log(require('util').inspect(data))
    });
    client.end();
  }
};

And the output:

Running:  Test end-to-end new incident flow - Step 1

first
 Element <#Login> was visible after 78 milliseconds.
 Testing if element <#Login> is visible.
 Element <img.chatter-photo> was visible after 10140 milliseconds.
 Testing if attribute title of <span.chatter-avatarSmall> equals "Panda Tester".
 Element <#AllTab_Tab> was visible after 19 milliseconds.
 Element <.pbTitle> was visible after 1384 milliseconds.
 Testing if element <.pbTitle> contains text: "Recent Incidents".
 Element <.pbButton> was visible after 21 milliseconds.
 Testing if element <.pbButton> is present.
 Element <.pageDescription> was visible after 967 milliseconds.
 Testing if element <.pageDescription> contains text: "New Incident".
 Element <.mainTitle> was visible after 2622 milliseconds.
 Testing if element <.mainTitle> contains text: "Incident Detail".
 Element <h2.pageDescription> was visible after 20 milliseconds.

I
-0241

OK
. 14 assertions passed. (30.874s)


Running:  Test end-to-end new incident flow - Step 2  

two
{ totalSize: 1, done: true, records: [Id: 'xxxxxx', Name: 'I-0241'] }

No assertions ran.
OK
. 14 total assertions passed. (34.659s)


Jens Gellynck

unread,
Oct 22, 2016, 2:41:14 PM10/22/16
to NightwatchJs
Thanks! I was having the same problem

Op maandag 26 januari 2015 00:35:32 UTC+1 schreef Monogeoo:
Reply all
Reply to author
Forward
0 new messages