Nightwatch.js - How to assert a checkbox is NOT checked?

tommarshall picture tommarshall · Jan 19, 2015 · Viewed 12k times · Source

I'm using nightwatch.js to do some end-to-end testing of an application, but having trouble verifying the state of checkboxes.

I'm using attributeEquals() to verify that a checkbox is checked:

module.exports = {
  "Checkbox is checked" : function (client) {
    client
      .url(client.launch_url)
      .useCss()
      .waitForElementVisible("body", 1000)
      .verify.attributeEquals('#a_checkbox', 'checked', 'true') // quotes necessary
      .end();
  }
};

But I also need to verify that checkboxes are not checked.

To do that I've tried using attributeEquals() again, with various expectations:

module.exports = {
  "Checkbox is not checked" : function (client) {
    client
      .url(client.launch_url)
      .useCss()
      .waitForElementVisible("body", 1000)
      .verify.attributeEquals('#b_checkbox', 'checked', null)
      .verify.attributeEquals('#b_checkbox', 'checked', 'null')
      .verify.attributeEquals('#b_checkbox', 'checked', 'false')
      .verify.attributeEquals('#b_checkbox', 'checked', false)
      .verify.attributeEquals('#b_checkbox', 'checked', '')
      .end();
  }
};

But they all fail with a message stating that the checked attribute does not exist:

Running:  Checkbox is not checked

✔  Element <body> was visible after 68 milliseconds.
✖  Testing if attribute checked of <#b_checkbox> equals "null". Element does not have a checked attribute.  - expected "null" but got: null
✖  Testing if attribute checked of <#b_checkbox> equals "null". Element does not have a checked attribute.  - expected "null" but got: null
✖  Testing if attribute checked of <#b_checkbox> equals "false". Element does not have a checked attribute.  - expected "false" but got: null
✖  Testing if attribute checked of <#b_checkbox> equals "false". Element does not have a checked attribute.  - expected "false" but got: null
✖  Testing if attribute checked of <#b_checkbox> equals "". Element does not have a checked attribute.  - expected "" but got: null

That message is correct, there is no checked attribute, but the absence of the attribute means the checkbox is not checked, and therefore I want the test to pass.

How can this be achieved?

I'm using nightwatch v0.5.36 if that's significant.

Answer

Kai picture Kai · Sep 4, 2015

You can try: browser.expect.element('#b_checkbox').to.not.be.selected;