More examples available in interactions-webdriver-demo




Versions


In the box

WebDriver Interactions is based on the concept of 'ready state elements' and distinguishes 3 stages:

WebDriver Interactions compiles a set of functions you can easely integrate in your Selenium WebDriver framework to manage technical stuff automaticaly

Automatic behavior Configurable behavior
  • wait for the presence of an element before accessing its properties
  • wait for a certain state of an element before interacting with it
  • wait for the presence of an element before switching to it
  • scroll the element into view before interacting with it
  • if the locator is Null do nothing
  • if the value is Null do nothing
  • clear field(s) before setting the value
  • wait for a condition that may be realized or not

Usage

1. Add dependency on your pom.xml file
<dependency>
    <groupId>io.github.simple4tests</groupId>
    <artifactId>interactions-webdriver</artifactId>
    <version>...</version>
</dependency>
2. Instanciate a WebDriverInteractions object
WebDriver driver;
...
driver = new ChromeDriver();
...
WebDriverInteractions wdi = new WebDriverInteractions(driver);
3a. Use WebDriver Interactions functions
wdi.click(by);
wdi.sendKeys(by,value);
wdi. ...
3b. Use native WebDriver functions
wdi.driver.navivate().to(myUrl);
wdi.driver.getCurrentUrl();
wdi.driver.findElement(by);
wdi.driver. ...

Recommended implementation

Even WebDriverInteractions object can be used straight forward as shown previuosly, the recommendation is to create in your project a class Wdi that extends WebdriverInteractions. This way, you will be able to easely customize WebDriverInteractions creating your custom technical actions based on WebDriverInteractions.

public class MyWebDriverInteractions extends WebDriverInteractions {
    public MyWebDriverInteractions(WebDriver driver) { super(driver); }

    // custom technical action to select a value within a custom dropdown
    public void selectInList(By by, String value) {... doSomething(); ...}
}

Then instanciate your newly class Wdi and you are ready to play with WebDriverInteractions functions as well as your custom functions. All ot them from a single object.

WebDriver driver;
...
driver = new ChromeDriver();
...
MyWebDriverInteractions wdi = new MyWebDriverInteractions(driver);
wdi.driver.navivate().to(url);
wdi.click(by);
wdi.sendKeys(by,value);
wdi.selectInList(by,value);
...

Objects

Notice that main of the function are already knowned by selenium expert. We try, as much as possible, to keep same name and parameters as native selenium in order to reduce the awareness effort.

We include in the documentation the main selenium function embeded in the WebDriver Interactions function.

Name Description
WebDriverInteractions Includes functions to interact with the elements in a page as well as the following objects
  • WebDriver
  • JavascriptExecutor
  • Wait
WebDriver The WebDriver itself used for direct calls to WebDriver functions
JavascriptExecutor A JavascriptExecutor object to execute javascript
Wait Dynamic wait engine based on a FluentWait with fancy configurations

WebDriverInteractions functions

Constructor

Name Description
WebDriverInteractions(
    WebDriver driver)
WebDriverInteractions constructor initializating a Browser, a Wait and a JavaScript objects with defaut settings

Get elements or element information

Name Description
getElement(By by) Once present, return a WebElement matching the locator
getInteractableElement(By by) Once interactable, return a WebElement matching the locator.
Same as getInteractableElement(by, false, true, true)
getInteractableElement(
    By by,
    boolean waitUntilElementIsDisplayed,
    boolean waitUntilElementIsEnabled,
    boolean scrollIntoView)
Once interactable, return a WebElement matching the locator
count(By by) Return the number of elements matching the locator
isPresent(By by) Return true is there are one or more elements matching the locator
isAbsent(By by) Return true is there are 0 elements matching the locator

Interact with elements

Name Description
click(By by)

Once interactable, click on the element matching the locator

Selenium : WebElement.click()
dblclick(By by)

Once interactable, double click on the element matching the locator

Selenium : n/a
clearAll(boolean alwaysClear)

If true, sendKeys() will always clear the element before setting the new value.

Default is false (do not clear before setting the value).
clearNext(boolean alwaysClear)

This function configure the next call to sendKeys() to clear or not the element before setting the new value.

