The actions class

Websites, nowadays, have various features such as double click, drag and drop, and hover, to accomplish certain functionality. A special API called the Actions API is used to handle these scenarios. The actions class implements the builder pattern by creating a composite action made up of constituent actions that are specified by method calls.

Actions is a class in org.openqa.selenium.interactions.Actions and it extends the base class object. This class has one protected field called action, which is of the CompositeAction type. CompositeAction is a class that collects all actions and triggers them at the same time. CompositeAction implements the Action interface. The action interface has just one method: perform(), which the actions class implements.

There are various methods in the actions class. All of these methods return an actions object unless specified explicitly. The generic code for a few of the important methods in this class are shown when we look at the TestBase class, and are as follows:

  • keyDown(Keys): Simulates a key press. The key under consideration remains pressed and the subsequent operations may assume that the key is kept pressed.
  • keyDown(WebElement,Keys): Simulates a key press after focusing on a WebElement. The key under consideration remains pressed and the subsequent operations may assume that the key is kept pressed.
  • keyUp(Keys): Simulates a key release. If you try to use this method on an undepressed key, this will yield an undefined behavior.
  • keyUp(WebElement, Keys): Simulates a key release after focusing on an element.
  • sendKeys(CharSequence...): Sends a variable number of arguments of the CharSequence type. This is a key combination that's sent to the currently active element.
  • sendKeys(WebElement, CharSequence...): Sends a variable number of arguments of the CharSequence type after focusing on the WebElement that's passed as a parameter.
  • clickAndHold(): Simulates a left mouse click without releasing at the current mouse location.
  • clickAndHold(WebElement): Simulates a left mouse click without releasing in the center of a given web element.
  • release(): Releases the depressed left mouse button at the current location. Attempting to invoke this method without first invoking clickAndHold() will result in an undefined behavior.
  • release(WebElement): Releases the depressed left mouse button in the middle of the web element. Attempting to invoke this method without first invoking clickAndHold() will result in an undefined behavior.
  • click(): Left mouse click at the current mouse location.
  • click(WebElement): Left mouse click in the middle of the given web element.
  • doubleClick(): Performs a double click at the current mouse location.
  • doubleClick(WebElement): Performs a double click at the middle of the given web element.
  • moveToElement(WebElement): Moves the mouse cursor to the center of the element. The element is also scrolled into view.
  • moveToElement(WebElement,int,int): Moves the mouse cursor to an offset position from the top-left corner of the element. The element is also scrolled into view.
  • moveByOffset(int,int): Moves the mouse from its current position (or 0,0 if the mouse has not been moved) by the given offset. If the coordinates that are provided are outside the visible window (the mouse will end up outside the browser visible window), then the content is scrolled to match.
  • contextClick(): Performs a right-click at the current mouse location.
  • contextClick(WebElement): Performs a right-click at the center of the given element. First, it does a mouseMove to the location of the element.
  • dragAndDrop(WebElement, WebElement): Executes click-and-hold at the source element, moves to the location of the target element, and then releases the mouse.
  • dragAndDropBy(WebElement,int,int): Executes click-and-hold at the source element, moves the mouse by a given offset, and then releases the mouse.
  • build(): Generates a composite action containing all of the actions that have been gathered so far, ready to be performed (the internal builder state is reset, so subsequent calls to build() will contain fresh sequences).
  • perform(): Performs the accumulated actions without calling build() first. This method has a return type void.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.230.44