mark gibson
mark gibson

Reputation: 61

How to debug in nightwatch

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

Upvotes: 6

Views: 15328

Answers (2)

Niraj Tathe
Niraj Tathe

Reputation: 191

"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.

Upvotes: 15

anasarbescu
anasarbescu

Reputation: 158

As you don't really have an example, or some code that you run, i can only give you the general direction you can try in order to fix the issue.

This happens because your code is ran asynchronously (cause that's how javascript works) and everything that is not browser/html related will run before your browser starts your actual test. I suggest you should try using callbacks in order avoid printing your check msgs first (this way you link the console.log to your code that depends on the browser and it will run only after this finishes. If you do that, the debugging with console log will actually work.

Maybe you can learn here how to do that, as an example.

Upvotes: 0

Related Questions