问题描述
我正在尝试验证焦点元素是否在页面加载时设置为我的测试之一.
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 调用,你可以比较两个 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'));
});
这篇关于我如何断言一个元素是焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!