热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用KeyboardEvent模拟测试的按键-UsingKeyboardEventtosimulateakeypressforatest

IwanttowriteaJavascripttestthatsimulatesakeypressontherightarrowkey.Hereismyattem

I want to write a Javascript test that simulates a keypress on the right arrow key. Here is my attempt to create the 'keydown' event:

我想编写一个模拟右箭头键上的按键的Javascript测试。这是我尝试创建'keydown'事件:

var data = {
   key: 'ArrowRight'
};
var ev = new KeyboardEvent('keydown', data);
console.log(ev);

http://jsfiddle.net/lsiden/6e7duck6/

When I examine the output in the developer console, I expect to see the value 39 in one of the event fields, but it appears to remain uninitialized.

当我在开发者控制台中检查输出时,我希望在其中一个事件字段中看到值39,但它似乎仍然未初始化。

KeyboardEvent {which: 0, keyCode: 0, charCode: 0, repeat: false, metaKey: false…}
altKey: false
bubbles: false
cancelBubble: false
cancelable: false
charCode: 0
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 0
eventPhase: 0
keyCode: 0
keyIdentifier: ""
keyLocation: 0
layerX: 0
layerY: 0
location: 0
metaKey: false
pageX: 0
pageY: 0
path: Array[0]
repeat: false
returnValue: true
shiftKey: false
srcElement: null
target: null
timeStamp: 1430931169397
type: "keydown"
view: null
which: 0
__proto__: KeyboardEvent
.

I tried to follow the docs: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent

我试着按照文档:https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent

Has anyone done this before?

有没有人这样做过?

2 个解决方案

#1


I won't lie, this seems odd to test. It sounds like you need to wrap all the functionality of that key press event into one function and then test that function.

我不会撒谎,这似乎很奇怪。听起来你需要将该按键事件的所有功能包装到一个函数中,然后测试该函数。

If you did that already and you need to test that your function is looking for a certain keyCode or something, then you can use plain Javascript objects and pass that into the function you're testing.

如果您已经这样做并且需要测试您的函数正在寻找某个keyCode或其他东西,那么您可以使用普通的Javascript对象并将其传递给您正在测试的函数。

e.g.

if your function looks like this.

如果你的功能看起来像这样。

function myFunc(evt) {
  if (evt.keyCode === 39) {
    doThis();
  } else {
    doThat();
  }
}

Then to test your function, all you need is an object with a keyCode equal to 39. Your function shouldn't care about the fact that it was triggered with a KeyBoardEvent object that has a bunch of other stuff attached to it that you don't need.

然后为了测试你的函数,你需要的只是一个keyCode等于39的对象。你的函数不应该关心它是由一个KeyBoardEvent对象触发的,它有一堆附加在它​​上面的其他东西你不能需要。

So your test will look like this.

所以你的测试看起来像这样。

var keyboardEvent = {keyCode: 39};
myFunc(keyboardEvent);
// Make sure that doThis() was called and not doThat()

I think that would solve your problem and make things simpler.

我认为这可以解决您的问题并使事情变得更简单。

#2


I was having the same problem and realized that it works in Firefox, but not Chrome. Chrome doesnt seem to use 'key' attribute in the KeyboardEvent. You have to use 'keyIdentifier'. To test in both browsers, you can do this:

我遇到了同样的问题,并意识到它适用于Firefox,但不适用于Chrome。 Chrome似乎没有在KeyboardEvent中使用'key'属性。你必须使用'keyIdentifier'。要在两种浏览器中进行测试,您可以这样做:

var data = {
       key: 'ArrowRight',
       keyIdentifier:'ArrowRight'
    };
var ev = new KeyboardEvent('keydown', data);
console.log(ev); 

推荐阅读
author-avatar
cshaadi_915
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有