Mouse Hover and Double Click using Selenium

As the world is evolving at a rapid pace and especially in the field of software development, testing plays a critical role in making the process defect or bug-free. Selenium is one such tool that lets us automate the whole process of manual testing & that we term as Automation Testing. And, this article is on “How to perform double click & Mouse hover using selenium“.

After completion of this tutorial you will be able to perform a right click, double click, move to Element. This tutorial provides a detailed explanation of each step, snapshot and code which not only help you in understanding even help you in writing your error-free code.

What is Selenium WebDriver?

Selenium is a web automation tool which provides testing framework to automate the testing process of web applications. Test script in selenium can be written in many programming languages like- Java Python, C# etc.

Mouse Hover & Click in Selenium:

Highly interactive website designed to understand user behaviour and help according to the user. To achieve this most of website responded mouse hover also. Probably most of us found that when we hover the mouse over menu it getting expanded and more options getting visible and we perform operations on them. Fortunately, Selenium Actions class provides wonderful methods for mouse hover.

Refer below step by step tutorial for mouse hover and other mouse-related operations.

Sample Website where complete menu visible on mouse hover.

Menu displayed on Mouse hover

Pre requisite:

Selenium set up should be done and TestNg should be configured.
(If Selenium set up is not done then please check read this tutorial – Selenium and TestNG Setup Tutorial).

Selenium Mouse Hover & Click Methods:

Selenium Action Class

Read out official documentation from Selenium: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html

Steps to write Automation Script for Mouse Hover & Click using Selenium:

  • Step 1: Create one Test Class: Open Selenium Project and create one class to write Action class demo test.
  • Step 2: Write code to Invoke Browser: We use WebDriverManager for browser binaries and invoke browser. This code is common for all the test method so we are keeping this test under beforeTestSetUP and use Testng annotation @BeforeMethod. As we know @BeforeMethod code is executed before each TestNG method.

(For more detailed explanation refer our blog- Selenium Installation Tutorial with Example)

public class dragAndDropTest {
public static WebDriver driver = null;@BeforeMethod public void beforeTestSetUP() { WebDriverManager.chromedriver().version("79.0").setup(); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
}
}

Steps for Right-click:

  • Step 3: Create test method to write rightClick() methods.

Refer below for code steps and code explanation:

driver.get("http://swisnl.github.io/jQuery-contextMenu/demo.html");
  • Step 4: Store the webElement and make object of action class.
WebElement rightClickableElement = driver.findElement(By.xpath("//span[contains(text(),'right click me')]"));
Actions action = new Actions(driver);
  • Step 5: Use contextClick method for Right click on above web element.
action.contextClick(rightClickableElemet).build().perform();
driver.findElement(By.xpath("//html//body//ul//li//span[contains(text(),'Paste')]")).click();
//Accept generated Alert
driver.switchTo().alert().accept();

Steps for Double click:

  • Step 6: Create new method for doubleClick and write Code to navigate on page.
driver.get("https://kybarg.github.io/bootstrap-dropdown-hover/");
  • Step 7: Find xpath of target element and store as web element.
WebElement dropDownonHover = driver.findElement(By.xpath("//body/div/div/div/div/div/div[1]/button[1]"));
  • Step 8: Make object of Action Class
Actions action = new Actions(driver);// driver passed as constructor
  • Step 9: Use method moveToElement on step 6 object.
action.moveToElement(dropDownonHover).perform();

Example 1 – testMethod:

public void mouseHover() throws InterruptedException {
driver.get("https://kybarg.github.io/bootstrap-dropdown-hover/");
Thread.sleep(5000);
WebElement dropDownonHover = driver.findElement(By.xpath("//body/div/div/div/div/div/div[1]/button[1]"));
Actions action = new Actions(driver);
action.moveToElement(dropDownonHover).perform();
WebElement onemoreDropDown = driver.findElement(By.xpath("//body/div/div/div/div/div/div[1]/ul[1]/li[3]/a[1]"));
action.moveToElement(onemoreDropDown).perform();
WebElement MoreDropdown = driver.findElement(By.xpath("//body/div/div/div/div/div/div[1]/ul[1]/li[3]/ul[1]/li[3]/a[1]"));
action.moveToElement(MoreDropdown).build().perform();
Thread.sleep(7500); // added delay for visual validation
}
  • Step 8: Write Code to close set after every Test Method. After each test we will close browser.
@AfterMethod public void closeSetUp() { 
if (!driver.equals(null)) { 
driver.quit(); 
}
 }
Action Class in selenium

Automation Script For Mouse Hover and Double Click using Selenium WebDriver:

Complete Code: (Write Automation Script For Mouse Hover and Double Click using Selenium WebDriver)

package stepDef;
import io.github.bonigarcia.wdm.WebDriverManager;
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.logging.LogEntries;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
public class actionClassDemo {
public static WebDriver driver = null; 
@BeforeMethod 
public void beforeTestSetUP() { WebDriverManager.chromedriver().version("79.0").setup(); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
} 
@Test 
public void rightClick() { driver.get("http://swisnl.github.io/jQuery-contextMenu/demo.html"); WebElement rightClickableElemet = driver.findElement(By.xpath("//span[contains(text(),'right click me')]")); 
Actions action = new Actions(driver); action.contextClick(rightClickableElemet).build().perform(); driver.findElement(By.xpath("//html//body//ul//li//span[contains(text(),'Paste')]")).click(); driver.switchTo().alert().accept(); 
} 
@Test 
public void mouseHover() throws InterruptedException { driver.get("https://kybarg.github.io/bootstrap-dropdown-hover/"); 
Thread.sleep(5000); 
WebElement dropDownonHover = driver.findElement(By.xpath("//body/div/div/div/div/div/div[1]/button[1]")); 
Actions action = new Actions(driver); action.moveToElement(dropDownonHover).perform(); WebElement onemoreDropDown = driver.findElement(By.xpath("//body/div/div/div/div/div/div[1]/ul[1]/li[3]/a[1]")); action.moveToElement(onemoreDropDown).perform(); WebElement MoreDropdown = driver.findElement(By.xpath("//body/div/div/div/div/div/div[1]/ul[1]/li[3]/ul[1]/li[3]/a[1]")); action.moveToElement(MoreDropdown).build().perform(); Thread.sleep(7500); // added delay for visual validation } 
@AfterMethod 
public void closeSetUp() { 
if (!driver.equals(null)) { 
driver.quit(); 
} 
}
}

List of steps:

Below steps involved in above tutorial:

  1. Open Browser
  2. Navigate to webpage
  3. Launch Browser
  4. Find draggable and droppable web Elements
  5. Make object of Action class
  6. Use moveToElement method followed by perform to mouse hover
  7. Use method contextClick() for right Click
  8. Use method doubleClick() for double click.
  9. Close browser.

Great, now you have completed mouse hover operation using Selenium Actions class. Hope you understand concepts well and able to use in your project. Feel free to write us on query@thoughtcoders.com in case of any difficulty/feedback.

References:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html

Subscribe to our Newsletter:

0 0 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact