Simulating Keyboard Operations
When automating text boxes or similar controls that receive keyboard input, it is necessary to simulate keyboard input. The pressKeys()
method is an operation API supported by all control types in CukeTest, and is often used to input strings in the input box. This method is provided in each technology type, which is divided into the following according to the technology type:
It has the following signature:
interface PressKeysOptions {
textOnly?: boolean,
cpm?: number
}
pressKeys(keys: string, options?: PressKeysOptions | number): Promise<void>;
class PressKeysOptions():
textOnly: bool
cpm: int
Enter a key or string, and focus on the target control before inputting. When a string is passed in, some special characters (^+~%{}()
) in the string will be executed as control keys (Shift
key, CTRL
key, etc.) For the corresponding symbols, please refer to Appendix: Input Key Correspondence Table for details. If you want to enter plain text, ignoring these control key symbols, you can use the {textOnly: true}
option and call it like this: pressKeys(str, {textOnly: true})
.
- keys:
string
, The key, key combination or string to be input, with a maximum of 1024 characters. - options: (Optional) Some optional parameters to control the input mode.
- textOnly: Only enter strings, and control characters are also entered as text. The effect is equivalent to calling
Keyboard.typeString()
. - cpm: The number of input characters per minute is used to control the text input speed. It is recommended to set the
cpm
value above 200 during automatic operation. Due to the internal implementation of the method, and the processing of text input by each system and application is different, the actual input speed is not necessarily the same Can reach the setcpm
. When options is a number, it is equivalent to the cpm parameter.
- textOnly: Only enter strings, and control characters are also entered as text. The effect is equivalent to calling
- returns: An asynchronous method that returns no value.
This method implements string input by simulating keyboard signals, so it can also be used for complex keyboard operations. For example, use the following code to quickly complete login:
await input.pressKeys("username{TAB}password~");
input.pressKeys("username{TAB}password~")
Among them, {TAB}
means to press the Tab key, ~
means to press the Enter key. On the premise that the Tab key is supported to switch the input box and the Enter key to log in, this statement can successfully complete the account password input and Login, and these commands for simulating keys are called key codes (keycodes). For more related content, please refer to Appendix: Correspondence Table of Input Keys.
Example 1: Enter special characters
Suppose now you need to input the text This method implements string input by simulating keyboard signals, so it can also be used for complex keyboard operations. For example, use the following code to quickly complete login:
The answer is to use curly braces to eliminate ambiguity, as follows :
let textarea = model.getDocument("text editor"); // The following examples are the same as
//Requirement: Enter the special symbol "+^%~()[]{}"
await textarea.pressKeys("{+}");
await textarea.pressKeys("{^}");
await textarea.pressKeys("{%}");
await textarea.pressKeys("{~}");
await textarea.pressKeys("{(}");
await textarea.pressKeys("{)}");
await textarea.pressKeys("{[}");
await textarea.pressKeys("{]}");
await textarea.pressKeys("{{}");
await textarea.pressKeys("{}}");
await textarea.pressKeys("\""); // Quotes still need to be escaped
textarea = model.getDocument("文本编辑器"); # The following examples are the same as
#Requirement: Enter the special symbol "+^%~()[]{}"
textarea.pressKeys("{+}");
textarea.pressKeys("{^}");
textarea.pressKeys("{%}");
textarea.pressKeys("{~}");
textarea.pressKeys("{(}");
textarea.pressKeys("{)}");
textarea.pressKeys("{[}");
textarea.pressKeys("{]}");
textarea.pressKeys("{{}");
textarea.pressKeys("{}}");
textarea.pressKeys("\""); # Quotes still need to be escaped
Example 2: Insert newlines and tabs
Text is ready to enter, but now it needs to wrap? Or does the entered text need to insert tabs for typesetting? Of course, use the Enter key and the Tab key, as follows:
//Requirements: Enter the Enter key to change the line, enter Tab to insert a tab character
await textarea.pressKeys("{ENTER}");
await textarea.pressKeys("~"); // Another keycode for the Enter key
await textarea.pressKeys("{TAB}");
#Requirements: Enter the Enter key to change the line, enter Tab to insert a tab character
textarea.pressKeys("{ENTER}")
textarea.pressKeys("~") # Another keycode for the Enter key
textarea.pressKeys("{TAB}")
Example 3: Operation using combination keys
There may be a situation where you want to use the shortcut key combination to close the current window instead of clicking the cross in the upper right corner. This shortcut key may be ctrl+w
or alt+F4
, depending on which window supports . as follows:
//Requirement: Use the Shift key plus the arrow keys to select 5 characters
await textarea.pressKeys("+{LEFT 5}");
//Requirement: Use Ctrl key plus x to cut selected characters
await textarea.pressKeys("^x");
//Requirement: Use the Ctrl key plus v to paste characters
await textarea.pressKeys("^v");
//Requirement: Use the Ctrl key plus w to close the window
await textarea.pressKeys("^w");
//Requirement: Use the Alt key plus F4 to close the window
await textarea.pressKeys("%{F4}");
textarea.pressKeys("+{LEFT 5}")
textarea.pressKeys("^x")
textarea.pressKeys("^v")
textarea.pressKeys("^w")
textarea.pressKeys("%{F4}")
Maybe you will notice that when multiple commands are passed in, how does the pressKeys()
method distinguish whether these commands are executed separately or combined? In fact, only the three control keys Shift
, Ctrl
, and Alt
will be executed as a combination key. When an instruction for these three keys appears, it will automatically wait for the next instruction to be executed (If the next command is still a control key, it will be postponed to the next command).
Therefore, if you want to achieve an effect similar to pressing the Shift
key to enter, you only need to wrap the instruction after the Shift
instruction in parentheses. You can use the following two lines of code to understand:
await textarea.pressKeys("+abc"); // The input result is Abc
await textarea.pressKeys("+(abc)"); // The input result is Abc
textarea.pressKeys("+abc")
textarea.pressKeys("+(abc)")
Summarize
The above is an introduction example of using the input key command in the actual scene. It should be noted that the pressKeys()
method is an instance method of Test Object, which needs to specify the object of the keyboard input. For example, in a To press a combination of keys in a window and enter a string in an input box, an object must be specified to use the pressKeys()
method.