Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

javascript - Simulate slow typing in Protractor

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
}

Usage:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...