Selenium WebDriver

Mouse Events & Actions Class

Master mouse events and Actions class including click, double-click, right-click, drag-and-drop, scroll, and mouse hover operations.

Actions Class Overview

Mouse Events in Actions Class

All mouse event methods are present in Actions class and return Actions class. To perform actions on elements using Actions class, create an Actions object by passing the driver instance.

Actions Class Syntax

Actions act = new Actions(driver);

Mouse Events

  • • click() - Click on element
  • • doubleClick() - Double click on element
  • • contextClick() - Right click on element
  • • moveToElement() - Mouse hover without click

Advanced Actions

  • • scrollToElement() - Scroll to element
  • • clickAndHold() - Hold element
  • • dragAndDrop() - Drag and drop
  • • build().perform() - Execute actions

build() and perform() Methods

build() Method

Used to combine multiple actions in a single statement. Present in Actions class, returns Action interface.

act.click(wb).build()

perform() Method

Used to execute each and every combined action. Present in Action interface, returns void.

.build().perform()

Click Operations

1. click() Method

5 Ways to Click on Element

  • 1. WebElement click() method
  • 2. WebElement submit() method
  • 3. Actions class click() method
  • 4. Actions class ENTER keyword
  • 5. JavaScript Executor click() method

Example: https://demoqa.com/automation-practice-form

Click on female radio button using Actions class

package Tutorial8;

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

public class Demo1 {

    public static void main(String[] args) {

        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/automation-practice-form");

        WebElement wb = driver.findElement(By.id("gender-radio-2"));

        Actions act = new Actions(driver);
        act.click(wb).build().perform();
    }
}

2. doubleClick() Method

Example: https://demoqa.com/buttons

Double click on "Double Click Me" button

ChromeDriver driver = new ChromeDriver();
driver.get("https://demoqa.com/buttons");

WebElement wb = driver.findElement(By.id("doubleClickBtn"));
Actions act = new Actions(driver);

act.doubleClick(wb).build().perform();

3. contextClick() Method

Right Click Operation

contextClick() method is used to perform right-click operations on elements

ChromeDriver driver = new ChromeDriver();
driver.get("https://demoqa.com/buttons");

WebElement wb = driver.findElement(By.id("rightClickBtn"));
Actions act = new Actions(driver);

act.contextClick(wb).build().perform();

Drag and Drop Operations

clickAndHold() + release() = dragAndDrop()

Example: https://jqueryui.com/droppable/

Drag element from source to target location

Method 1: Separate Actions

// Hold source element
act.clickAndHold(wb1).build().perform();

// Release at target
act.release(wb2).build().perform();

Method 2: Combined Actions

// Combined in single statement
act.clickAndHold(wb1)
   .release(wb2)
   .build().perform();
ChromeDriver driver = new ChromeDriver();
driver.get("https://jqueryui.com/droppable/");

// Switch to frame containing drag-drop elements
driver.switchTo().frame(0);

WebElement wb1 = driver.findElement(By.id("draggable"));
WebElement wb2 = driver.findElement(By.id("droppable"));

Actions act = new Actions(driver);
act.clickAndHold(wb1).release(wb2).build().perform();

dragAndDrop() Method

Single Method Approach

dragAndDrop() method combines clickAndHold() and release() in a single operation

WebElement wb1 = driver.findElement(By.id("draggable"));
WebElement wb2 = driver.findElement(By.id("droppable"));

Actions act = new Actions(driver);
act.dragAndDrop(wb1, wb2).build().perform();

Scroll and Mouse Hover Operations

scrollToElement() Method

Scroll to Element Location

Used to scroll down or scroll up to element locations automatically

ChromeDriver driver = new ChromeDriver();
driver.get("https://jqueryui.com/droppable/");

WebElement wb = driver.findElement(By.linkText("Position"));
Actions act = new Actions(driver);

act.scrollToElement(wb).build().perform();

moveToElement() Method

Mouse Hover Without Click

Used to mouse over on element without clicking on it (pointing on element)

Example: https://www.amazon.in/

Mouse hover on "Account & Lists" to show dropdown menu

ChromeDriver driver = new ChromeDriver();
driver.get("https://www.amazon.in/");

WebElement wb = driver.findElement(By.id("nav-link-accountList"));
Actions act = new Actions(driver);

act.moveToElement(wb).build().perform();

Practice Assignments

🎯 Assignment 1: Flipkart Navigation

URL: https://www.flipkart.com/

  • • Mouse over on "Electronics"
  • • Mouse over on "Gaming"
  • • Click on "Controller"

🎯 Assignment 2: Context Menu

URL: https://swisnl.github.io/jQuery-contextMenu/demo.html

  • • Right click on button
  • • Click on "Copy" keyword
  • • Switch focus to alert window
  • • Capture alert text and click OK

🎯 Assignment 3: Button Operations

URL: https://testkru.com/Elements/Buttons

  • • Double-Click on button
  • • Right-Click on button
  • • Left-Click on button
  • • Test disabled button
  • • Test color change on hover button

Day 18 Knowledge Check

Question 1 of 5

How many ways can you click on an element in Selenium?

SDET Mastery

Master Test Automation

Home
CurriculumPracticeQ&ACheatsheet
🍵Buy me a Chai

Automation Testing Course

Comprehensive course covering Manual Testing, Java Programming, and Selenium WebDriver

🍵Buy me a Chai
Privacy Policy•GitHub
© 2024 Automation Testing Course. All rights reserved.