tried

Selenium WebDriver – AdvanceUserInteractions API – Move and click on specific coordinates

Moving mouse over specific coordinates and clicking on it.

Actions vActions = new Actions(vDriver);
vActions.moveByOffset(16, 320);
vActions.click();
Action vClickAction = vActions.build();
vClickAction.perform();

  • Working on Desktop Browsers(mostly known) using Selenium WebDriver
  • Working on Mobile Browsers(ChromeDriver, SafariDriver) using Appium 

Selenium WebDriver : getSize() Vs getLocation()

In Selenium WebDriver getSize() and getLocation() methods are different:

getSize() : will return the “Dimension” object, from which we can get Height and Width of specific element
Dimension vDimesions = vDriver.findElement(By.id(“id“)).getSize();
System.out.println(“Height :” + vDimesions.height + ”Width : “+ vDimesions.width);

getLocation() : will return the “Point” object, from which we can get X and Y coordinates of specific element
Point vPoint=driver.findElement(By.id(“id”)).getLocation();

System.out.println(“X : “+ vPoint.x + “Y : “+ vPoint.y);

Selenium WebDriver – Using AdvancedUserInteractions API

The Advanced User Interactions API is more comprehensive API for describing actions a user can perform on a web page such as drag and drop or clicking multiple elements while holding down the Control key.

Actions vMyActions = new Actions(vDriver);
      vMyActions keyDown(Keys.CONTROL)
        .click(someElement)
        .click(someOtherElement)
        .keyUp(Keys.CONTROL);

Then get the action:

   Action vBindAction = vMyActions.build();

And execute it:

   vBindAction.perform();

  • It supports single and composite actions for Mouse and Keyboards
  • It supports most of common browsers including mobile browsers

source

Following are my reusable custom actions, will add more as needed 🙂

 1. MouseMove
Move mouse over an element, usually to get hover

Locatable vElement = (Locatable) vDriver.findElement(by);
Mouse vMouse = ((HasInputDevices) vDriver).getMouse();
vMouse.mouseMove(vElement.getCoordinates());

2. Drag and drop
I need to drag and element on some other element,

vFirtElement = vDriver.findElement(By.id(“id));
vContaingerElement = vDriver.findElement(By.id(“containerid));
new Actions(vDriver). dragAndDrop(vFirtElement, vContaingerElement).build().perform();

3. Drag and Drop By
I need to drag and element from one place to other,

vElementToDrag = vDriver.findElement(By.id(“id”));
new Actions(vDriver).dragAndDropBy(vElementToDrag, 120, 1100).build().perform();

4. Select multiple elements
If I need to check/select multiple list items,

List vList = vDriver.findElements(By .cssSelector(“.class));
Actions vClickMultiple = new Actions(vDriver);
 vClickMultiple.clickAndHold(vList.get(1))
        .clickAndHold(vList.get(2))
        .click();
Action vBindActions = vClickMultiple.build();
 vBindActions.perform();

5. Slide
we can use dragAndDropBy to emulate sliding event

vElementToSlide = vDriver.findElement(By .className(“class”));
new Actions(vDriver).dragAndDropBy(vElementToSlide, 120, 0).build().perform();