Selenium has a mechanism to manage keyboard events that might simulate user motions. Consider searching “India” on the Google search engine. In order to see the search results for this, type “India” and press the Enter key. How would you simulate pressing the ENTER key in Selenium code, let’s say you wanted to automate this Google search? The ability to imitate such keyboard actions is made possible by keyboard events produced by Selenium WebDriver’s Actions class. There are selenium interview questions that ask questions related to keyboard events in the selenium Actions class, so you can check them out.
What do Selenium keyboard events entail?
A user’s interaction with the keyboard is described by a Keyboard Event. Keyboard events happen whenever a user pushes a key or a series of keys. There are several approaches to automate these Keyboard Events using Selenium.
- Use the sendKeys() function of the WebElement class to automate keyboard events.
- Use the Robot class to automate keyboard actions.
- Utilise the Actions class to automate keyboard events.
The first two methods of handling keyboard events using the “sendKeys()” function of the WebElement class and the “Robot class” of WebDriver were already covered in the articles provided by the corresponding links. We will discuss the specifics of Selenium WebDriver’s “Actions” class in this article. Let’s first examine why particularly the “Actions” class is required to handle those keyboard events before delving deeply into its fundamentals.
Why is the Actions class needed to perform Keyboard actions using Selenium WebDriver?
There are different circumstances when the user does the following actions when interacting with a web application:
- Wherever the user needs to type a word or letter in capital letters, they simply press the “SHIFT” key and enter the required characters. Any characters entered while holding down the “SHIFT” key will type as a capital letter.
- Copy and Paste Text: Text can be copied and pasted by selecting it with the “CTRL+A” keyboard shortcut, copying it with the “CTRL+C” key, and pasting it into a new text box by simply clicking in the existing text box and using the “CTRL+V” keyboard shortcut.
These are pretty typical user behaviours that we engage in virtually daily.
Now, Selenium WebDriver offers two options, as we previously covered, to transmit any keyboard event to a web element:
sendKeys() method of WebElement class.
Actions class
Let’s try to explore in more depth how the sendKeys() method of the WebElement class may be used to automate the above-mentioned scenario of inputting capital letters while holding down the SHIFT key.
Although WebElement’s sendKeys() method permits the use of the Control Key, copying and pasting is not possible since the key sequences are not combined. Moreover, it placed the letter “V” in the Permanent Address text field rather than the contents of the Current Address.
When the text of these two fields is compared, the assertion fails because the text of the Current Address text field was not copied to the Permanent Address text field. So, the test above fails with an error message when we run it.
Therefore, this is the problem with the WebElement class’ sendKeys() method. To put it another way, it doesn’t work when we need to combine special keys like “SHIFT,” “CONTROL,” etc. Using any web program, as users, is frequently the case with the various key sequences, as we all know.
The Selenium WebDriver Actions class, which offers a variety of methods to particularly handle operations of these meta keyboard keys that must be pressed while carrying out operations on the other keyboard keys, enters the picture as a result. Let’s look at how the Selenium WebDriver Actions class may be used to handle such keyboard actions.
What is an Actions Class in Selenium WebDriver?
As shown above, the “Actions” class in Selenium WebDriver has a number of methods that can be used to automate and simulate keyboard and mouse actions.
What are the various Keyboard Events Actions class methods available?
The Actions class primarily offers the following three methods for emulating the keyboard events, as shown in the screenshot up top:
- sendKeys()
Sends a string of keystrokes to a specified web element using the sendKeys() function. Two formats of this approach are overloaded:
SendKeys (CharSequence, KeysToSend): This function transmits a series of keys to a web element that is now focused, therefore if we want to send a specific character to a web element, we must first focus the element in question before sending only the specified character to it.
- sendKeys(WebElement element, CharSequence.KeysToSend): This particular implementation of the sendKeys() method transmits a series of characters or keys to the particular web element specified in the method’s first parameter. This technique executes the same action as sendKeys by first concentrating on the target web element (CharSequence keys).
- keyDown()
When a particular keyboard key has to be pressed, this function simulates a keyboard action. So, we can utilise the keyDown() method to keep the key pressed anytime you need to press a key and then carry out specific other tasks. Take the case where a user needs to input some characters in capital letters. So, to mimic user behaviour, the user would press the SHIFT key before pressing the necessary capitalization-required characters. This approach is also accessible in the following two overloaded variants:
- keyDown(CharSequence key): By using this method, the key given is pressed on the focused Web Element. This technique typically involves pressing “Modifier keys” like SHIFT, CTRL, etc. This technique must be used after the target web element has been explicitly focused if you want to press a keyboard key on that web element.
- keyDown(WebElement element, CharSequence key): This method presses the key on the web element that has been supplied as a parameter to the method after first focusing on the web element that has been passed as a parameter.
- KeyUp()
This method is frequently used in conjunction with KeyDown(). The keyboard key that is pressed using the keyDown() method is not automatically released; therefore, it must be released deliberately using the keyUp() method.
- keyUp(CharSequence key): This method releases the designated key on the focused Web Element at the moment. This method must first be used to specifically focus on the web element you want to release the keyboard key on before you can use it to release the key on any other web element.
- keyUp(WebElement element, CharSequence key): This function first concentrates on the web element, which is supplied as a parameter to the method. The stated key is then released on that Web Element.
ConclusionFinally, we can say that we understand every keyboard-specific method that the Actions class offers. Let’s now examine how we may automate the user in the situation described above by utilising the Selenium WebDriver Actions class’s methods. Other concepts like how to scroll down in Selenium are others you can learn in Selenium.