I'm trying to use protractor (with jasmine) to do some functional tests for my web application, and one of problem I came across is:
how to check what is the current hash (#fragment) in my url?
for example using, browser.get('/')
it should navigate to http://localhost:8000/#/
url ... where #/
is the url #fragment (the hash) that is used by angular when html5 mode is not enabled.
the browser.getCurrentUrl()
will return the absolute url since http://
and not just the fragment which I want to test.
is there a way to test this case in a correct way?
I would expect to do something like this:
browser.get('/');
expect(browser.getCurrentUrlFragment()).toBe('#/');
browser.get('/user/');
expect(browser.getCurrentUrlFragment()).toBe('#/user');
is that possible?
thank you.
I'm trying to use protractor (with jasmine) to do some functional tests for my web application, and one of problem I came across is:
how to check what is the current hash (#fragment) in my url?
for example using, browser.get('/')
it should navigate to http://localhost:8000/#/
url ... where #/
is the url #fragment (the hash) that is used by angular when html5 mode is not enabled.
the browser.getCurrentUrl()
will return the absolute url since http://
and not just the fragment which I want to test.
is there a way to test this case in a correct way?
I would expect to do something like this:
browser.get('/');
expect(browser.getCurrentUrlFragment()).toBe('#/');
browser.get('/user/');
expect(browser.getCurrentUrlFragment()).toBe('#/user');
is that possible?
thank you.
Share Improve this question edited Mar 14, 2016 at 13:33 Scantlight asked Mar 13, 2016 at 17:14 ScantlightScantlight 2533 silver badges13 bronze badges2 Answers
Reset to default 6Multiple matchers can be used in this case, for example toMatch()
with a $
at the end:
expect(browser.getCurrentUrl()).toMatch(/#\/user$/);
Or, toEndWith()
matcher from jasmine-matchers
third-party:
expect(browser.getCurrentUrl()).toEndWith("#/user");
The built-in toContain()
could also be used, but this would not enforce the substring to be expected at the end of the string:
expect(browser.getCurrentUrl()).toContain("#/user");
Just use built-in object - document.location.hash
. It will return only hash, or empty string if url doesn't contain it.
Look up Location
object - it contains a few other methods for retrieving url parts.