Return a WebDriverInteractions object

  • Assuming clearall = false, set the value : sendKeys(by, value)
  • Assuming clearall = false, clear and set the value : clearNext(true).sendKeys(by, value)
The call of this function affects only the first following sendKeys() action
clear(By by) Once interactable, clear input and textarea element matching the locator
sendKeys(
    By by,
    CharSequence... value)

Once interactable, set the value of the (input) element

Selenium : WebElement.sendkeys()
upload(
    By by,
    String fileAbsolutePath)

Once interactable, upload a file using an input element

Selenium : WebElement.sendkeys()
setSelected(By by, Boolean value)

Once interactable, set the property 'selected' of an (checkbox, radio btn) element

Selenium : WebElement.click()
Select getSelect(By by)
selectByVisibleText(
    By by,
    String visibleText)
selectByValue(
    By by,
    String value)
selectByIndex(
    By by,
    int index)

Once interactable, return a Select or perform the corresponding action

Selenium : new Select(by) or new Select(by).xxx

Others functions

Name Description
getAlert() One present, return the alert element
switchToDefaultContent()

Switch to the default content (top of the page) of the current page

Selenium : driver.switchTo().defaultContent()
switchToParentFrame()

Switch to the parent frame of the current frame

Selenium : driver.switchTo().parentFrame();
switchToFrame(By by) Once available, switch to the frame identified by its locator
switchToFrame(int index) Once available, switch to the frame identified by its index
switchToFrame(String nameOrId) Once available, switch to the frame identified by its name or ID
switchToTab(int index)

Once present, show tab with the given index

Selenium : driver.switchTo().window()
closeTab()

Close current tab

Selenium : driver.close()
setScrollIntoViewOptions(
    String behavior,
    String block,
    String inline)
Set scrollIntoView javascript command parameters
Default values :
  • public static final String DEFAULT_SCROLL_BEHAVIOR = "auto";
  • public static final String DEFAULT_SCROLL_BLOCK = "center";
  • public static final String DEFAULT_SCROLL_INLINE = "center";
scrollIntoView(
    WebElement webElement)

Scroll the page to display the element as specified by the scrollIntoView's options.

By default, the element is displayed in the middle of the page.
scrollIntoView(
    WebElement webElement,
    String behavior,
    String block,
    String inline)
Call the scrollIntoView function with custom settings

Wait functions

Name Description
Wait(WebDriver driver) Public constructor
pollingEvery(Duration interval)

Set the interval to be used while performing the checks

public static final Duration DEFAULT_INTERVAL = Duration.ofMillis(50)
withTimeout(Duration timeout)

Set the timeout to be used to perform the checks

public static final Duration DEFAULT_TIMEOUT = Duration.ofMillis(10000);
ignoreAll(Collection exceptions) List exceptions to be ignored when performing the checks
ignoreTimeoutException()

By default the engine wait for a condition that must be realized. If not, there is a failure and the test stops with failed status. Some times we need to wait for a condition that may be realized (or not).

This function configure the wait engine to wait for a condition to be realized and if not relalized to continue the test.

Return a Wait object

  • wait.elementToBePresent(By by) :
    if the condition is not realized the test stop with status failed
  • wait.catchTimeoutException().elementToBePresent(By by) :
    if the condition is not realized the test continue
The call of this function affects only the first following wait
until(Function expectedCondition)

This functions wait for the expectedCondition to be relazided and return the result of the expectedCondition

  • wait.until(
        ExpectedConditions.textToBePresentInElement(element, text))
  • wait.until(input -> 0 < driver.findElements(by).size())
until(
    Function expectedCondition,
    final Duration interval,
    final Duration timeout)
until(
    Function expectedCondition,
    final Collection ignoredExceptions)
until(
    Function expectedCondition,
    final Duration interval,
    final Duration timeout,
    final Collection ignoredExceptions)
Wait for the expectedCondition with specific settings and return the result of the expectedCondition
static until(
    Function expectedCondition,
    WebDriver driver,
    Duration interval,
    Duration timeout,
    Collection ignoredExceptions)
Wait for the expectedCondition with custom settings and return the result of the expectedCondition