我有一个Jasmine测试,其中有两个输入字段。我正在关注第一个输入,然后模拟按下“tab”键,并期望焦点在第二个输入上。不幸的是,情况并非如此。焦点没有从第一个改变,我的测试失败了。如何修复这个问题,使失败的测试通过?
试图测试的Fiddle链接: http://jsfiddle.net/G2Qz3/1/ 失败的Jasmine测试的Fiddle链接: http://jsfiddle.net/mFUhK/4/ HTML:
试图测试的Fiddle链接: http://jsfiddle.net/G2Qz3/1/ 失败的Jasmine测试的Fiddle链接: http://jsfiddle.net/mFUhK/4/ HTML:
<input id="first"></input>
<input id="second"></input>
JavaScript:
function simulateTab() {
var TAB_KEY = 9;
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod]("keydown", true, true, window, 0, 0, 0, 0, 0, TAB_KEY);
document.dispatchEvent(keyboardEvent);
}
describe('input tabbing test', function() {
beforeEach(function() {
document.getElementById('first').focus();
});
//this passes
it('input with id "first" should be focussed', function() {
expect(document.activeElement.getAttribute('id')).toBe('first');
});
//this fails
it('input with id "second" should be focussed after tabbing', function() {
simulateTab();
expect(document.activeElement.getAttribute('id')).toBe('second');
});
});