Advanced User Interactions | Automating Keyboard press events
Learn how to automate different keyboard press events in Selenium including key combinations like Ctrl+C, Ctrl+V, TAB, and more.
In the Previous Tutorial, we learned to automate drag-drop operations. In this tutorial, we'll learn ways to automate different keyboard press events.
builder.keyDown(Keys.SHIFT)
.sendKeys(driver.findElement(By.id("firstname")),"testuser")
.keyUp(Keys.SHIFT)
.build()
.perform();
Let's break this down step by step:
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://cosmocode.io/automation-practice");
//Instantiate Action
Actions builder = new Actions(driver);
Keep pressing 'SHIFT' key while typing into the input box. Once we are done with typing we'll release 'SHIFT'. First of all, we will get that element in which we need to enter text.
-
You need to import the following package for Keys –
org.openqa.selenium.Keys -
Next, write the code to find element and press keys:
WebElement eleFirstName = driver.findElement(By.id("firstname"));
builder.keyDown(Keys.SHIFT);
builder.sendKeys(eleFirstName,"testuser");
builder.keyUp(Keys.SHIFT);
builder.build().perform();
Why didn't you call build().perform() after each step?
Well, we need to call build().perform() after calling chain of events that you need to execute. In this scenario we needed to execute the following chain of events – keep pressing 'SHIFT' key while typing and then releasing 'SHIFT' key.
In fact, we can also club all five steps into one like this:
builder.keyDown(Keys.SHIFT)
.sendKeys(driver.findElement(By.id("firstname")),"testuser")
.keyUp(Keys.SHIFT)
.build()
.perform();
- Press Ctrl-a to select all text in the input box and then press Ctrl-c to copy it to the clipboard.
builder.sendKeys(Keys.chord(Keys.CONTROL,"a"),Keys.chord(Keys.CONTROL,"c"));
builder.build().perform();
What the heck is Keys.chord() and why you are passing it to sendKeys?
If you are familiar with chords in guitar, you can easily relate that to it. Using Keys.Chrod() we can simulate pressing multiple keys simultaneously. First, we are pressing 'Control' and 'a' keys simultaneously and then 'Control' and 'c'. We are passing both chords to sendKeys() to send desired keystrokes.
- Write code to press 'TAB' to shift focus to the next input box.
builder.sendKeys(Keys.TAB);
builder.build().perform();
- Press Ctrl-v
builder.sendKeys(Keys.chord(Keys.CONTROL,"v"));
builder.build().perform();
I guess I can combine both lines of codes?
Yes, you can:
builder.sendKeys(Keys.chord(Keys.CONTROL,"v")).build().perform();
Full Example
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\teachmeselenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://cosmocode.io/automation-practice");
Actions builder = new Actions(driver);
WebElement eleFirstName = driver.findElement(By.id("firstname"));
builder.keyDown(Keys.SHIFT);
builder.sendKeys(eleFirstName,"testuser");
builder.keyUp(Keys.SHIFT);
builder.build().perform();
//builder.keyDown(Keys.SHIFT).sendKeys(driver.findElement(By.id("firstname")),"testuser").keyUp(Keys.SHIFT).build().perform();
builder.sendKeys(Keys.chord(Keys.CONTROL,"a"),Keys.chord(Keys.CONTROL,"c"));
builder.build().perform();
builder.sendKeys(Keys.TAB);
builder.build().perform();
builder.sendKeys(Keys.chord(Keys.CONTROL,"v"));
builder.build().perform();
}
}
In the Next Tutorial, we will learn something very exciting. How about distributing your tests to run on multiple machines, including cloud?