简体   繁体   English

Testcafe .presskey 用于多次按下

[英]Testcafe .presskey for multiple presses

I'm trying to write TestCafe Javascript to test a webpage using the keyboard as sole navigation (ie 'tabbing through the options').我正在尝试编写 TestCafe Javascript 来测试使用键盘作为唯一导航的网页(即“选项卡”)。

TestCafe runs these tests, but they run extremely fast (5 seconds max), and nothing happens on the screen. TestCafe 运行这些测试,但它们运行得非常快(最多 5 秒),屏幕上没有任何反应。 So I'm wondering if it's even actually working.所以我想知道它是否真的有效。

On top of that, I'm trying to find a way to DRY out my code.最重要的是,我试图找到一种方法来干掉我的代码。 From what I read in the docs, each time I want a key pressed, I need to call .pressKey('tab').从我在文档中读到的内容来看,每次我想要按下一个键时,我都需要调用 .pressKey('tab')。 If I need to hit 'tab' 5 times in a row, I have 5 lines of that in my code.如果我需要连续点击“tab”5 次,我的代码中有 5 行。 Is there any way to eliminate this unnecessary repetition?有没有办法消除这种不必要的重复?

Thanks!谢谢!

TestCafe sets focus to the next element on the page when the .pressKey('tab') action is called..pressKey('tab')操作时,TestCafe 将焦点设置到页面上的下一个元素。 To make your code cleaner, you can pass several keys separated with spaces to the pressKey action.为了使您的代码更pressKey ,您可以将几个用空格分隔的键传递给pressKey操作。

I've created a simple example for this stackoverflow page:我为这个 stackoverflow 页面创建了一个简单的例子:

import { Selector } from 'testcafe';

fixture `stackoverflow`
    .page `https://stackoverflow.com/questions/46612440/testcafe-presskey-for-multiple-presses`;

test('tab', async t => {
    await t
        .click(Selector('#search').find('[name="q"]'))
        .pressKey('tab tab tab tab tab tab tab tab tab tab tab tab tab tab');
});

Here is a screencast that demonstrates how it works (I've set the test run speed to 0.5 via the --speed option): https://www.screencast.com/t/dERD60nGc4f这是一个演示其工作原理的截屏视频(我已通过--speed选项将测试运行速度设置为 0.5): https : //www.screencast.com/t/dERD60nGc4f

If you want to slow it down to visually check, you can interleave calls to wait(x)如果您想放慢速度以进行目视检查,您可以交错调用 wait(x)

  await t.pressKey(TAB);
  await t.wait(800);
  await t.pressKey(TAB);
  await t.wait(800); 

etc.等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM