Nightwatch: Using `initClient` throws a TypeError

Created on 14 Jan 2019  路  8Comments  路  Source: nightwatchjs/nightwatch

I'm attempting to upgrade from Nightwatch 0.9.19 to 1.0.18 and running into some problems. I use nightwatch with normal Mocha as follows

"use strict";

const nightwatch = require("nightwatch");

suite("test", function() {
  suiteSetup(function() {
    this.client = nightwatch.initClient({
      selenium_host: "localhost",
      silent: true,
      desiredCapabilities: {
        browserName: "chrome",
        javascriptEnabled: true,
        acceptSslCerts: true,
        chromeOptions: { args: ["--no-sandbox"] }
      }
    });
    this.browser = this.client.api;  // changed from api() to api for 1.x.x
  });

  test("demo test google", function(done) {
    this.browser
      .url("http://www.google.com")
      .waitForElementVisible("body")
      .setValue("input[type=text]", "nightwatch")
      .waitForElementVisible("input[name=btnK]")
      .click("input[name=btnK]")
      .pause(1000)
      .assert.containsText("#main", "Night Watch")
      .end();

    this.client.start(done);
  });
});

Invoking mocha as follows:

npx mocha --ui tdd

results in the following output:

  1) test
       "before all" hook:
     TypeError: Cannot read property 'verbose' of null
      at Settings.setCliOptions (node_modules/nightwatch/lib/settings/settings.js:177:19)
      at Settings.adaptSettings (node_modules/nightwatch/lib/settings/settings.js:55:10)
      at Settings.init (node_modules/nightwatch/lib/settings/settings.js:235:10)
      at Function.parse (node_modules/nightwatch/lib/settings/settings.js:20:22)
      at CliRunner.initTestSettings (node_modules/nightwatch/lib/runner/cli/cli.js:26:35)
      at Object.Nightwatch.initClient (node_modules/nightwatch/lib/index.js:518:13)
      at Context.<anonymous> (test.js:7:30)

System Information:

Ubuntu 18.04.1 LTS x86_64
Node.js 10.15.0
stale

Most helpful comment

I made a workaround for this issue:

import * as nightwatch from "nightwatch"

const nightwatchOptions = {
// ...
}

const cliRunner = nightwatch.CliRunner()
cliRunner.initTestSettings(nightwatchOptions, {}, {}, null)

const client = nightwatch.client(cliRunner.test_settings)

All 8 comments

I believe this is caused by a bug in CliRunner#initTestSettings which defaults argv to null.

https://github.com/nightwatchjs/nightwatch/blob/8baa09dee8a1ee3b6533cf088c8e59760fe87076/lib/runner/cli/cli.js#L25-L32

Nightwatch.initClient(copts = {}) does the following:

  1. call Nighwatch.ClRunner() which defaults argv to {} and returns an instance of CliRunner with argv set to {}
  2. call initTestSettings(opts)

Why does initTestSettings default argv and testEnv to null instead of this.argv and this.testEnv given that it is an instance method

Argument with value null is not going to be replaced with "default" value. The only argument that will be replaced with default is undefined

So the code should be

  initTestSettings(opts = {}, baseSettings = undefined, argv = undefined, testEnv = undefined) {

right, I'm not sure what the developer intended with the current implementation as in is initTestSettings buggy or is initClient buggy

fwiw defaulting arguments to undefined kind of a noop as you don't pass in the argument it will just be undefined, no need to explicitly default assign undefined

I made a workaround for this issue:

import * as nightwatch from "nightwatch"

const nightwatchOptions = {
// ...
}

const cliRunner = nightwatch.CliRunner()
cliRunner.initTestSettings(nightwatchOptions, {}, {}, null)

const client = nightwatch.client(cliRunner.test_settings)

This issue has been automatically marked as stale because it has not had any recent activity.
If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.

Not stale?

This issue has been automatically marked as stale because it has not had any recent activity.
If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.

I'm seeing the same error with below code. Is this a actual bug in initClient()? Any help is appreciated, thanks!

async 'mydemo'(browser){

        var nightwatch = require('../node_modules/nightwatch');
        var config = require('../nightwatch.json');

        console.log(config);
        var client = nightwatch.initClient(config.test_settings.default);
        var browserObj = client.api();

        const homePage = browserObj.page.homepage();
        homePage.navigate();
        let result = await homePage.api.elements('css selector', 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr');
        console.log(" NightWatch Obj Row Length is " + "<->" + result.value.length);

    }

Nightwatch.json

{
  src_folders: [ 'Tests' ],
  page_objects_path: [ 'pageobjects' ],
  webdriver: {
    start_process: true,
    server_path: 'node_modules/chromedriver/lib/chromedriver/chromedriver',
    port: 9515
  },
  test_settings: {
    default: {
      launch_url: 'http://localhost:8080',
      desiredCapabilities: [Object]
    }
  }
}

Error:

FAILED: 1 errors (17ms)
TypeError: Cannot read property 'verbose' of null


Please let me know how to create browser object from a nightwatch.json file.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxgalbu picture maxgalbu  路  3Comments

davecranwell picture davecranwell  路  3Comments

dakebl picture dakebl  路  4Comments

manjero picture manjero  路  4Comments

danielbentov picture danielbentov  路  4Comments