Nightwatch: Important: .navigate() doesn't take any parameter(s)

Created on 12 Nov 2015  路  11Comments  路  Source: nightwatchjs/nightwatch

While i'm navigate with dynamical url.
I cannot pass any parameter in that method.

For instance, .navigate("en");

The pageObject:

module.exports = {
url : function (language) {
return this.api.launchUrl + '/login?lang=' + language;
},
elements : {
}
};

Most helpful comment

It's not well documented but navigate actually takes one optional parameter, which is a url to navigate to (defaults to the url property on the page object).

So you could do client.navigate(client.launchUrl + '/login?lang=en')

I see the use case though, we could consider adding an optional array parameter that gets passed if the page.url is a function

All 11 comments

@GBeauny you can add a navigate command on the page which could take a parameter

It's not well documented but navigate actually takes one optional parameter, which is a url to navigate to (defaults to the url property on the page object).

So you could do client.navigate(client.launchUrl + '/login?lang=en')

I see the use case though, we could consider adding an optional array parameter that gets passed if the page.url is a function

Thanks a lot, Guys :).

It will be useful as we have parameters in the url which evolve:

I see the use case though, we could consider adding an optional array parameter that gets passed if the page.url is a function

Thanks.

One obvious need for this is testing REST APIs. For example, the URL to a single item in a collection is

/myapp/things/1

The page object that displays a thing needs to be able to say

url : function (id) {
return this.api.launchUrl + '/things/'+id;
}

Has there been any progress on this?

(Note that client.navigate() could still take a URL parameter, it's only client.page.navigate that needs to pass its parameter to the page url function)

@GreenAsJade I have an open PR for this: https://github.com/nightwatchjs/nightwatch/pull/929

Just this second looked for this functionality. We have an angular app with dynamic URLs...

hardcoding an url into a page-object is not a good practice. We have to divide the code and the values used in tests. URL, usernames/passwords etc. all are values, which will change with another testing environment. And we want to run our tests flexible with different environments and different data. Do we?

So i recommend to use .navigate() always with an optional parameter, store hostname, port and fixed part of the path elsewhere (JSON) and just local path to keep in the Page object.

something like this:
.navigate(appdata.['environment'].hostname + pageObject.url)

Does this help?
browser.url(theUrl);

Afaik you can implement .url as a function inside a page-object and return dynamic values based on your environment. Iirc there is also an example for it in the _API_ docs.

~david

This would be nice to have. We made things better thanks to @sknopf's solution, but passing arguments to the url would be cleaner. Any progress on it?

(@DeeMusil, I can see where you're coming from. In our case, the host part of the URL is already dynamic. Arguably, everything in the URL apart from the host is part of the page's interface, just like CSS selectors. Looking at the problem from that direction, it's OK for us to have those URLs in the page objects.)

Was this page helpful?
0 / 5 - 0 ratings