🗯️Action vs. Actions

🌟 Action 🌟

Action is an interface in Selenium that represents a single user-interaction action. It is defined in org.openqa.selenium.interactions. It contains one of the most widely used method perform(). Action is used when performing a single user interaction on a web page.

Sure, here are all the commands in Action that are not in Actions:

  • Action.click()

  • Action.doubleClick()

  • Action.contextClick()

  • Action.dragAndDrop()

  • Action.keyDown()

  • Action.keyUp()

  • Action.sendKeys()

  • Action.move()

  • Action.release()

These commands are all used to perform simple user-interaction actions. They are all implemented in the Action interface, but they are not all exposed in the Actions class. This is because the Actions class is designed to be more powerful and flexible, and it does not need to expose all of the simple actions that are available in the Action interface.

If you need to perform any of the commands listed above, you can use the Action interface directly. However, if you need to perform more complex actions, you should use the Actions class.

Here is an example of how to use the Action interface to click on an element:

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;

public class Example {

    public static void main(String[] args) {
        WebElement element = driver.findElement(By.id("myElement"));

        Action clickAction = new Action(driver);
        clickAction.click(element);
        clickAction.perform();
    }
}

This code will click on the element with the ID myElement.

🌟 Actions 🌟

Actions is a class in Selenium that is defined in org.openqa.selenium.interactions. It is the user-facing API for emulating complex user gestures. Actions class implements the builder pattern, which can build a CompositeAction containing all actions specified by the method calls. Actions is used when performing a sequence of user interactions on a web page.

Here is an example of using Action and Actions in Selenium:

// Using Action to click on an element
WebElement element = driver.findElement(By.id("example-id"));
Action action = new Actions(driver).click(element).build();
action.perform();

// Using Actions to perform a sequence of actions
WebElement element1 = driver.findElement(By.id("example-id-1"));
WebElement element2 = driver.findElement(By.id("example-id-2"));
Actions actions = new Actions(driver);
actions.click(element1).click(element2).sendKeys("text").build().perform();

In summary, Action is used for a single user interaction, while Actions is used for a sequence of user interactions.

Last updated