Using nightwatch for automation testing

Introduction

Hello readers! Today we are going to learn about Nightwatch which is used for end to end testing for your web apps.

What is Nightwatch?

Nightwatch is a framework which is used to perform end to end testing of your web applications and websites. It is written in nodejs and supports all major browsers(Chrome, Firefox, edge and Safari). It allows developers to write, manage and execute tests easily, making it a popular choice among modern web developers.

Here is a list of the type of testing supported by Nightwatch:

  • End to end testing of web apps
  • API Testing
  • Component testing for Angular, Vue and React
  • Mobile App Testing for Android and iOS
  • Virtual Regression Testing

Features

  • First of all, Nightwatch uses a simple and easy-to-understand syntax.
  • Moreover, it uses Javascript and runs on Nodejs, making it ideal for Javascript developers.
  • It easily integrated with BrowserStack as well, making it easy for running tests in various environments.
  • It supports running tests in parallel, improving test speed.
  • It supports Page Object Model, encouraging reusable approach to write tests.

Version 3 of Nightwatch supports several new features:

  • To begin with, it has added support for Angular component testing.
  • In addition, Nightwatch has introduced some new selectors: findByText(), findByRole(), findByAltText().
  • Lastly, with version 3, it has introduced Nightwatch inspector which will help you easily find selectors.

Installation

To begin with, for installing Nightwatch, you must have nodejs installed in your system.

Once nodejs is installed, you can then run the following command to add nightwatch in a new project

npm init nightwatch <directory-name>

After successfully running the above command you will be able to see nightwatch.conf.js file in the directory.

Writing tests in Nightwatch

Here is a simple example of a test in nightwatch which checks the title of the homepage.

describe('Verify homepage', function() {
  it('Should render the title properly', function(browser) {
    browser.navigateTo('/').assert.textEquals('h1', 'Nightwatch Testing')
  })
})

To run the tests, we use the following command:

npx nightwatch

After running this, it will open Chrome, run your test and post the results on the terminal.

Conclusion

Nightwatch offers a rich set of features that makes it a powerful and flexible tool for end to end testing of web applications. By using Nightwatch you can ensure better test coverage, faster development cycles and a more reliable web application.

For more information about Nightwatch, you can explore the official docs here.

Finally, for more such posts like this, please follow our LinkedIn page- FrontEnd Competency. OR have a look at such blogs here.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top