Nightwatch.js

website·Indexed 39 days ago

https://nightwatchjs.org/v26

An end-to-end test framework featuring a comprehensive API for web element interaction, window management, and document scripting. Documentation includes guides for mobile web testing on Android emulators, Chrome extension testing, custom reporter creation, and migration paths from Protractor.

Tokens
121.6K
Snippets
667
Records
927
Agent score
73%

What's inside Nightwatch

  1. Overview of Component Testing in Nightwatch

    Nightwatch provides a component testing solution designed to test individual UI components in isolation. It is powered by the vite-plugin-nightwatch plugin for projects using Vite. For specific frameworks, Nightwatch offers dedicated plugins built on top of the Vite plugin: @nightwatch/react for React components and @nightwatch/vue for Vue components.
  2. Overview of the Nightwatch HTML Reporter

    The integrated HTML reporter (available since v2.2) aggregates test results into a user-friendly HTML view. It provides test results, execution time per test module, assertions made in each test case, and raw HTTP logs for debugging. The HTML reporter is enabled by default along with JUnit XML and JSON reporters.
  3. Overview of the Nightwatch JSON Reporter

    The JSON Reporter is one of the default reporters in Nightwatch (alongside HTML and XML). It outputs test results in JSON format, which can be consumed by other tools to visualize test reports. It has been enabled by default since version 2.2.
  4. Overview of Nightwatch.js capabilities

    Nightwatch.js is a Node.js-based integrated framework designed for automated end-to-end (E2E) testing of web applications and websites across all major browsers. It utilizes the W3C WebDriver API to interact with browsers and can be used for:

    • End-to-end tests on major web browsers
    • Unit tests for Node.js services
    • Integration tests for HTTP APIs
  5. Overview of Nightwatch User Actions API

    Nightwatch provides access to the Selenium Actions API, allowing developers to simulate complex user interactions across different devices. These actions are queued and then executed using the .perform() command.
  6. Manage WebDriver sessions using .session()

    The .session() command allows you to get information about the current session, create a new session, or delete a specific session. By default, if no action is specified, it performs a 'get' request for the current session.
    this.demoTest = function (browser) {
       // Get info about the current session
       browser.session(function(result) {
         console.log(result.value);
       });
    
       // Delete the current session
       browser.session('delete', function(result) {
         console.log(result.value);
       });
    
       // Delete a specific session by ID
       browser.session('delete', '12345-abc', function(result) {
         console.log(result.value);
       });
    }
  7. Automate native mobile apps with Nightwatch and Appium

    Nightwatch provides end-to-end functional testing for native mobile applications on Android and iOS by integrating with Appium. Appium acts as a NodeJS-based server that exposes REST APIs compliant with the W3C WebDriver protocol, wrapping vendor automation libraries such as UIAutomator2/Espresso for Android and Apple XCUITest/UIAutomation for iOS. In a test scenario, Nightwatch sends requests to the Appium server, which then communicates with the native platform drivers to execute commands on the device.
  8. Best practices for upgrading Chrome on Android Emulators

    When upgrading the Google Chrome browser on an Android emulator for mobile web testing (e.g., with Nightwatch and Appium), follow these guidelines to ensure stability and compatibility:

    • Match Architecture: The Chrome APK architecture must match the system image architecture of the emulator.
    • Version Compatibility: Ensure the Chrome version is compatible with the Android API level of the emulator image.
    • ChromeDriver Support: Using the latest Chrome version is recommended for better Appium support and automatic ChromeDriver download capabilities. Note that older or specific versions may require manual ChromeDriver installation.
    • Verification: Always verify that Chrome functions correctly and that ChromeDriver is available before executing Nightwatch tests.
  9. Perform BDD-style assertions using the Expect API

    Nightwatch provides a fluent BDD-style interface for performing assertions on elements via the expect namespace on the main Nightwatch instance. It allows for chainable assertions based on CSS or XPath selectors. To improve readability, several 'language chains' (getters) are available that do not affect the test logic: to, be, been, is, that, which, and, has, have, with, at, does, and of.
    describe('expect example', function() {
      it('sample test', function (browser) {
        // Assert the element is present
        browser.expect.element('#main').to.be.present;
        // Assert the element is visible
        browser.expect.element('#main').to.be.visible;
      });
    });
  10. Perform API testing with Nightwatch

    Available since v2.6.4, Nightwatch supports API testing to verify the reliability and functionality of an application's API layer. Key capabilities include performing request and response assertions, viewing API test results within the HTML report, and utilizing a mock server.
  11. Create a Spy with @nightwatch/testdoubles

    A spy records metadata about function calls (e.g., call count and arguments) without changing the function's behavior. Use sinon.spy(object, 'methodName') to track calls to a specific method. Always call .restore() after the test to return the function to its original state.
    describe('use spies in nightwatch', function() {
      it('should log message when called', function({sinon}) {
        const obj = {
          hello: () => console.log('Hello!')
        }
        const sayHello = () => obj.hello(); 
        const spy = sinon.spy(obj, 'hello'); // create a spy on hello
        sayHello();
        assert(spy.calledOnce);  // assert that the spy was called once
        spy.restore();  // restore original hello function
      })
    })