1.

What is Action Class?

Answer»

Selenium has a built-in capability to handle various types of keyboard and mouse events. In order to perform action events, we need to use org.openqa.selenium.interactions Actions class,  user-facing API for performing complex user gestures. Instead of using Keyboard or Mouse directly, you can use the selenium actions class.

Syntax to create an object ‘action’ of selenium action class –

  • Actions action – new Actions(driver);
  • To focus on element -
  • action.moveToElement(element).perform();

Here element is the web element we have CAPTURED and perform() method is used here to execute the action.

To click on Element -

  • Action.moveToElement(element).click().perform();
  • Here click() method is used to click the element.
  • Let us see methods available with Selenium Actions class -

Keyboard Events Using Selenium Actions Class API:

  • sendKeys(keysTo Send  sends a series of keystrokes onto the element
  • keyDown(theKey): Sends a keypress without RELEASE it. Subsequent actions may ASSUME it as pressed. (example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
  • keyUp(theKey): Performs a KEY release

Mouse Events Using Selenium Actions Class API:

  • click (): Simply click on an element
  • doubleClick (): Double clicks on Element
  • contextClick() : Performs a context-click (right-click) on an element
  • clickAndHold(): Clicks at the present mouse location (without releasing)
  • dragAndDrop(source, target): Clicks-and-hold at the source location and moves to the target location of the element before releasing the mouse. source – element to grab, target – element to release
  • dragAndDropBy(source, xOffset, YOFFSET): Invokes click-and-hold at the source location, shifts by a given offset, then frees the mouse. xOffset  is to shift horizontally and yOffset is to shift vertically
  • 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)
  • moveToElement(toElement): It shifts the mouse to the center of the element
  • release(): Releases the depressed left mouse button at the existing mouse location


Discussion

No Comment Found