How to debug in nightwatch

mark gibson picture mark gibson · Apr 12, 2016 · Viewed 11.6k times · Source

I am trying to debug in nightwatch. When I put in a console.log it prints before the test runs - while its doing some sort of construction/compiling of the test. I tried also visual studio code debugger and same thing - the breakpoint hits before the tests are actually run. thanks for help - mark

Answer

Niraj Tathe picture Niraj Tathe · May 16, 2017

"nightwatch" is built on node.js. Node.js executes statements asynchronously. If you write console.log();, it will be executed asynchronously.

The statements you write using browser (or client) object (e.g. browser.url(); , browser.click(); etc.) will be queued up at Selenium Server. They are also executed asynchronously by node.js but are queued up at selenium server.

To perform console.log() in sync with other statements in nightwatch, use .perform(). You will get output in sync with other statements.

Example

var elementValue;
browser
.getValue('.some-element', function(result) {
  elementValue = result.value;
})
// other stuff going on ...
//
// self-completing callback
.perform(function() {
  console.log('elementValue', elementValue);
  // without any defined parameters, perform
  // completes immediately (synchronously)
})
.end();

For debugging purpose, you can stop the execution to find out element values or check the browser related data. Use this command: browser.pause(); (or client.pause();). Do not pass any timer here. It will stop the execution.