我如何断言一个元素是有焦点的? [英] How do I assert an element is focused?
问题描述
我正在尝试验证焦点元素是否在页面加载时设置为我的测试之一.
I am attempting to verify that the focused element is set on page load as one of my tests.
这似乎有效,我可以使用元素浏览器进行验证,但 Jasmine 匹配器似乎没有接受这一点.
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.
这是我的代码:
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:
由于 expect
期望通过 promise 调用,您可以比较两个 webElement 的某些属性(您的输入和当前的 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'));
});
这篇关于我如何断言一个元素是有焦点的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!