温馨提示:本文翻译自stackoverflow.com,查看原文请点击:testing - How can I open a menu using a keyboard shortcut?
automated-tests e2e-testing testcafe testing browser-automation

testing - 如何使用键盘快捷键打开菜单?

发布于 2020-04-12 10:34:57

我们有一个快捷菜单,如果用户按下该.键,则会打开一个快捷菜单。我如何模仿用户的行为?从文档来看,该t.keyPress作品只能在输入上使用,而不能在其外部启动

查看更多

提问者
doron
被浏览
97
Arseniy Rubtsov 2020-02-04 02:10

您不需要输入元素t.keyPress即可工作。

编辑:

这是使用的简单测试t.pressKey('.')

import { Selector } from 'testcafe';

fixture`Press key`
  .page`./index.html`;

test('Press dot', async t => {
    await t        
        .pressKey('.')
        .expect(Selector('p').textContent).eql('Dot is pressed');
});

index.html

<html>
    <body>
        <p id='paragraph'>Press key</p>
        <script>
            document.addEventListener('keydown', event => {
                if (event.key === '.')
                    document.getElementById('paragraph').textContent = 'Dot is pressed';
            });
        </script>
    </body>
</html>