问题描述
我试图验证焦点元素被设置在页面加载作为我的测试之一。
I am attempting to verify that the focused element is set on page load as one of my tests.
这似乎是工作,我可以用元素Explorer来检验,但茉莉的匹配似乎没有拿起这一点。
This seems to be working, and I can verify with the element explorer, but the Jasmine matchers don't seem to pick up on this.
下面是我的code:
var LoginPage = function () {
this.basePath = browser.params.baseUrl;
this.loginPart = "/#/login";
this.usernameInput = element(by.model('username'));
this.get = function () { ... }
}
it('should focus on the username field on load', function () {
loginPage.get();
expect(loginPage.usernameInput).toBe(browser.driver.switchTo().activeElement());
});
本场本身是正确获取焦点在页面加载时(和元素探险正确地让我通过 browser.driver.switchTo()。activeElement()
查询此,所以我觉得这个测试应该是合格的,但事实并非如此。
The field itself is correctly getting focus when the page loads (and element explorer correctly allows me to query this via browser.driver.switchTo().activeElement()
, so I think this test should be passing, but it isn't.
相反,我得到它并没有提供任何有用的信息,一个巨大的堆栈跟踪。
Instead I get an enormous stacktrace which doesn't offer any useful information.
推荐答案
我想我找到了一个解决方法:
I think I found a workaround:
由于期望
期望与承诺被调用,您可以比较两个webElements的一些属性(你的输入和当前 activeElement
)
Since expect
expects to be called with a promise, you can compare some attribute of the two webElements (your input and the currently activeElement
) :
it('should focus on the username field on load', function () {
loginPage.get();
expect(loginPage.usernameInput.getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));
});
这篇关于我如何确认一个元素的重点是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!