In this tutorial, I will show you how sendkeys in Selenium Webdriver to emulate keyboard key press actions.In our previous articles, we have already seen how to set desired value in a textbox using the “sendKeys()” method of WebDriver’s WebElement class.
This article especially focuses on emulating non-text keyboard events using Actions class because it is more reliable than WebDriver’s WebElement class for emulating keyboard events.
SendKeys in Selenium Webdriver
Why Emulating Keyboard Events is Required in Selenium WebDriver?
The following are a few of the examples wherein we need to emulate keyboard press (non-text keys) actions like a real user.
- Copy and Paste: Copying all existing text from any TextBox/ TextArea to another TextBox/ TextArea by pressing “CTRL+A” and “CTRL+C” followed by “CTRL+V“.
- Typing Camel Case letters: If you need to type either capital letters or camel case letters like (GoodDay).In such cases, you can press the “SHIFT” key and then type the required characters in the input text field, the letters will be typed in capital letters until the “SHIFT” key is pressed.
- Pressing Tab: Pressing the “Tab” key.
These are a few examples however, there could be many more as per the test scenario of the application.
Example of Doing Copy and Paste Text from one TextBox to another TextBox
Test Scenario
- Launch the Chrome browser
- Open the page https://www.w3schools.com/html/html_forms.asp
- Enter the First Name in the “First name:” text box
- Select the value of the first name by pressing “CTRL+A“
- Copy the value of the first name by pressing “CTRL+C“
- Press the “Tab” key to set the focus on the Last Name
- Select the existing value of the last name by pressing “CTRL+A“
- Paste the first name value into the Last Name text box by pressing “CTRL+V“
- Build and perform the above series of actions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class EmulatingKeyBoardEvents { public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; System.setProperty("webdriver.chrome.driver",BrowserDriverPath); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("https://www.w3schools.com/html/html_forms.asp"); driver.manage().window().maximize(); // Create object of the Actions class Actions actions = new Actions(driver); // Clear existing value in the first name textbox driver.findElement(By.id("fname")).clear(); //Setting a new name driver.findElement(By.id("Richard")).clear(); // Select the value of First Name using CTRL + A actions.keyDown(Keys.CONTROL); actions.sendKeys("a"); actions.keyUp(Keys.CONTROL); actions.build().perform(); // Copy the value of First Name using CTRL + C actions.keyDown(Keys.CONTROL); actions.sendKeys("c"); actions.keyUp(Keys.CONTROL); actions.build().perform(); // Press the TAB Key to Switch Focus to Last Name actions.sendKeys(Keys.TAB); actions.build().perform(); // Select the existing value of Last Name using CTRL + A actions.keyDown(Keys.CONTROL); actions.sendKeys("a"); actions.keyUp(Keys.CONTROL); actions.build().perform(); // Paste the value of First Name using CTRL + V actions.keyDown(Keys.CONTROL); actions.sendKeys("v"); actions.keyUp(Keys.CONTROL); actions.build().perform(); // Uncomment following line to Close the browser // driver.quit(); } } |
Example of Typing Camel Case Text in a TextBox
Test Scenario
- Launch the Chrome browser
- Open the page https://www.w3schools.com/html/html_forms.asp
- Enter the came case text value “ABcdEF” in the “First name:” text box
- Move the mouse over “First name:” and click on it using Actions class to set focus on it
- Type text “ab” by pressing the “SHIFT” key.
- Release the “SHIFT” key and type “cd”
- Again type text “ef” by pressing the “SHIFT” key
- Release the “SHIFT” key and build and perform the series of actions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class TypingCamelCaseLetters { public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; System.setProperty("webdriver.chrome.driver",BrowserDriverPath); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("https://www.w3schools.com/html/html_forms.asp"); driver.manage().window().maximize(); // Create object of the Actions class Actions actions = new Actions(driver); // Clear existing value in the first name textbox driver.findElement(By.id("fname")).clear(); actions.moveToElement( driver.findElement(By.id("fname"))).click(); //Typing text in camel case ( ABcdEF ) in the First name textbox actions.keyDown(Keys.SHIFT).sendKeys("ab").keyUp(Keys.SHIFT).sendKeys("cd").keyDown(Keys.SHIFT).sendKeys("ef").keyUp(Keys.SHIFT).build().perform(); //Uncomment following line to Close the browser //driver.quit(); } } |
Conclusion
We learned how to use Actions Class to Send Keyboard Keys in Selenium Webdriver. You can have a look at the above examples and try to understand them. It will help you out to perform any keyboard event action in Selenium.If you find this article helpful please don’t forget to share it.
Note: After performing any keyDown(theKey) method you must call the keyUp(theKey) or sendKeys(Keys.NULL) to release the pressed the key otherwise it would remain in the pressed state.
Recommended Posts
- Handle Dynamic Web Tables In Selenium WebDriver
- Top #23 Most Useful Selenium WebDriver Commands That You Must know
- XPath in Selenium WebDriver with Example
- How to Use CSS Selector in Selenium WebDriver | 13 Examples
- How to Handle a New Tab in Selenium WebDriver and Switch Between Tabs
- How to Use AutoIT In Selenium WebDriver
- Get ToolTip Text in Selenium WebDriver
- 3 Ways Of Drag and Drop Action in Selenium With Examples