How To Press Keyboard Keys in Selenium

Yosuva ArulanthuAppium, Java, Selenium, Test Automation, TestingLeave a Comment

On occasion, you’ll come across functionality that requires the use of keyboard key presses in your tests.

Perhaps you’ll need to tab to traverse from one portion of the page to another, back out of some kind of menu or overlay with the escape key, or even submit a form with Enter.

But how do you do it and where do you start?

You can easily issue a key press by using the send_keys command.

This can be done to a specific element, or generically with Selenium’s Action Builder (which has been documented on the Selenium project’s Wiki page for Advanced User Interactions). Either approach will send a key press. The latter will send it to the element that’s currently in focus in the browser (so you don’t have to specify a locator along with it), whereas the prior approach will send the key press directly to the element found.

You can see a full list of keyboard key symbols here.

Let’s step through the example.

SAMPLE CODE

Java

// filename: KeyboardKeys.java

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Keys;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class KeyboardKeys {
    WebDriver driver;

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
    @Test
    public void uploadFile() throws Exception {
        driver.get("http://the-internet.herokuapp.com/key_presses");
        driver.findElement(By.id("content")).sendKeys(Keys.SPACE);
        assertThat(driver.findElement(By.id("result")).getText(), is("You entered: SPACE"));
        Actions builder = new Actions(driver);
        builder.sendKeys(Keys.LEFT).build().perform();
        assertThat(driver.findElement(By.id("result")).getText(), is("You entered: LEFT"));
    }

}

Python

# filename: keyboard_keys.py
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

class KeyboardKeys(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def tearDown(self):
        self.driver.quit()
    def test_example_1(self):
        driver = self.driver
        driver.get('http://the-internet.herokuapp.com/key_presses')
        driver.find_element_by_class_name('example').send_keys(Keys.SPACE)
        assert driver.find_element_by_id('result').text == 'You entered: SPACE'
        ActionChains(driver).send_keys(Keys.TAB).perform()
        assert driver.find_element_by_id('result').text == 'You entered: TAB'
if __name__ == "__main__":
    unittest.main()

Ruby

# filename: key_presses.rb

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
  @driver = Selenium::WebDriver.for :firefox
end

def teardown
  @driver.quit
end

def run
  setup
  yield
  teardown
end

run do
  @driver.get 'http://the-internet.herokuapp.com/key_presses'
  @driver.find_element(class: 'example').send_keys :space
  expect(@driver.find_element(id: 'result').text).to eql('You entered: SPACE')
  @driver.action.send_keys(:tab).perform
  expect(@driver.find_element(id: 'result').text).to eql('You entered: TAB')
end

C#

// filename: KeyboardKeys.cs
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;

public class KeyboardKeys
{
    IWebDriver Driver;

    [SetUp]
    public void SetUp()
    {
        Driver = new FirefoxDriver();
    }

    [TearDown]
    public void TearDown()
    {
        Driver.Quit();
    }
    [Test]
    public void KeyboardKeysExample()
    {
        Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/key_presses");

        Driver.FindElement(By.Id("content")).SendKeys(Keys.Space);
        Assert.That(Driver.FindElement(By.Id("result")).Text.Equals("You entered: SPACE"));
        Actions Builder = new Actions(Driver);
        Builder.SendKeys(Keys.Left).Build().Perform();
        Assert.That(Driver.FindElement(By.Id("result")).Text.Equals("You entered: LEFT"));
    }
}

When you save this file and run it, here is what will happen:

  • Open the browser
  • Visit the page
  • Find the element and send the space key to it
  • Find the result text on the page and check that it’s what we expect
  • Send the left arrow key to the element that’s currently in focus
  • Find the result text on the page and check that it’s what we expect
  • Close the browser

If you have a specific element that you want to issue key presses to, then finding the element first is the way to go. But if you don’t have a receiving element, or you need to string together multiple key presses, then the action builder is what you should reach for.

Leave a Reply

Your email address will not be published.