Saturday 3 March 2018

How to do Series of Multiple Mouse and Keyboard actions in Selenium

Lets take an example for understand how to do series of multiple actions with Action class.

First Scenario: 

Suppose we need to send any word to text box with upper case with out using ToUpper() function.

First we have to press and hold 'SHIFT' key then enter word which we want to send to text box then release 'SHIFT' key. It is very easy to do with Action class.

  Actions seriesOfAction = new Actions(driver);
  
  // It is google search text box
  WebElement wSearch = driver.findElement(By.id("lst-ib"));
  
  seriesOfAction.keyDown(wSearch, Keys.SHIFT)   // Press and hold 'SHIFT' key
  .sendKeys("selenium") // Send 'selenium' word
  .keyUp(wSearch, Keys.SHIFT) // Release 'SHIFT' key
  .build() // Build all actions
  .perform();  // Perform all actions in sequence


Second Scenario:

Suppose we need to enter any word to text box then copy that word and again send word to that text box.

  Actions seriesOfAction = new Actions(driver);
  
  // It is google search text box
  WebElement wSearch = driver.findElement(By.id("lst-ib"));
  
  seriesOfAction.sendKeys(wSearch, "selenium")
  .doubleClick(wSearch)  // Double click on text. So that it will select whole text
  .sendKeys(Keys.chord(Keys.CONTROL, "c")) // Press Ctrl + C
  .sendKeys(wSearch, " ")  // For release double click and enter space after first word
  .sendKeys(Keys.chord(Keys.CONTROL, "v")) // Press Ctrl + v  
  .build() // Build all actions
  .perform();  // Perform all actions in sequence

Please add comment if you have any question.

No comments:

Post a Comment

Popular