Interaction At a Glance

Figure 1: Interaction Overview

While most methods are straightforward, some words on usage of Actions:
Actions:
is the user-facing API for emulating complex user gestures. A simple example of using Actions for Keyboard and Mouse Events:

//hover mouse over target element
Actions hoverOver= new Actions(driver);
hoverOver.moveToElement(element).build().perform();

Note these 2 special methods:

.build(): is used compile all the listed actions into a single step.
.perform(): A convenience method for performing the actions without calling build() first.

When our actions only contains one operation, like the above example(moveToElement), the build() method can be skipped:

//The following 2 line of codes have the same effect
hoverOver.moveToElement(element).build().perform();
//.build() can be skpped since we only have one action here
hoverOver.moveToElement(element).perform();

We can chain many actions together:

Actions builder = new Actions(driver);
builder.clickAndHold(element1)
.clickAndHold(element2)
.click()
.build()
.perform();

In this case, we are performing more than one operations so we have to use build() to compile all the actions into a single step, and then perform it. .build() method cannot be skipped for actions which contains more than one action.

General rule for interactions:

  • To interact with element via webdriver, the element needs to be visible, and enabled if it is a button.
  • WebDriver will automatically scroll the screen, if necessary to the element when executing .moveToElement().
  • By default actions like .click() is blocking, i.e. webdriver will not return control to our code until Click is done.
  • Before we interact with the element, try to understand the dynamism of the page first, and then come up with a way to make sure that the element is ready to be interacted before we actually do that.

Leave a Reply

Your email address will not be published. Required fields are marked *