Nightwatch.js: Open new window and continue testing in new window

Using Nightwatch.js, let’s say you have a button that opens a new window, and you want to do some testing in that new window, such as checking the new window’s url.

  // open new window button
 .verify.visible('.open-new-window')
 .getAttribute('.open-new-window', 'href', function (link) {
 newWindowUrl = link.value;
 })
 .click('.open-new-window')
 .windowHandles(function(result) {
    var newWindow;
    this.verify.equal(result.value.length, 2, 'There should be 2 windows   open');
    newWindow = result.value[1];
    this.switchWindow(newWindow);
    this.verify.urlContains(newWindowUrl);
})

Notice the windowHandles() function that returns all the windows open in the session, and the switchWindow() function allows you to switch windows and target newWindow. ┬áThe context of “this” becomes the new window that you switched to.

Nightwatch.js: Check if a checkbox is checked

This test involves waiting for a checkbox to be visible, clicking on the checkbox, and then verifying that the checkbox has been checked.

The checkbox has the following HTML DOM:

<input type="checkbox" name="vote">

Here is the Nightwatch.js test:

.verify.visible('input[name="vote"]', 'The checkbox named vote is visible')
 .click('input[name="vote"]')
 .waitForElementVisible('body', 1000)
 .element('name', 'vote', function(response) {
 client.elementIdSelected(response.value.ELEMENT, function(result) {
 client.verify.ok(result.value, 'Checkbox is selected');
 });
 })