Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

  • Selenium Video Tutorials

Selenium WebDriver - Keyboard Events



Selenium Webdriver can be used to perform keyboard events operations like key up, and down, enter multiple characters in the middle other operations, and copy and paste operations using the Actions class. The methods keyUp(), keyDown(), and sendKeys() are used to perform these operations.

Basic Methods of Keyboard Events in Actions Class

There are multiple methods to perform Keyboard events available in the Actions class. To get more information on these methods, please refer to the below link −

  • keyDown(CharSequence key) − This method is used to perform a modifier key press, passed as a parameter.
  • keyDown(WebElement e, CharSequence key) − This method is used to perform a modifier key press post focusing an element. The webElement e, and key to be pressed are passed as parameters.
  • keyUp(CharSequence key) − This method is used to perform a modifier key release, passed as a parameter.
  • keyUp(WebElement e, CharSequence key) − This method is used to perform a modifier key release post focusing an element. The webElement e, and key to be released are passed as parameters.
  • sendKeys(CharSequence key) − This method is used to send keys to elements in focus. The key to be sent is passed as a parameter.
  • sendKeys(WebElement e, CharSequence key) − This method is used to send keys to the webElement passed as parameter.
  • build() − This method is used to create a combination of actions having all the actions to be carried on.
  • perform() − This method is used to perform actions without invoking the build() first.

Please note that, while using the methods of Actions Class, we would need to add the import statement −

import org.openqa.selenium.interactions.Actions in our tests.

Example 1 - Copy Paste Task

Let us take an example on the below page, where we would first enter the text - Selenium beside the Full Name: in the first input box(highlighted) then copy and paste the same text in another input box(highlighted) beside the Last Name: .

Selenium Keyboard Events 2

Syntax

WebDriver driver = new ChromeDriver();

// Identify the first input box with xpath locator
WebElement e = driver.findElement(By.xpath("<value of xpath>"));

// enter some text
e.sendKeys("Selenium");

// chose the key as per platform
Keys k = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;

// object of Actions class to copy then paste
Actions a = new Actions(driver);
a.keyDown(k);
a.sendKeys("a");
a.keyUp(k);
a.build().perform();

// Actions class methods to copy text
a.keyDown(k);
a.sendKeys("c");
a.keyUp(k);
a.build().perform();

// Action class methods to tab and reach to next input box
a.sendKeys(Keys.TAB);
a.build().perform();

// Actions class methods to paste text
a.keyDown(k);
a.sendKeys("v");
a.keyUp(k);
a.build().perform();

// Identify the second input box with xpath locator
WebElement s = driver.findElement(By.xpath("<value of xpath>"));

// Getting text in the second input box
String text = s.getAttribute("value");
System.out.println("Value copied and pasted: " + text);

Code Implementation

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class CopyPasteAction {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");

      // Identify the first input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='firstname']"));

      // enter some text
      e.sendKeys("Selenium");

      // chose the key as per platform
      Keys k = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;

      // object of Actions class to copy then paste
      Actions a = new Actions(driver);
      a.keyDown(k);
      a.sendKeys("a");
      a.keyUp(k);
      a.build().perform();

      // Actions class methods to copy text
      a.keyDown(k);
      a.sendKeys("c");
      a.keyUp(k);
      a.build().perform();

      // Action class methods to tab and reach to next input box
      a.sendKeys(Keys.TAB);
      a.build().perform();

      // Actions class methods to paste text
      a.keyDown(k);
      a.sendKeys("v");
      a.keyUp(k);
      a.build().perform();

      // Identify the second input box with xpath locator
      WebElement s = driver.findElement(By.xpath("//*[@id='lastname']"));

      // Getting text in the second input box
      String text = s.getAttribute("value");
      System.out.println("Value copied and pasted: " + text);

      // Closing browser
      driver.quit();
   }
}

Output

Value copied and pasted: Selenium

Process finished with exit code 0

In the above example, we had first entered the text Selenium in the first input box and then copied and pasted the same text in the second input box.

Finally, we had obtained the entered text in the second input box as a message in the console - Value copied and pasted: Selenium.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 2 - Enter Text in Focus Input Box

Let us take another example, where we would enter text Selenium within the input box beside Full Name: which in focus.

Selenium Keyboard Events 3

Syntax

WebDriver driver = new ChromeDriver();

// Identify the input box with xpath locator
WebElement e = driver.findElement(By.xpath("<value of xpath>"));

// object of Actions class to input text in focus
Actions a = new Actions(driver);
a.click(e).sendKeys("Selenium").build().perform();

Code Implementation

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class EnterTextInFocus {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php");

      // Identify the first input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

      // object of Actions class to input text in focus
      Actions a = new Actions(driver);
      a.click(e).sendKeys("Selenium").build().perform();

      // Getting text in the second input box
      String text = e.getAttribute("value");
      System.out.println("Value entered to input box in focus: " + text);

      // Closing browser
      driver.quit();
   }
}

Output

Value entered to input box in focus: Selenium

Process finished with exit code 0

In the above example, we had first entered the text Selenium in the input box in focus and then obtained the text as a message in the console - Value entered to input box in focus: Selenium.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 3 - Enter Text in Input Box

Let us take another example, where we would enter text Selenium within a designated input box beside Name:.

Selenium Keyboard Events 4

Syntax

WebDriver driver = new ChromeDriver();

// Identify the input box with xpath locator
WebElement e = driver.findElement(By.xpath("<value of xpath>"));

// object of Actions class to input text in focus
Actions a = new Actions(driver);
a.sendKeys(e, "Selenium").build().perform();

Code Implementation

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class EnterText {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Identify input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));

      // object of Actions class to input text
      Actions a = new Actions(driver);
      a.sendKeys(e,"Selenium").build().perform();

      // Getting text in the second input box
      String text = e.getAttribute("value");
      System.out.println("Value entered to input box: " + text);

      // Closing browser
      driver.quit();
   }
}

Output

Value entered to input box: Selenium

Process finished with exit code 0

In the above example, we had entered the text Selenium in an input box and then obtained the text as a message in the console - Value entered to input box: Selenium.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Conclusion

This concludes our comprehensive take on the tutorial on Selenium Webdriver Keyboard Events. Weve started with describing basic methods of keyboard events in Actions class,, and examples to illustrate how to handle keyboard events in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver Keyboard Events. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements