'Calling custom command in nightwatch.js

What I'd like to do is call a custom command within another custom command in nightwatch.js (with the aim of making the tests less brittle).

For example, in my first custom command (message1.js), I could have the following code;

exports.command = function(browser) {
  this
    browser
      console.log('display this first')
  return this;

and subsequently in my second custom command (message2.js), I want to call on the message1.js command first, then perform the rest of the code.

For example;

exports.command = function(browser) {
 this
   browser
     //call the message1.js command
       console.log('display the second message')
 return this;

I've tried calling it with the method;

exports.command = function(browser) {
  this
    browser
      .message1();
        console.log('display the second message')
  return this;

but this didn't work.

So my question(s) is;

Is it possible to call one custom command in another custom command And if not, is there another way of doing this?



Solution 1:[1]

Yes, you can chain your custom commands together or with other nightwatch methods:

Custom method openUrl.js:

exports.command = function(url) {
    const browser = this;    
    browser.url(url);
    return this;

}

Custom method openStackOverflow.js, that calls openUrl.js:

exports.command = function(url) {
    const browser = this;
    browser.openUrl(url).waitForElementVisible('#submit-button');
    return this;
    }

Using them in the tests:

'Open Stack Overflow': browser => {
    browser.openStackOverflow(`https://stackoverflow.com`);
 },

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 return