nightwatch custom command callback

2020-07-27 03:47发布

问题:

I'm trying to create a custom command in nightwatch that runs a query on a Postgres database and returns the result. The query runs just fine and outputs the result to the console but then the execution of the test stops. I don't understand how callbacks work. How can I fix this custom command?

exports.command = function(sql, callback) {
  var self = this;
  var pg = require('pg');
  var conString = self.globals.testinfo.connectionString;
  var db = new pg.Client(conString);
  db.connect(function(err) {
    if(err) {
      console.error('could not connect', err);
    } 
    else {
      db.query(sql, function(err, result) {
        if(err) {
          console.log('error running query', err);
        } 
        else {
          console.log(result.rows.length);
          db.end();
        }
      });
    }
  }),
  function(result) {
    if (typeof callback === 'function') {
      callback.call(self, result);
    }
  }  
  return this;
};

回答1:

I had to wrap the database connection in a perform command to get this working. I'm not sure if this is the best way to handle the callback, but it works. Here's the updated version of the custom command:

exports.command = function(sql,callback) {
  var self = this;
  var pg = require('pg');
  var cs = self.globals.testinfo.connectionString;
  self.perform(function(self,done) {
    pg.connect(cs,function(err,db,done) {
      if(err) {
        return console.error(err);
      }  
      db.query(sql, function(err,result) {
        done();
        if(err) {
          return console.error(err);
        } 
        console.log(result.rows.length);
        callback(result.rows[0]);
      });
    });
    pg.end();
    done();
  });
};

Here's how I call the custom command in the test:

browser.myCustomCommand('select * from table limit 1;', function(row) {
  browser.assert.deepEqual(row.column,'some value');
});


回答2:

Can you try this:

exports.command = function(sql, callback) {
   var self = this;
   var pg = require('pg');
   var conString = self.globals.testinfo.connectionString;
   var db = new pg.Client(conString);
   var cb= function(result) {
     if (typeof callback === 'function') {
      callback.call(self, result);
     }
   };
  db.connect(function(err) {
    if(err) {
      console.error('could not connect', err);
       cb(false);
    } 
    else {
      db.query(sql, function(err, result) {
        if(err) {
          console.log('error running query', err);
          cb(false);
        } 
        else {
          console.log(result.rows.length);
          db.end();
          cb(true);
        }
      });
    }
  }), 
  return this;
};

And in your test :

'test' : function(browser){
   browser.yourCommandName(sql,function(result){
      console.log(result); //if connect is good result would be true and false if fail to connect.
});  
}

Ps: the result in callback can be as an object(contain rows or anything you want), instead of boolean only in this example.

And Nightwatch is used for end-to-end testing, it is not aimed for Database testing,i think you should find another framework to test database connection.