噶测试用例:焦点事件
我测试用因果报应测试用例angularjs一个焦点事件。但该元素是没有得到关注。我这样做之后的 element.focus()并检查元素
重点是,我得到错误的。和 document.activeElement 在这一点上是全身。
I am testing a focus event in angularjs using karma test cases. But the element is not getting focused. Right after I do element.focus() and check if the element is focused, I get false. And document.activeElement at that point is the whole body.
请提出一个解决方案。谢谢
Please suggest a solution. Thanks
(此答案由测试为重点的AngularJS指令一>)
您需要将您的元素添加到文档正文,以便 document.activeElement 的发挥它。
You need to add your element to the document body in order for document.activeElement to play with it.
您致电前 element.focus()
,做到这一点:
Before you call element.focus()
, do this:
element.appendTo(document.body);
另外,如果你在单元测试中这样做,我建议你在测试后删除元素,否则每次测试都会添加另一个元素体(以及它可能会影响你的其他测试的结果)。
Also, if you're doing this in unit tests I would recommend that you remove the element after the test, otherwise each test will add another element to the body (and it could affect your other tests' results).
afterEach(function () {
element.remove();
}