[英]expect to test url #fragment with protractor and jasmine
我正在尝试使用量角器(使用jasmine)为我的Web应用程序执行一些功能测试,我遇到的一个问题是:
如何检查我的网址中的当前哈希值(#fragment)是什么?
例如,使用, browser.get('/')
它应该导航到http://localhost:8000/#/
url ...其中#/
是当html5模式时angular使用的url #fragment(哈希)未启用。
browser.getCurrentUrl()
将返回自http://
以来的绝对URL,而不仅仅是我要测试的片段。
有没有办法以正确的方式测试这个案例?
我希望做这样的事情:
browser.get('/');
expect(browser.getCurrentUrlFragment()).toBe('#/');
browser.get('/user/');
expect(browser.getCurrentUrlFragment()).toBe('#/user');
那可能吗?
谢谢。
在这种情况下可以使用多个匹配器,例如toMatch()
,最后是$
:
expect(browser.getCurrentUrl()).toMatch(/#\/user$/);
或者,来自jasmine-matchers
toEndWith()
匹配jasmine-matchers
:
expect(browser.getCurrentUrl()).toEndWith("#/user");
也可以使用内置的toContain()
,但这不会强制在字符串末尾预期的子字符串:
expect(browser.getCurrentUrl()).toContain("#/user");
只需使用内置对象 - document.location.hash
。 如果url不包含散列,它将仅返回散列或空字符串。
查找Location
对象 - 它包含一些其他用于检索url部分的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.