Mouse Click & Keyboard Event : Action Class

There are a lot of methods in this class which can be categorized into two main categories:

  • Keyboard Events
  • Mouse Events
 Different Methods for performing Keyboard Events:
  • keyDown(modifier key): Performs a modifier key press. 
  • sendKeys(keys to send ): Sends keys to the active web element.
  • keyUp(modifier key): Performs a modifier key release.

  • Actions actions = new Actions(webdriver object);
    WebElement element = driver.findElement(By strategy to identify element);
    actions.keyDown(element, Keys.SHIFT);
    actions.sendKeys(“TextToBeConvertAndSendInUpperCase”);
    actions.keyUp(Keys.SHIFT);
    action.perform();
        actions.build();


Different Methods for performing Mouse Events:

Right Click

 //Instantiate Action Class
  Actions actions = new Actions(driver);
 //Retrieve WebElement to perform right click
  WebElement btnElement = driver.findElement(By.id("rightClickBtn"));
  //Right Click the button to display Context Menu 
    actions.contextClick(btnElement).perform();

DoubleClick :

Actions builder = new Actions(driver);
 WebElement from = driver.findElement(By.id("draggable"));
 WebElement to = driver.findElement(By.id("droppable")); 
 //Perform drag and drop
 builder.dragAndDrop(from, to).perform();

Drag and Drop:


 Actions actions = new Actions(driver); 
  //Retrieve WebElement to perform double click WebElement
 btnElement = driver.findElement(By.id("doubleClickBtn")); 
 //Double Click the button 
 actions.doubleClick(btnElement).perform(); 


  • click(): Clicks at the current mouse location.
  • doubleClick(): Performs a double-click at the current mouse location.
  • contextClick() : Performs a context-click at middle of the given element.
  • clickAndHold(): Clicks (without releasing) in the middle of the given element.
  • dragAndDrop(source, target): Click-and-hold at the location of the source element, moves to the location of the target element
  • dragAndDropBy(source, xOffset, yOffset):  Click-and-hold at the location of the source element, moves by a given offset
  • moveByOffset(x-offset, y-offset): Moves the mouse from its current position (or 0,0) by the given offset
  • moveToElement(toElement): Moves the mouse to the middle of the element
  • release(): Releases the depressed left mouse button at the current mouse location

Comments

Popular posts from this blog

Status code

Browsers Setting