mucsi96 / nightwatch-api

[DEPRECATED] Cross-runner API for Nightwatch.js
https://nightwatch-api.netlify.com/
MIT License
84 stars 64 forks source link

waitForCondition support for nightwatch-api #787

Closed samtbabu closed 5 years ago

samtbabu commented 5 years ago

Expected Behavior

nightwatch-commands Package Support for nightwatch-api

I am trying to migrate my project from cucumer-nightwatch to nightwatch-api. In cucumer-nightwatch, I am using the nightwatch-commands package and I was trying to use the same with nightwatch-api.

Current Behavior

Getting the below error message while using the "nightwatch-commands": "^3.0.0" Package

Consol Log


  @end2end @only
  Scenario: Validate the Brand Page Title Displayed
    Given I open the "brandSearch" Page
√ Element <.ow-app> was visible after 44 milliseconds.
    × failed
      Error while running "waitForAjaxCompleted" command: Error while running "waitForCondition" command: "TypeError: Class constructor ProtocolActions cannot be invoked without 'new'
      "
      Error while running "waitForCondition" command: "TypeError: Class constructor ProtocolActions cannot be invoked without 'new'
      "
      TypeError: Class constructor ProtocolActions cannot be invoked without 'new'
          at new CommandAction (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch-commands\commands\waitForCondition.js:10:62)
          at new CommandInstance (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\api-loader\command.js:38:5)
          at Function.createInstance (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\api-loader\command.js:84:22)
          at CommandLoader.commandFn [as __commandFn] (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\api-loader\command.js:106:40)
          at TreeNode.execute (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\core\treenode.js:103:33)
          at TreeNode.runCommand (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\core\treenode.js:118:26)
          at TreeNode.run (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\core\treenode.js:81:17)
          at AsyncTree.runChildNode (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\core\asynctree.js:74:17)
          at AsyncTree.traverse (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\core\asynctree.js:41:19)
          at CommandQueue.traverse (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\core\queue.js:82:8)
          at Timeout.scheduleTimeoutId.setTimeout [as _onTimeout] (C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatch-api\packages\cucumber-selenium-example\node_modules\nightwatch\lib\core\queue.js:59:52)
          at ontimeout (timers.js:436:11)
          at tryOnTimeout (timers.js:300:5)
          at listOnTimeout (timers.js:263:5)
          at Timer.processTimers (timers.js:223:10)

Dependencies used

  "devDependencies": {
    "@babel/cli": "^7.5.5",
    "@babel/core": "^7.5.5",
    "@babel/plugin-transform-spread": "^7.2.2",
    "@babel/preset-env": "^7.5.5",
    "@babel/register": "^7.5.5",
    "@ow/acceptance-tests-common": "^2.7.10",
    "chromedriver": "76.0.0",
    "cross-env": "^5.2.0",
    "cucumber": "^5.1.0",
    "cucumber-html-reporter": "^5.0.0",
    "cucumber-pretty": "^1.5.0",
    "edgedriver": "^4.17134.1",
    "geckodriver": "^1.16.2",
    "iedriver": "^3.14.1",
    "mkdirp": "^0.5.1",
    "nightwatch": "^1.1.12",
    "nightwatch-api": "^2.3.0",
    "nightwatch-commands": "^3.0.0",
    "nightwatch-custom-commands-assertions": "^1.1.1",
    "npm-run-all": "^4.1.5",
    "selenium-server-standalone-jar": "^3.141.5"

Nightwatch Config

module.exports = {
  custom_commands_path: [
    './node_modules/nightwatch-commands/commands'
  ],
  page_objects_path: '../pagemodels',
.....

Using Custom Command in Step Definition

Given(/^I open the "([^"]*)" Page$/, async pagemodel => {
  World.currentPageModel = client.page[pagemodel]();
  await World.currentPageModel
    .navigate()
    .waitForElementVisible('@root', PAGE_WAIT_TIME)
    .waitForAjaxCompleted();
});

waitForAjaxCompleted function definition in the Package: nightwatch-commands

exports.command = function(callback) {
    var client = this;

    // Uses waitForCondition to run code within the client browser
    //
    return client.waitForCondition('return jQuery.active;', 8000, function(result) {
        if (typeof callback === 'function') {
            callback.call(client, result);
        }
    });

};

What are you trying to accomplish?

nightwatch-commands Package has very useful methods, which can we used along with Nightwatch-api

@mucsi96 - Any help is much appreciated and let me know if you need any details from my side.

spnraju commented 5 years ago

Hi @samtbabu , thanks for bringing this to our notice. Could you kindly help with below details?

