urlHash - What about just setting the hash instead of starting from the "launch_url"??

352 views
Skip to first unread message

Juri Strumpflohner

unread,
Mar 18, 2014, 8:43:00 AM3/18/14
to nightw...@googlegroups.com
Hi,

I'm currently trying to use Nightwatch to test single page JavaScript applications. As it is common practice you use the hash as the client-side routing mechanism. My scenario is that I have to first perform a login on the application I'm testing (simple forms authentication) which will then redirect me to some destination in the app, like I'm requesting http://localhost:1234/Account/Login for the authentication which will then redirect me to http://localhost:1234/some/dynamic/url/index.html#!home.

So far so good. What I'd like to do is to navigate to specific points within my SPA by setting the hash from #!home to #!person/edit etc... I thought to do that by using the urlHash(..) but it turned out that this doesn't work as expected as it uses the configured "launch_url". The problem is that I do not know that upfront as the path of the application potentially contains variable keys...

What was the intention on making urlHash(..) perform as it currently does rather than just replacing the current hash with the provided one?? Are there any possibilities to work around this issue/to realize the behavior I'm describing??

Thx for any help :)

Andrei Rusu

unread,
Mar 18, 2014, 9:39:53 AM3/18/14
to nightw...@googlegroups.com
I see. You can't set only the hash with selenium webdriver. You need to specify the entire url. Usually with single page apps the url remains fixed and you only need to change the hash so that's why the urlHash behaves this way. 

So in your case you would need to do a call to url with passing just the callback to get the current url and build a custom command that will reuse this value.

var baseURL = '';
client.url(function(result) {
  baseURL = result.value;
});

client.< customHashChangeCommand >(function() {
  return baseURL;
}, '#home');

And your custom command will look something like this:

exports.command = function(baseUrlResolver, hash, optionalCallback) {
  var baseUrl = baseUrlResolver();
  return this.url(baseUrl + hash, optionalCallback);   
}

Some more info about custom commands: http://nightwatchjs.org/guide#custom-commands
Message has been deleted

Juri Strumpflohner

unread,
Mar 18, 2014, 11:56:48 AM3/18/14
to nightw...@googlegroups.com
Hi,

thx, works like a charm. I just adapted the code slightly s.t. previous hashes are being removed from the url before adding the new one.

Thx again
Reply all
Reply to author
Forward
0 new messages