期望使用量角器和茉莉花测试 URL #fragment

expect to test url #fragment with protractor and jasmine

本文关键字:测试 URL #fragment 茉莉花 量角器 期望      更新时间:2023-09-26

我正在尝试使用量角器(带有茉莉花)为我的 Web 应用程序进行一些功能测试,我遇到的问题之一是:

如何检查我的 URL 中的当前哈希 (#fragment) 是什么?

例如,使用,browser.get('/')它应该导航到http://localhost:8000/#/ URL...其中#/是 Angular 在未启用 HTML5 模式时使用的 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$/);

或者,toEndWith()来自第三方jasmine-matchers匹配器:

expect(browser.getCurrentUrl()).toEndWith("#/user");

也可以使用内置toContain(),但这不会强制要求在字符串末尾显示子字符串:

expect(browser.getCurrentUrl()).toContain("#/user");

只需使用内置对象 - document.location.hash .它将仅返回哈希,如果 url 不包含它,则返回空字符串。

查找Location对象 - 它包含一些用于检索 url 部分的其他方法。