I assume you have updated your nightwatch version as cucumber-nightwatch is not compatible with nightwatch v1. Did you try running the package with pure nightwatch? Meaning, without nightwatch-api.

samtbabu commented 5 years ago

.

Hi @samtbabu , thanks for bringing this to our notice. Could you kindly help with below details?

I assume you have updated your nightwatch version as cucumber-nightwatch is not compatible with nightwatch v1. Did you try running the package with pure nightwatch? Meaning, without nightwatch-api.

Hi @spnraju , Thanks for the quick response. Please find the inline response

Q. I assume you have updated your nightwatch version as cucumber-nightwatch is not compatible with nightwatch v1 ?

Answer: Yes, I updated the nightwatch to 1.1.12 for using it with nightwatch-api. When I am using the nightwatch-cucumber, I used the nightwatch version: "^0.9.21"

Q. Did you try running the package with pure nightwatch? Meaning, without nightwatch-api. Answer:

When I was using nightwatch-cucumber package, I use the below statement.

const { client } = require('nightwatch-cucumber');

Step Definition using nightwatch-cucumber

const { client } = require('nightwatch-cucumber');

const PAGE_WAIT_TIME = 20000;

module.exports = (Given, Then, When, World) => {
  Given(/^I open the (.*) page/, pageModel => {
    World.currentPageModel = client.page[pageModel]();
    // @root signifies the class to look for to identify a page.
    return World.currentPageModel
      .navigate()
      .waitForElementVisible('@root', PAGE_WAIT_TIME)
      .waitForXHRComplete(PAGE_WAIT_TIME);
  });

Step Definition using nightwatch-api

const { client } = require('nightwatch-api');
const PAGE_WAIT_TIME = 20000;

Given(/^I open the "([^"]*)" Page$/, async pagemodel => {
  World.currentPageModel = client.page[pagemodel]();
  await World.currentPageModel
    .navigate()
    .waitForElementVisible('@root', PAGE_WAIT_TIME)
    .waitForAjaxCompleted();
});

I am not sure I understand your second question correctly, Please let me know, if you need further information.

spnraju commented 5 years ago

Hi @samtbabu my second question is more about using nightwatch without cucumber i.e neither Nightwatch-api nor cucumber-nightwatch packages. Could you check the sample here https://github.com/spnraju/nightwatchjs-selenium-example

samtbabu commented 5 years ago

@spnraju - Thanks for the clarification. I tried integrating the nightwatch-commands package with pure nightwatch and I am able to reproduce the issue, which proves it's an issue with nightwatch package rather than nightwatch-api

I try taking this up with the nightwatch-commands team.

@spnraju It will be greatly appreciated if you can point us to some packages related to custom commands, which supports nightwatch-api package.

Console Error Log

λ npm run chrome

> nightwatchjs-selenium-example@1.0.0 chrome C:\Users\sthoma40\MyDocs\Officeworks\Automation\nightwatchjs-selenium-example
> nightwatch -e chrome

[Github] Test Suite
===================
Running:  Demo test GitHub

   TypeError: Error while running "waitForCondition" command: "TypeError: Class constructor ProtocolActions cannot be invoked without 'new'
  "

FAILED: 1 errors (1.355s)
   TypeError: Error while running "waitForCondition" command: "TypeError: Class constructor ProtocolActions cannot be invoked without 'new'
  "

github.js

module.exports = {
  tags: ['git'],
  'Demo test GitHub': function (client) {
    client
      .url('https://github.com/nightwatchjs/nightwatch')
      .waitForAjaxCompleted()
      .waitForElementVisible('body', 1000)
      .getTitle(function (result) {
        client.assert.equal(result.indexOf('GitHub - nightwatchjs/nightwatch'), 0, 'Page title starts with nightwatch')
      })
      .assert.visible('.container h1 strong a')
      .assert.containsText('.container h1 strong a', 'nightwatch', 'Checking project title is set to nightwatch');
  },

  after: function (client) {
    client.end();
  }
};

nightwatch.conf.js

module.exports = {
  src_folders: ['tests'],
  output_folder: 'output/reports',
  custom_commands_path: './node_modules/nightwatch-commands/commands',
  custom_assertions_path: '',
  page_objects_path: '',

Package.json

  "devDependencies": {
    "chromedriver": "^75.1.0",
    "edgedriver": "^4.17134.1",
    "geckodriver": "^1.16.2",
    "iedriver": "^3.14.1",
    "nightwatch": "^1.2.1",
    "nightwatch-commands": "^3.0.0",
    "selenium-server": "^3.141.59"
  }