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.
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).
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).
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With