Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to nest nightwatch.js commands in page sections?

I have a page pages/login.js looks like:

function fillAndSubmitLogin(email, password) {
  return this
  .waitForElementVisible('@emailInput')
  .setValue('@emailInput', email)
  .setValue('@passwordInput', password)
  .waitForElementVisible('@loginSubmitButton')
  .click('@loginSubmitButton');
}


export default {
  commands: [
    fillAndSubmitLogin
  ],
  elements: {
    emailInput: 'input#email',
    passwordInput: 'input[type=password]',
    TFAInput: 'input#token',
    loginSubmitButton: '.form-actions button.btn.btn-danger'
  }
};

I have another page pages/hompage.js homepage.js attempts to include pages/login.js as a section

import login from "./login.js";

module.exports = {
  url: 'http://localhost:2001',
  sections: {
    login: {
      selector: 'div.login-wrapper',
      ...login
    }
  }
};

I then have a test case that attempts to login on the hompage section

  'Homepage Users can login': (client) => {
    const homepage = client.page.homepage();
    homepage
    .navigate()
    .expect.section('@login').to.be.visible;


    const login = homepage.section.login;
    login
    .fillAndSubmitLogin('[email protected]', 'password');

    client.end();
  }

This test then fails with the following error

TypeError: login.fillAndSubmitLogin is not a function
       at Object.Homepage Users can login (/Users/kevzettler//frontend/test/nightwatch/specs/homepage.spec.js:32:6)
       at <anonymous>
       at process._tickCallback (internal/process/next_tick.js:182:7)

  login.fillAndSubmitLogin is not a function
       at Object.Homepage Users can login (/Users/kevzettler//frontend/test/nightwatch/specs/homepage.spec.js:32:6)
       at <anonymous>
       at process._tickCallback (internal/process/next_tick.js:182:7)
like image 853
kevzettler Avatar asked Jun 01 '18 22:06

kevzettler


1 Answers

According to the Nightwatch docs, any commands that are exported in page objects should be plain JavaScript objects with a key being a command name and the value being a function. For example:

var googleCommands = {
  submit: function() {
    this.api.pause(1000);
    return this.waitForElementVisible('@submitButton', 1000)
      .click('@submitButton')
      .waitForElementNotPresent('@submitButton');
  }
};

module.exports = {
  commands: [googleCommands],
  elements: //...etc ...
  // etc...
}

In this example, the module exports googleCommands, which is a command object which has a key (submit) and a corresponding function. I believe you should refactor your code as follows:

function fillAndSubmitLogin = {
  fillAndSubmitLogin: function(email, password) {
    return this
    .waitForElementVisible('@emailInput')
    .setValue('@emailInput', email)
    .setValue('@passwordInput', password)
    .waitForElementVisible('@loginSubmitButton')
    .click('@loginSubmitButton');
  }
};

Of course, you don't have to make the command name the same in both places (as the example shows (googleCommands/submit). This allows you to expose a variety of functions in one command. Hope that answers the question!

like image 55
Steve H. Avatar answered Nov 11 '22 10:11

Steve H.