最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - expect to test url #fragment with protractor and jasmine - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Multiple 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.

发布评论

评论列表(0)

  1. 暂无评论