Nightwatch: Page Object .navigate() method should allow params passing to url function

Created on 28 Sep 2017  路  3Comments  路  Source: nightwatchjs/nightwatch

I've got a page object like this:

somePage.js:

module.exports = {
    url: function(fooId = 123) { 
        return `${this.api.launchUrl}/foo/${fooId}/bar`; 
    },
    sections: {
        ...blah
    }   
};

I'd like to be able to use it for testing any number of different users with varying access to different fooIds. So when I want to test user X's ability to access 123 I feel it would make sense to use the exact same page object as user Y, who can only access 456.

e.g:

"Can user access their private foos": function (browser) {
    var somePage = browser.page.somePage();
    somePage.navigate(456).waitForElementVisible('body', 10000, false, function() {
        // Yay;
    });     
}

Is this possible? Is there any other way of doing it? Or am I misunderstanding something fundamental about how to architect Page Objects so they're generic?

It seems my only way to achieve this is thus, which seems avoidably verbose:

"Can user access their private foos": function (browser) {
        var somePage = browser.page.somePage();
        var newUrl = somePage.url(456);
    browser.url(newUrl).waitForElementVisible('body', 10000, false, function() {
        //Yay
    });
}
stale

Most helpful comment

pretty sure you can write your own navigate func like this

function fNavigate(somePath) {
  return this.api
    .url(`${this.api.launchUrl}/foo/${somePath}/bar`)
    .waitForElementVisible('body', 10000, false);
}

module.exports = {
  commands: [
    {
      navigate: fNavigate,
    },
  ],
  elements: { },
};

and then calling

"Can user access their private foos": function (browser) {
        var somePage = browser.page.somePage();

    somePage
            .navigate(456)
            .click('xyz') // some other chained command
            .perform(() => { // do something async }));
}

All 3 comments

pretty sure you can write your own navigate func like this

function fNavigate(somePath) {
  return this.api
    .url(`${this.api.launchUrl}/foo/${somePath}/bar`)
    .waitForElementVisible('body', 10000, false);
}

module.exports = {
  commands: [
    {
      navigate: fNavigate,
    },
  ],
  elements: { },
};

and then calling

"Can user access their private foos": function (browser) {
        var somePage = browser.page.somePage();

    somePage
            .navigate(456)
            .click('xyz') // some other chained command
            .perform(() => { // do something async }));
}

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.

pretty sure you can write your own navigate func like this

This no longer works as I get the error

Error: Trying to overwrite page object/section "providers"  method/property "navigate".

(where providers is the name of my page object)

Was this page helpful?
0 / 5 - 0 ratings