Performing double-click on an element

There will be elements in a web application that need double-click events fired to perform some actions. For example, double-clicking on a row of a table will launch a new window. The Advanced User Interaction API provides a method to perform a double-click.

In this recipe, we will use the Actions class to perform double-click operations.

How to do it...

Let's create a test that locates an element for which a double-click event is implemented. When we double-click on this element, it changes its color:

package com.secookbook.examples.chapter04;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.interactions.Actions;
import static org.junit.Assert.*;
import org.junit.Test;

public class DoubleClickTest {
  @Test
  public void testDoubleClick() throws Exception {
    WebDriver driver = new ChromeDriver();
    driver.get("http://cookbook.seleniumacademy.com/DoubleClickDemo.html");

    try {
      WebElement message = driver.findElement(By.id("message"));

      // Verify color is Blue
      assertEquals("rgba(0, 0, 255, 1)",
          message.getCssValue("background-color"));

      Actions builder = new Actions(driver);
      builder.doubleClick(message).perform();

      // Verify Color is Yellow
      assertEquals("rgba(255, 255, 0, 1)",
          message.getCssValue("background-color"));
    } finally {
      driver.quit();
    }
  }
}

How it works...

To perform a double-click on an element, the doubleClick() method of the Actions class is called. To call this method, we need to create an instance of the Actions class, as shown in the following code:

Actions builder = new Actions(driver);

The doubleClick() method needs the element on which the double-click event will be fired. We can call the doubleClick() method by passing the element, as follows:

builder.doubleClick(message).perform();

See also

  • The Using Advanced User Interactions API for mouse and keyboard events recipe
  • The Performing drag-and-drop operations recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.216.74