如何模拟赛普拉斯上的长按按键? [英] How to simulate long hold key press on cypress?

查看:54
本文介绍了如何模拟赛普拉斯上的长按按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模拟长按Backquote键而不选择某些dom元素,我们如何使用cypress来实现呢?我的Web应用程序具有一项功能,当按下此键时会触发该功能.

I want to simulate a long key press of backquote key without selecting certain dom element, how can we achieve this using cypress? I have a functionality on my web app that will trigger when this key is pressed.

我在下面尝试了几种代码,但没有任何效果.

I have tried several code below and nothing works.

// 1st
cy.get('#sidepanel').trigger('keydown', { keycode: 192, release: false })

// 2nd
cy.get('body').type('`', { release: false })

// 3rd
cy.get('body').trigger('keydown', { keycode: 192, release: false })
cy.wait(15000)
cy.get('body').trigger('keyup', { keycode: 192, release: false })

我希望它将模拟backqoute的长按并按住键,但是看起来backqoute的键入仅发生一次而不是按住.

I expect it will simulate the long and hold key press of backqoute, but it looks like the typing of backqoute only happens once and not hold.

推荐答案

我不知道为什么,但是几个月前尝试Zach Bloomquist回答时,它不起作用.刚才我再次尝试过,它有效.我在另一台计算机上尝试过该软件,然后重新安装了cypress 3.3.2版.不确定是否会产生不同的影响.

I don't know why, but when trying Zach Bloomquist answer months ago, it didn't work. Just now I tried again, it works. I tried it on different machine and re-install cypress version 3.3.2. Not really sure if it has different impact.

以某种方式,Zach的解决方案无法提供接近实际按下时间的键.实际上,运行他的解决方案将在Cypress运行时按下按键28秒钟.

Somehow, solution from Zach does not give close to actual time key pressed. Running his solution actually will press the key for 28 seconds in Cypress runtime.

// expect: holding down for 5 seconds
// actual: cypress will hold the keys for 28 seconds
for (var i = 0; i < 100; i++) {
  cy.get('body').trigger('keydown', { keycode: 192, release: false })
  cy.get('body').trigger('keypress', { keycode: 192, release: false })
  cy.wait(50)
}
cy.get('body').trigger('keyup', { keycode: 192, release: false })

我想出了一个更简单的解决方案,该解决方案与在cypress中运行时的实际持续时间相匹配:

I have come up with simpler solution that match almost actual duration press time when run in cypress:

cy.get('body').trigger('keydown', { keyCode: 192 })
cy.wait(duration)
cy.get('body').trigger('keyup', { keyCode: 192 })

如果您清楚地看到此解决方案,则与问题中的第三个备选方案相同.以前它不起作用,但是现在它起作用了.我注意到,赛普拉斯运行时的浏览器类型有所不同.在镀铬之前,现在是Electron61.也许是原因,需要进一步调查以进行验证.

If you see clearly this solution the same like my third alternative in the question. Before it didn't work, but now it works. What I noticed different is the browser type when Cypress run. Before it's chrome, now Electron 61. Maybe it's the cause, need to investigate further to validate.

这篇关于如何模拟赛普拉斯上的长按按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