In this tutorial, we will explain how to work with Actions Class for double clicking an element by taking a simple example. To do the same, we have created a Junit class.

We will exploit the capabilities of WebDriver’s Actions Class in order to perform Double Click. Check out the syntax below:

Actions action = new Actions(driver);
action.moveToElement(button).doubleClick().build().perform();

Script Creation

For script creation, create a Sample Java project in Eclipse IDE. Now create a Junit class and paste the code explained later in this blog.

Explanation of Application under Test

In order to explain the simulation of double click using WebDriver, we have designed a web page which includes two textboxes and a button. The scenario is where the user enters two unique strings values into these textboxes and double clicks on the “Cop Text” button. As soon as the user double clicks the button, a function is triggered which copies the text from Field1 into Field2.

 
DoubleClick1

Subsequent is the HTML code used to create the above mentioned webpage:

<!DOCTYPE html>
<html>
<body>

Field1: <input type="text" id="field1" value="Hello World!"><br>
Field2: <input type="text" id="field2"><br><br>

<button ondblclick="myFunction()">Copy Text</button>

<p>A function is triggered when the button is double-clicked. The function copies the text from Field1 into Field2.</p>

<script>
function myFunction() {
    document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>

</body>
</html>

This code can also be found at http://www.w3schools.com/

Scenario:

  • Launch the browser and open AUT
  • Enter a unique valid string in Field1
  • Enter a unique valid string in Field2
  • Double click on Copy Text button
  • Close the web browser

WebDriver code using Action Class

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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;


public class DoubleClick {

    // objects declaration
    WebDriver driver;

    @Before
    public void setUp() throws Exception {
        // URL of the application under test
        String url = "file:///D:/Shruti/Protechskills/Blogs-Writeups/DoubleClick.html";
        // launch the firefox browser and open the application url
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get(url);
    }

    @After
    public void tearDown() throws Exception {
        // close the web browser window opened by WebDriver
        //driver.quit();
    }

    @Test
    public void test() throws InterruptedException {
        String field1Text = "Double Click Test";
        String field2Text = "Text in field two";
        
        // enter a valid string in Field1

        WebElement field1 = driver.findElement(By.id("field1"));
        field1.clear();
        field1.sendKeys(field1Text);
        
        // enter a valid string in Field2
        
        WebElement field2 = driver.findElement(By.id("field2"));
        field2.sendKeys(field2Text);
        
        Thread.sleep(5000);
        
        // double click the button
        WebElement button = driver.findElement(By.xpath("//button[contains(text(),'Copy Text')]"));
        Actions action = new Actions (driver);
        action.moveToElement(button).doubleClick().build().perform();// An alternative way could be - action.doubleClick(button).build().perform(); 
            
    }
}

Take a note that Actions class is particularly used to emulate the most complex user interactions with the application. Users are advised to use Actions Class’ capabilities to perform mouse or keyboard actions.

Stay tuned…There is a lot more coming up in this space.

Share this:

3 thoughts on “Double Clicking an element in Selenium WebDriver

  1. ルイヴィトン – N級バッグ、財布 専門サイト問屋

    弊社は販売ルイ・ヴィトン) バッグ、財布、 小物、靴類などでございます。
    1.品質を重視、納期も厳守、信用第一は当社の方針です。

    2.弊社長年の豊富な経験と実績があり。輸入手続も一切は弊社におまかせてください。質が一番、最も合理的な価格の商品をお届けいたします。

    3.お届け商品がご注文内容と異なっていたり、欠陥があった場合には、全額ご返金、もしくはお取替えをさせていただきます。

    弊社は「信用第一」をモットーにお客様にご満足頂けるよう、

    送料は無料です(日本全国)! ご注文を期待しています!
    下記の連絡先までお問い合わせください。

    是非ご覧ください!
    誕生日プレゼント https://www.watcher007.com/watch/menu-pid-128.html

Leave a Reply to 誕生日プレゼント Cancel reply

Your email address will not be published. Required fields are marked *