在Protractor中模拟慢速键入 [英] Simulate slow typing in Protractor

查看:63
本文介绍了在Protractor中模拟慢速键入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sendKeys()方法一次发送所有密钥(实际上,一次一个但很快):

sendKeys() method would send all the keys at once (actually, one at a time but very quickly):

var elm = element(by.id("myinput"));
elm.sendKeys("test");

有没有办法减慢打字速度,以便量角器会发送一个字符时间每个字符之间有一点延迟?

Is there a way to slow the typing down so that Protractor would send one character at a time with a small delay between each of the characters?

我们可以完全放慢Protractor ,但这不会改变 sendKeys()的工作方式我们只需要发送密钥部分而且只在特定情况下才会减慢一切。

We can slow down Protractor entirely, but that does not change the way sendKeys() works and it would also slow everything down while we just need the "send keys" part and only in specific cases.

推荐答案

这个想法是使用 browser.actions() 并构造一系列发送密钥命令 - 一个用于字符串中的每个字符。在每次发送密钥命令后,我们通过引入自定义来添加延迟睡觉行动。最后,这是我们提出的可重用函数:

The idea is to use browser.actions() and construct a series of "send keys" command - one for every character in a string. After every "send keys" command we are adding a delay by introducing a custom sleep action. At the end, here is a reusable function we've come up with:

function slowType(elm, keys, delay) {
    var action = browser.actions().mouseMove(elm).click();

    for (var i = 0; i < keys.length; i++) {
        action = action.sendKeys(keys[i]).sleep(delay);
    }

    return action.perform();
}

用法:

slowType(elm, "some text", 100);

这篇关于在Protractor中模拟慢速键入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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