Elena Florko
Elena Florko

Reputation: 41

Nightwatch: Using of custom commands inside page objects

For the new version of a product I decided to try a page objects approach instead of using views and probably I started to use it wrong.

We have a custom command that simply waits for an element and clicks (waitAndClick.js):

exports.command = function(selector, callback) {
  return this
    .waitForElementPresent(selector, 30000)
    .click(selector, callback);
};

It works perfectly inside the test:

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

defineSupportCode(({Given, Then, When}) => {
    Given(/^I enable Dashboard management$/, () => {
        return client.waitAndClick('[id=enableManagement]');
    });
});

But when I am trying to use it inside the page object it throws an error:

module.exports = {
  url() {
    return this.api.launchUrl;
  },
  elements: {
    username: '[name="_Nitro_Login_username"]',
    password: '[name="_Nitro_Login_password"]',
    enter_button: '[title="Enter"]'
  },
  commands: [
    {
      loginAs(username, password) {
        return this.waitForElementVisible('@username', 50000)
          .setValue('@username', username)
          .setValue('@password', password)
          .waitAndClick('@enter_button')
          .waitForElementNotPresent('@enter_button', 50000);
      }
    }
  ]
};

I also tried with .api.waitAndClick('@enter_button'), the same result.

And the error message:

Error while running click command: Provided locating strategy is not supported: [title="enter"]. It must be one of the following:

class name, css selector, id, name, link text, partial link text, tag name, xpath

at Object.exports.command (/Users/eaflor/dev/jive-next/test/ui/commands/waitAndClick.js:9:63)

at Object.F.command (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/api.js:274:31)

at Object.commandFn (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/api.js:287:24)

at AsyncTree.runCommand (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/queue.js:154:30)

at AsyncTree.runChildNode (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/queue.js:114:8)

at AsyncTree.walkDown (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/queue.js:80:10)

at AsyncTree.walkUp (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/queue.js:97:8)

at AsyncTree.walkDown (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/queue.js:90:12)

at AsyncTree.traverse (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/queue.js:73:8)

at F.onCommandComplete (/Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/core/queue.js:131:12)

at F.g (events.js:291:16)

at emitNone (events.js:86:13)

at F.emit (events.js:185:7)

at /Users/eaflor/dev/jive-next/node_modules/nightwatch/lib/api/client-commands/_locateStrategy.js:18:10

at _combinedTickCallback (internal/process/next_tick.js:67:7)

at process._tickCallback (internal/process/next_tick.js:98:9)

Is it even possible to use custom commands inside the page object?

Upvotes: 2

Views: 6708

Answers (3)

Dan Joseph Palisoc
Dan Joseph Palisoc

Reputation: 161

Too late for an answer here but might help others facing similar issue. Returning this could fix the chaining issue from the page object.

exports.command = function(selector, callback) {
  this
    .waitForElementPresent(selector, 30000)
    .click(selector, callback);
  return this;
};

Upvotes: 0

Marko Mandic
Marko Mandic

Reputation: 11

I'm still new with Nightwatch, but I'm using commands in Page Objects like this:

commands: [
    {
        login: function() {
            this.api
                .waitForElementVisible('body', 2000)
                .setValue(this.elements.username.selector, user.loginUser) //here I'm inputting username from json file into element that I've defined in this page object
                .setValue(this.elements.password.selector, pass.loginPass) //here I did same thing with password
                .pause(500)
                .click(this.elements.submitButton.selector) //here I'm clicking on predefined button element               
        }
    }
]

Works perfectly, and it's very readable. This is simple login command. Hope this helps.

Cheers

Upvotes: 0

Elena Florko
Elena Florko

Reputation: 41

I found the way to fix it. In order to use custom commands in page objects you have to write them in class-style: http://nightwatchjs.org/guide#writing-custom-commands

Here how it should look like:

var util = require('util');
var events = require('events');

function waitAndClick() {
    events.EventEmitter.call(this);
}

util.inherits(waitAndClick, events.EventEmitter);

waitAndClick.prototype.command = function(selector) {
    const api = this.client.api;

    api
            .waitForElementPresent(selector)
            .click(selector, () => {
                this.emit('complete');
            })
    ;

    return this;
};

module.exports = waitAndClick;

Hope it will help someone.

Upvotes: 2

Related Questions