Quick Reference for key-press automation
builder.keyDown(Keys.SHIFT)
.sendKeys(driver.findElement(By.id("firstname")),"testuser")
.keyUp(Keys.SHIFT)
.build()
.perform();
action_chains.key_down(Keys.SHIFT)
.send_keys(driver.find_element_by_id("firstname")),"testuser")
.key_up(Keys.SHIFT)
.perform();
In the Previous Tutorial, we learned to automate drag-drop operations. In this tutorial, we’ll learn ways to automate different keyboard press events.
Automating a use case for keypress
- Go to Automation Practice (Beginner) page
- Press ‘SHIFT’ and then enter values in the input box to simulate typing in upper-case.
- Press Ctrl-a to select all characters in the input box.
- Press Ctrl-c to copy all contents in the input box.
- Press ‘TAB’ to shift focus to the next input box.
- Press Ctrl-v to simulate copy-paste operations.
Initial Code
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);
action_chains.key_down(Keys.SHIFT)
.send_keys(driver.find_element_by_id("firstname")),"testuser")
.key_up(Keys.SHIFT)
.perform();
Press Keys
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();
- Import the package:
from selenium.webdriver.common.keys import Keys
driver.find_element_by_id("firstname").click()
action_chains.key_down(Keys.SHIFT)
action_chains.send_keys(eleFirstName,"testuser")
action_chains.key_up(Keys.SHIFT)
action_chains.perform()
Why didn’t you call perform() after each step?
Well, we need to call perform() after calling chain of actions that you need to execute. In this scenario we needed to execute the following chain of actions – keep pressing ‘SHIFT’ key while typing and then releasing ‘SHIFT’ key.
In fact, we can also club all four steps into one like this:
action_chains.key_down(Keys.SHIFT)
.send_keys(driver.findElement(By.id("firstname")),"testuser")
.key_up(Keys.SHIFT)
.perform()
- Press Ctrl-a to select all text in the input box
action_chains.key_down(Keys.LEFT_CONTROL).send_keys("a").key_up(Keys.LEFT_CONTROL).perform()
- Press Ctrl-c to copy it to the clipboard
action_chains.key_down(Keys.LEFT_CONTROL).send_keys("c").key_up(Keys.LEFT_CONTROL).perform()
- Press ‘TAB’ to shift focus to the next input box
action_chains.send_keys(Keys.TAB).perform()
- Press Ctrl-v
action_chains.key_down(Keys.LEFT_CONTROL).send_keys("v").key_up(Keys.LEFT_CONTROL).perform()
Complete Code
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();
}
}
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver_path = "C:\teachmeselenium\chromedriver.exe"
driver = webdriver.Chrome(driver_path )
driver.get("https://cosmocode.io/automation-practice")
driver.maximize_window()
action_chains = ActionChains(driver)
#Type by pressing SHIFT
driver.find_element_by_id("firstname").click()
action_chains.key_down(Keys.SHIFT)
action_chains.send_keys("testuser")
action_chains.key_up(Keys.SHIFT)
action_chains.perform()
time.sleep(2)
#Ctrl-a
action_chains.key_down(Keys.LEFT_CONTROL).send_keys("a").key_up(Keys.LEFT_CONTROL).perform()
time.sleep(2)
#Ctrl-c
action_chains.key_down(Keys.LEFT_CONTROL).send_keys("c").key_up(Keys.LEFT_CONTROL).perform()
time.sleep(2)
#Press TAB
action_chains.send_keys(Keys.TAB).perform()
time.sleep(2)
#Ctrl-v
action_chains.key_down(Keys.LEFT_CONTROL).send_keys("v").key_up(Keys.LEFT_CONTROL).perform()
driver.quit()
In the Next Tutorial, we will learn something very exciting. How about distributing your tests to run on multiple machines, including cloud?