Saturday 3 March 2018

Mouse and Special Keyboard Action with Selenium

Sometime in testing, we need to do some operations with mouse and keyboard. 
Selenium web driver directly can not handle mouse and keyboard event like double click, right click, pressing key combination in keyboard.

We can handle such type of event with Actions class in selenium webdriver. We have to create object of that class and perform action which we want.

Double Click

Syntax : action.doubleClick(WebElement).build().perform();

  Actions action = new Actions(driver);
  
  // It is google search text box
  WebElement wSearch = driver.findElement(By.id("lst-ib"));
  
  // enter 'test' on search text box and double click on that. So test will select.
  wSearch.sendKeys("test");
  action.doubleClick(wSearch).build().perform();
  Thread.sleep(2000);

ContextClick (Right Click)

Syntax : action.contextClick(WebElement).build().perform();

  Actions action = new Actions(driver);
  
  // It is google search text box
  WebElement wSearch = driver.findElement(By.id("lst-ib"));
  
  // right click on google search test box
  action.contextClick(wSearch).build().perform();
  Thread.sleep(2000);

ClickAndHold


Syntax : action.clickAndHold(WebElement);

  Actions action = new Actions(driver);
  
  // It is google search text box
  WebElement wSearch = driver.findElement(By.id("lst-ib"));
  
  // Click and hold on search text box.
  wSearch.sendKeys("test");
  action.clickAndHold(wSearch).build().perform();
  Thread.sleep(2000);

KeyDown and KeyUP

Syntax : action.keyDown(Key_modifier) and action.keyUp(Key_modifier)

Key Modifier: Keys.SHIFT, Keys.CONTROL, Keys.ALT etc.

If we want to press keyboard key on particular web element then we also need to pass that element with function.

action.keyDown(WebElement, Key_modifier)
action.keyUP(WebElement, Key_modifier)


  Actions action = new Actions(driver);
  
  // It is google Search text box
  WebElement wSearch = driver.findElement(By.id("lst-ib"));
  
  // Enter 'Selenium' in capital letters in search text box
  action.keyDown(wSearch, Keys.SHIFT).sendKeys("selenium").keyUp(wSearch, Keys.SHIFT).build().perform();
  Thread.sleep(2000);

We can also do series of multiple action with Actions class. Please visit below blog for how to do series of multiple action with Actions class.


Series of Multiple actions with Selenium


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.

Popular