Save url to string nightwatch

Viewed 4874

I'm automating QA testing for a marketing sim. I'm using BrowserStack and Nightwatch in node.js

I want to jump to a specific webpage during a run - trouble is, there is a unique course code that will change with each test run within the URL (eg. .../8759309/...). This course code stays constant throughout all the pages I want to go to. In this example, I want to save the current URL (www.domain.com/8759309/page-i-have) to a string so that I can insert the course code and jump to the next page (www.domain.com/8675309/page-i-want).

There seems to be a function in protractor for this (browser.getCurrentUrl()) but I can't seem to find one for NightWatch.

My question is, does such a function exist in NightWatch, and if not, can you think of any workarounds?

4 Answers

There is a way in Nightwatch but you didn't notice that, simply use browser.url() without argument but a callback can get the current URL.

var savedUrl='';
browser.url(function(result){
  savedUrl=result;
})

Check the api for details:
http://nightwatchjs.org/api#url

Nightwatch doesn't have that kind of a command built in. Since it sounds like you'd be using that functionality a lot, what I would do is write a javascript command to do that work, for you. you could even call it .getCurrentUrl. There's some documentation on creating custom commands, here: http://nightwatchjs.org/guide#writing-custom-commands

You could try using window.location.href to retrieve the url of the window, then save it as a string.

You also need to dereference the value

var savedUrl='';
browser.url(function(result){
  savedUrl=result.value;
})

I had to use .perform() to be able to use the current url variable.

let currentUrl;
browser.url(function(result) {
  currentUrl = result.value;
});
browser.perform(done => {
  console.log(currentUrl);
  done();
});

Related