3

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?

3
  • If you know the domain, the course #, and the name of the final page... is there some reason you can't put those three together and make the URL from scratch?
    – JeffC
    Commented Sep 21, 2017 at 1:44
  • That's just it, I won't know the course code, since it changes every time. Commented Sep 21, 2017 at 14:46
  • My guess is that you can get it from parsing a link on the page that contains the course code, e.g. get the href from an A tag and parse out the course #.
    – JeffC
    Commented Sep 21, 2017 at 15:11

4 Answers 4

2

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

1
  • 2
    How can I use this variable outside the function scope?
    – Basti
    Commented Aug 15, 2018 at 13:56
0

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.

0

You also need to dereference the value

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

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();
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.