Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 510
alecxe Avatar asked Jul 04 '16 14:07

alecxe


People also ask

How do I run a protractor test from a script?

The last script is the name of your test and it will be a shorthand to the traditional way to run a protractor test. To run a protractor test you need to type protractor config.js where config.js is the configuration file of your tests (will be discussed in the next section).

How can I simulate a delay in typing in C?

Print the character. Flush the output buffer so it shows up on screen. First, I needed a way to simulate a delay in typing, such as someone typing slowly, or pausing before typing the next word or pressing Enter. The C function to create a delay is usleep (useconds_t usec).

How to disable test cases in it-blocks in protractor?

Only second describe-block gets executed. x can be prefixed to any number of describe-block. Keep in mind that those test cases will never get executed until x prefix is removed. Protractor provides the capability to disable test cases, i.e it-blocks. In order to disable the block just prefix it with x.

Can I use protractor to write in typescript?

You can get more details about this from this magnificent paper. What’s really impressing when using Protractor, that you have the option to use JavaScript directly which is a dynamically typed language or start writing in TypeScript which is a statically typed one. So, you have the luxury of selecting the more suitable option for your project.


1 Answers

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);
like image 60
alecxe Avatar answered Oct 21 '22 18:10

alecxe