Double Click On Button Using Actions Class Of Selenium WebDriver

As you know, We can use WebDriver Actions class where we need to perform series of actions to complete operation. We have learnt many tutorials on usage of Actions class In selenium webdriver to perform such tricky advanced user Interactions like drag and drop element, selecting JQuery selectable Items, Moving slider, selecting date from JQuery date picker etc.. You can view all these advanced user Interactions tutorials links on THIS PAGE with practical examples on each one.

Same way, Double clicking on button Is series of actions as you needs to click two time on button. Simple single button click Is possible by click() method In webdriver but to perform double click action, we need to use Actions class of selenium webdriver. You can learn how to right click on select option from context menu In THIS POST.

I have created practical example to show you how to use Actions class with doubleClick() and perform() methods to double click on element.

package Testing_Pack;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DoubleClick {
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/09/selectable.html");
 }
 
 @Test
 public void doubleClick_Button() throws IOException, InterruptedException {
  WebElement ele = driver.findElement(By.xpath("//button[contains(.,'Double-Click Me To See Alert')]"));
  
  //To generate double click action on "Double-Click Me To See Alert" button.
  Actions action = new Actions(driver);
  action.doubleClick(ele);
  action.perform();
  
  Thread.sleep(3000);
  String alert_message = driver.switchTo().alert().getText();
  driver.switchTo().alert().accept();
  System.out.println(alert_message);
 }
}

When you run example, It will double click on "Double-Click Me To See Alert". Rest of code Is to handle alert generated by button double click. So this Is the way to double click on any element In selenium webdriver test.

2 comments: