Action class to work with keyboard in Selenium


Actions class is a built-in ability to handle various types of keyboard and mouse events.
We will use perform() to execute the actions.

Keyboard interface


sendKeys(keysToSend): Sends a series of keystrokes onto the element.
Actions.click(element).sendKeys(keysToSend).build().perform()
keyDown(theKey): Sends a key press without release it. Subsequent actions may assume it as pressed.
keyDown(CharSequence key): Performs a modifier key press.
keyDown(WebElement target, CharSequence key): Performs a modifier key press after focusing on an element.

keyUp(theKey): Performs a key release.
keyUp(CharSequence key): Performs a modifier key release.
keyUp(WebElement target, CharSequence key): Performs a modifier





public class ActionClass {
WebDriver driver;
 @BeforeClass
 public void launchBroser()
  {
  System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
  driver=new ChromeDriver();
  driver.get("https://google.com");
  }
 @Test
 public void ActionMethod()
 {
  Actions actionObj= new Actions(driver); 
  
  WebElement inputbox=driver.findElement(By.xpath("//*[@class='gLFyf gsfi']"));
  
  /*Action elem=actionObj.click(inputbox).sendKeys("Yogndrasingh").build();
  elem.perform();*/

 actionObj.click(inputbox).keyDown(Keys.SHIFT).sendKeys("Yogendrasingh").keyUp(Keys.SHIFT).build().perform();
 actionObj.click(inputbox).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build().perform();
 
 }
}

 
 


Mouse interface

click(): Clicks on the element.
actionObj.click(inputbox).build().perform();

doubleClick (): Double clicks on the element.
actionObj.doubleClick(inputbox).build().perform();

contextClick() : Performs a context-click (right-click) on the element.
actionObj.contextClick(inputbox).build().perform();

clickAndHold(): Clicks at the present mouse location without releasing.

actionObj.clickAndHold(gmail).build().perform();

dragAndDrop(source, target): Clicks at the source location and moves to the location of the target element before releasing the mouse. source (element to grab, target – element to release).
actionObj.dragAndDrop(From_Element ,  To_Element).build().perform();

dragAndDropBy(source, xOffset, yOffset): Performs click-and-hold at the source location, shifts by a given offset value, then frees the mouse. (X offset – to shift horizontally, Y Offset – to shift vertically).
actionObj.dragAndDropBy(From,105, 30).build().perform();

moveByOffset(x-offset, y-offset): Shifts the mouse from its current position (or 0,0) by the given offset. x-offset – Sets the horizontal offset (negative value – shifting the mouse to the left), y-offset – Sets the vertical offset (negative value – shifting the mouse to the up).
actionObj.moveByOffset(150, 30).click().build().perform();

moveToElement(toElement): It shifts the mouse to the center of the element.
actionObj.moveToElement(Element).click().build().perform();

release(): Releases the pressed left mouse button at the existing mouse location.

actionObj.moveToElement(Element).release().build().perform();





No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...