Expect assertions Suggest edits

In addition to the assert namespace, the Nightwatch API supports out of the box a BDD-style expect assertion library which greatly improves the flexibility as well as readability of the assertions.

The expect assertions use a subset of the Expect api from the Chai framework and at this point are available for elements, cookies, page title, and url.

Element expect example

Here a basic example that uses various expect.element([...]) assertions:

module.exports = {
  'Demo test Ecosia.org': function (browser) {

    // expect element header to be present in 1000ms

    // expect element header to have css property 'display'

    // expect element header to have attribute 'class' which contains text 'index-header'

    // expect element .search-form to be an input tag

    // expect element header to be visible


Expecting a specific elements count

In this example, the test is expecting that a specified number of elements exist on the page, using the expect.elements([...]).count assertion:

module.exports = {
  'demo test ecosia.org'(browser) {

  after(browser) {

The expect interface provides a much more flexible and fluid language for defining assertions, significantly improved over the existing assert interface. The only downside is that it's not possible to chain assertions anymore.

For a complete list of available expect assertions, refer to the API docs.

Improve this article