Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Nightwatch to show Chai assertions in the reporter?

I'm using Nightwatch JS v0.9.16 to run selenium / chai tests on my localhost. All the assertions work for nightwatch, but I can't get the chai assertion to show in the reporter.

This issue has been discussed here: https://github.com/nightwatchjs/nightwatch/issues/601 however it's been closed but there was no resolution... can anyone get this to work yet?

Here's my test:

var assert = require('chai').assert;

module.exports = {
  'pressing the "Servers" tab should change the URL to #servers' : function (client) {
    const pages = client.page,
          home_page = pages.home(),
          servers_page = pages.servers();

    home_page.navigate();
    servers_page.expect.element('body').to.be.present.before(1000);
    client.pause(1000);
    servers_page.click('@nav');
    servers_page.expect.element('@servers_div').to.be.present.before(1000);
    client.url(function(response){
        var currentUrl = response.value.replace(client.launch_url,"");
        console.log("url is ",response.value);

        //***CHAI ASSERTION doesn't get shown on reporter:***
        assert(currentUrl.indexOf('#servers')!=-1);

        client.end();
    });

  }
};

Screenshot of when this test passes shows all the assertions except the chai one:

chai test passed

And when it fails it shows AssertionError: Unspecified AssertionError:

chai test failure

Here's my test settings (nightwatch.json)

{
  "src_folders" : ["tests"],
  "output_folder" : "reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "./config/pages/",
  "globals_path" : "./config/globals.js",

  "selenium" : {
    "start_process" : false,
    "server_path" : "./libs/selenium-server-standalone-2-53-1.jar",
    "log_path" : "./logs/",
    "port" : 4444
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost:8081",
      "selenium_port"  : 9515,
      "selenium_host"  : "localhost",
      "default_path_prefix" : "",
      "screenshots" : {
        "enabled" : true,
        "path" : "./screens/",
        "on_failure": true
      },
      "desiredCapabilities": {
        "browserName": "chrome",
        "chromeOptions" : {
          "args" : ["--no-sandbox"]
        },
        "acceptSslCerts": true
      }
    }
  }
}

Versions:

"selenium-webdriver": "^3.0.1",
"nightwatch": "^0.9.8",
"chromedriver": "^2.25.2",
"chai": "latest"
like image 862
Katie Avatar asked Dec 08 '17 19:12

Katie


People also ask

What is the command to run the script Nightwatch?

You can add Nightwatch to your project simply by running npm install nightwatch --save-dev . This places the Nightwatch executable in your ./node_modules/.

How do you run a single test on Nightwatch?

A new parameter --testcase has been added to run a specified testcase. Show activity on this post. Test 2 and Test 3 will be executed. Separate each test function is mandatory because Nightwatch handled with filematcher each file.

What is Nightwatch automation?

End-to-End Automation Nightwatch. js is an integrated, easy to use End-to-End testing solution for web applications and websites, written in Node. js. It uses the W3C WebDriver API to drive browsers and perform commands and assertions on DOM elements.

How does Nightwatch js work?

Nightwatch works by communicating over a restful HTTP API with a WebDriver server (such as ChromeDriver or Selenium Server). The protocol is defined by the W3C WebDriver spec, which is derived from JSON Wire protocol. The latest version available in market is 1.0.


1 Answers

According to the doc, assert() takes a message as 2nd arg (http://www.chaijs.com/api/assert/), did you try it out? If so, was the message displayed?

Alternatively, have you tried with simpler tests (one failing, one not) for example: assert.isOk(false) & assert.isOk(true)?

Last, did you try running your chai assertion outside of client.url() 's callback?

I am not familiar with Nightwatch, but that's what I would have tried.

like image 141
Nicolas S. Avatar answered Nov 13 '22 21:11

Nicolas S.