Actions(class) and Action(interface) in selenium | demonstration of mouse events in selenium| drag and drop images from source to target location demo in java-selenium

Sadakar Pochampalli
JasperSoft BI Suite Tutorials - Sadakar Pochampalli )

Hi, in this page, we will discuss about Actions(class) and Action(interface) and mouse events example in selenium with some key take away notes.

For, keyboard events tutorial, click this link.


Actions

  • It is a class and the package is org.openqa.selenium.interactions.Actions
  • It represents collection of individual Action that you want to perform.
  • Using this class we can handle keyboard and mouse events. i.e., 
    • Keyboard interface methods 
    • Mouse interface methods 

Action 

  • Action is an interface 
  • It represents single user interaction. 
  • Using this interface, on the Actions object we perform series of actions. 
  • Most widely and must use method is perform() after creating series of actions and storing in Action
Mouse events examples 
   clickAndHold(),
   clickAndHold(WebElement target),
   moveToElement(WebElement target),
   moveToElement(WebElement target, int xOffset, int yOffset),
   release() ,
   release(WebElement target) and etc. 

  Implements the builder pattern: Builds a CompositeAction containing all actions specified
          by the method calls

  • build() method
    • The build() method is always the final method used
    • All the listed actions will be compiled into a single step.
  • Definition by the book: 
Generates a composite action containing all actions so far,
ready to be performed (and resets the internal builder state,
so subsequent calls to build() will contain fresh sequences).

Use case : drag and drop images from source location to target 

  1. Locate the source/from element which you want to drag and store it in a WebElement variable.
  2. Locate the target/to element where you want to drop the source element and store it in a WebElement variable. 
  3. We work with Actions(class) & Action(interface) together so let's first create  Actions object "builder" by attaching "driver" object.
  4. Build the series of actions on "builder" object and store it in "Action" interface variable say "drop1Image1".
    NOTE : Must apply build() method at the end.
  5. Perform the series actions using "perform()" method on the above created Action variable say "drop1Image1"
The code snippet below clicks and holds --> moves --> and releases the images from the source location to target location.
    WebElement drag1FromImage1 = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'The peaks of High Tatras')]"));
    WebElement dropImagesTo
    = driver.findElement(By.xpath("//div[@id='trash']"));

    Actions builder
    = new Actions(driver);
    Action drop1Image1
    = builder.clickAndHold(drag1FromImage1)
    .moveToElement(dropImagesTo)
    .release(dropImagesTo)
    .build();
    drop1Image1
    .perform();


    Demo site credits and courtesyhttps://www.globalsqa.com/demo-site/draganddrop/
    Click on the images to enlarge: 
    Before mouse events: 

    After mouse events: 


    Watch this space for walk through video tutorial for the example

    MouseEventsActionsDragAndDropImagesDemo.java
    //drag and drop - mouse events demo
    package mouse.event.drag.and.drop.elements;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.interactions.Action;

    public class MouseEventsActionsDragAndDropImagesDemo {

    public static void main(String[] args) throws InterruptedException {

    WebDriver driver
    ;

    System
    .setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver_83.exe");
    System
    .setProperty("webdriver.chrome.silentOutput", "true");

    driver
    = new ChromeDriver();
    driver
    .get("https://www.globalsqa.com/demo-site/draganddrop/");

    driver
    .manage().window().maximize();

    //drag and drop image from one location to another on web-page

    // switch to Frame
    driver
    .switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame lazyloaded']")));

    // source/from location
    WebElement drag1FromImage1
    = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'The peaks of High Tatras')]"));
    WebElement drag2FromImage2
    = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'The chalet at the Green mountain lake')]"));
    WebElement drag3FromImage3
    = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'Planning the ascent')]"));
    WebElement drag4FromImage4
    = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'On top of Kozi kopka')]"));

    // target/to location
    WebElement dropImagesTo
    = driver.findElement(By.xpath("//div[@id='trash']"));

    //Object of Actions class
    Actions builder
    = new Actions(driver);

    //Building the series of actions
    Action drop1Image1
    = builder.clickAndHold(drag1FromImage1) // without releasing clicks the the image source location
    .moveToElement(dropImagesTo) //moves the mouse to the middle of target location
    .release(dropImagesTo) //releases the mouse at the current mouse i.e, at target location
    .build(); // build all the above 3 actions

    //Performing the built actions
    drop1Image1
    .perform();

    Action drop2Image2
    = builder.clickAndHold(drag2FromImage2)
    .moveToElement(dropImagesTo)
    .release(dropImagesTo)
    .build();
    drop2Image2
    .perform();

    Action drop3Image3
    = builder.clickAndHold(drag3FromImage3)
    .moveToElement(dropImagesTo)
    .release(dropImagesTo)
    .build();
    drop3Image3
    .perform();

    Action drop4Image4
    = builder.clickAndHold(drag4FromImage4)
    .moveToElement(dropImagesTo)
    .release(dropImagesTo)
    .build();
    drop4Image4
    .perform();

    Thread
    .sleep(5000);
    driver
    .quit();

    }
    }

    Stay tuned for more automation!

    - Sadakar Pochampalli
    Feedback