Warm tip: This article is reproduced from stackoverflow.com, please click
automated-tests e2e-testing testcafe testing browser-automation

How can I open a menu using a keyboard shortcut?

发布于 2020-04-11 22:27:30

We have a shortcut menu that if the user presses the . key, a shortcut menu opens. How can I mimic the user's behavior? From the docs, the t.keyPress works only on inputs and cannot be initiated out side it

Questioner
doron
Viewed
57
Arseniy Rubtsov 2020-02-04 02:10

You do not need an input element for t.keyPress to work.

Edit:

Here is a simple test using 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>