Class ElementActions

All Implemented Interfaces:
ElementActionsContract
Direct Known Subclasses:
Actions

public class ElementActions extends FluentWebDriverAction implements ElementActionsContract
Provides high-level actions for interacting with web and mobile elements such as clicking, typing, selecting, dragging, and retrieving element state.

This class extends FluentWebDriverAction and supports method chaining via the and() connector. Prefer using Actions (returned by SHAFT.GUI.WebDriver.element()) for new tests.

Usage example:

driver.element().click(By.id("submit"))
      .and().element().type(By.id("name"), "John")
      .and().assertThat(By.id("result")).text().contains("Success");
See Also:
  • Constructor Details

    • ElementActions

      public ElementActions()
      Creates an element actions instance using a lazily initialized default driver helper.
    • ElementActions

      public ElementActions(org.openqa.selenium.WebDriver driver)
      Creates an element actions instance bound to an existing WebDriver.
      Parameters:
      driver - the active WebDriver session to use
    • ElementActions

      public ElementActions(org.openqa.selenium.WebDriver driver, boolean isSilent)
      Creates an element actions instance bound to an existing WebDriver with optional silent logging.
      Parameters:
      driver - the active WebDriver session to use
      isSilent - true to suppress non-critical action logs; false otherwise
    • ElementActions

      public ElementActions(DriverFactoryHelper helper)
      Creates an element actions instance bound to an existing driver helper.
      Parameters:
      helper - the driver factory helper that manages the underlying WebDriver
  • Method Details

    • and

      public Actions and()
      Description copied from class: FluentWebDriverAction
      Returns this instance to allow fluent (method-chaining) syntax between successive actions. Using .and() improves readability by making multi-step test sequences read like natural language.

      Example:

      driver.element().type(searchBox, "query")
            .and().browser().captureScreenshot();
      
      Specified by:
      and in interface ElementActionsContract
      Overrides:
      and in class FluentWebDriverAction
      Returns:
      this FluentWebDriverAction instance, enabling continued method chaining
    • assertThat

      public WebDriverElementValidationsBuilder assertThat(org.openqa.selenium.By elementLocator)
      Starts a hard-assertion chain on the target element. The test fails immediately when the assertion condition is not met.
      Specified by:
      assertThat in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the element under test (e.g. By.id("result"))
      Returns:
      a WebDriverElementValidationsBuilder for chaining element assertions
      See Also:
    • verifyThat

      public WebDriverElementValidationsBuilder verifyThat(org.openqa.selenium.By elementLocator)
      Starts a soft-assertion (verification) chain on the target element. Failures are collected and reported at the end of the test rather than stopping execution immediately.
      Specified by:
      verifyThat in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the element under test (e.g. By.id("result"))
      Returns:
      a WebDriverElementValidationsBuilder for chaining element verifications
      See Also:
    • getElementsCount

      public int getElementsCount(org.openqa.selenium.By elementLocator)
      Returns the number of elements on the current page that match the given locator.
      Specified by:
      getElementsCount in interface ElementActionsContract
      Parameters:
      elementLocator - the locator used to find elements (e.g. By.cssSelector(".item"))
      Returns:
      the count of matching elements, or 0 if none are found
      See Also:
    • executeNativeMobileCommand

      public Actions executeNativeMobileCommand(String command, Map<String,String> parameters)
      This is a generic method to enable the execution of the native mobile commands found herein: appium.io

      Note: This method does no validation on the output of the executed JavaScript

      Specified by:
      executeNativeMobileCommand in interface ElementActionsContract
      Parameters:
      command - the desired mobile command to be executed. e.g., "mobile: scroll"
      parameters - a map of the key, value parameters for this command. e.g., ImmutableMap.of("direction", "down")
      Returns:
      a self-reference to be used to chain actions
    • click

      public Actions click(org.openqa.selenium.By elementLocator)
      Clicks on a certain element using Selenium WebDriver, or JavaScript
      Specified by:
      click in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • clickUsingJavascript

      public Actions clickUsingJavascript(org.openqa.selenium.By elementLocator)
      Clicks on certain element using javaScript only
      Specified by:
      clickUsingJavascript in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • scrollToElement

      public Actions scrollToElement(org.openqa.selenium.By elementLocator)
      If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.
      Specified by:
      scrollToElement in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • clickAndHold

      public Actions clickAndHold(org.openqa.selenium.By elementLocator)
      Waits for the element to be clickable, and then clicks and holds it.
      Specified by:
      clickAndHold in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • doubleClick

      public Actions doubleClick(org.openqa.selenium.By elementLocator)
      Double-clicks on an element using Selenium WebDriver's Actions Library
      Specified by:
      doubleClick in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • dragAndDrop

      public Actions dragAndDrop(org.openqa.selenium.By sourceElementLocator, org.openqa.selenium.By destinationElementLocator)
      Drags the source element and drops it onto the destination element
      Specified by:
      dragAndDrop in interface ElementActionsContract
      Parameters:
      sourceElementLocator - the locator of the source webElement that should be dragged under test (By xpath, id, selector, name ...etc.)
      destinationElementLocator - the locator of the target webElement that should receive the dropped source element under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • dragAndDropByOffset

      public Actions dragAndDropByOffset(org.openqa.selenium.By sourceElementLocator, int xOffset, int yOffset)
      Drags the source element and drops it onto the determined offset
      Specified by:
      dragAndDropByOffset in interface ElementActionsContract
      Parameters:
      sourceElementLocator - the locator of the source webElement that should be dragged under test (By xpath, id, selector, name ...etc.)
      xOffset - the horizontal offset by which the element should be moved
      yOffset - the vertical offset by which the element should be moved
      Returns:
      a self-reference to be used to chain actions
    • hover

      public Actions hover(org.openqa.selenium.By elementLocator)
      Hovers over target element. If you want to hover on a webElement to expose another webElement and click on it, use hoverAndClick instead for a more reliable result.
      Specified by:
      hover in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • hoverAndClick

      public Actions hoverAndClick(List<org.openqa.selenium.By> hoverElementLocators, org.openqa.selenium.By clickableElementLocator)
      Hovers over the hoverElements in sequence then clicks the clickableElement
      Specified by:
      hoverAndClick in interface ElementActionsContract
      Parameters:
      hoverElementLocators - the list of locators of the webElements under test upon which the hover action will be performed in sequence (By xpath, id, selector, name ...etc.)
      clickableElementLocator - the locator of the webElement under test upon which the click action will be performed (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • select

      public Actions select(org.openqa.selenium.By elementLocator, String valueOrVisibleText)
      Selects an element from a dropdown list using its displayed text or attribute Value
      Specified by:
      select in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      valueOrVisibleText - the text of the choice that you need to select from the target dropDown menu or the string value of attribute "value"
      Returns:
      a self-reference to be used to chain actions
    • setValueUsingJavaScript

      public Actions setValueUsingJavaScript(org.openqa.selenium.By elementLocator, String value)
      Used to SetProperty value for an element (hidden or visible) using javascript
      Specified by:
      setValueUsingJavaScript in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      value - the desired value that should be SetProperty for the target element
      Returns:
      a self-reference to be used to chain actions
    • submitFormUsingJavaScript

      public Actions submitFormUsingJavaScript(org.openqa.selenium.By elementLocator)
      Used to submit a form using javascript
      Specified by:
      submitFormUsingJavaScript in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • switchToIframe

      public Actions switchToIframe(org.openqa.selenium.By elementLocator)
      Switches focus to a certain iFrame, is mainly used in coordination with switchToDefaultContent() to navigate inside any iFrame layer and go back to the main page
      Specified by:
      switchToIframe in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the iFrame webElement under test (By xpath, id, selector, name ...etc.)
      Returns:
      a self-reference to be used to chain actions
    • switchToDefaultContent

      public Actions switchToDefaultContent()
      Switches focus to default content, is mainly used in coordination with switchToIframe(By) to exit any iFrame layer and go back to the main page
      Specified by:
      switchToDefaultContent in interface ElementActionsContract
      Returns:
      a self-reference to be used to chain actions
    • getCurrentFrame

      public String getCurrentFrame()
      gets the current frame
      Specified by:
      getCurrentFrame in interface ElementActionsContract
      Returns:
      currentFrame the current frame name
    • type

      public Actions type(org.openqa.selenium.By elementLocator, CharSequence... text)
      Types the provided text into the target element, replacing any existing value.
      Specified by:
      type in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name, etc.)
      text - one or more CharSequence values (including Keys) to be typed into the target webElement
      Returns:
      a self-reference to be used to chain actions
    • clear

      public Actions clear(org.openqa.selenium.By elementLocator)
      Clears the value of the target element.
      Specified by:
      clear in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name, etc.)
      Returns:
      a self-reference to be used to chain actions
    • typeAppend

      public Actions typeAppend(org.openqa.selenium.By elementLocator, CharSequence... text)
      Appends the required string into the target element, regardless of the current text value.
      Specified by:
      typeAppend in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      text - the target text that needs to be appended into the target webElement
      Returns:
      a self-reference to be used to chain actions
    • typeFileLocationForUpload

      public Actions typeFileLocationForUpload(org.openqa.selenium.By elementLocator, String filePath)
      ValidationEnums the required file path into an input[type='file'] button, to successfully upload the target file.
      Specified by:
      typeFileLocationForUpload in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      filePath - the full path to the file that needs to be uploaded, it can be absolute or relative path, Engine will detect that.
      Returns:
      a self-reference to be used to chain actions.
    • typeSecure

      public Actions typeSecure(org.openqa.selenium.By elementLocator, CharSequence... text)
      Checks if there is any text in an element, clears it, then types the required string into the target element. Obfuscates the written text in the output report. This action should be used for writing passwords and secure text.
      Specified by:
      typeSecure in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the webElement under test (By xpath, id, selector, name ...etc.)
      text - the target text that needs to be typed into the target webElement
      Returns:
      a self-reference to be used to chain actions
    • getTableRowsData

      public List<Map<String,String>> getTableRowsData(org.openqa.selenium.By tableLocator)
      Get any simple table rows' data that has thead which include all the column labels and tbody which includes all table data
      Specified by:
      getTableRowsData in interface ElementActionsContract
      Parameters:
      tableLocator - the locator of the table which should be a table tag
      Returns:
      List of Map format and each Map Object follows the following format (Key:column label, value: cell data)
    • captureScreenshot

      public Actions captureScreenshot(org.openqa.selenium.By elementLocator)
      Captures a screenshot of the specified element and attaches it to the test report.
      Specified by:
      captureScreenshot in interface ElementActionsContract
      Parameters:
      elementLocator - the locator of the element to screenshot
      Returns:
      an Actions instance for chaining further actions
      See Also: