How to perform the Context-click and Double-click Actions


#1

contextClick() Action

In some scenarios, we may need to do a right click action or context click on an element to perform some action. For example, to copy some text or similar. In Selenium WebDriver we use Action class to perform the context (right) click.

In this example, we use the Login element (button) from this website.

The steps for performing this Action are the following:

  • Locate your elements! Use Ranorex Selocity to generate the needed selectors from the element on whom you are performing the context click.

    WebElement LoginElement = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Login));
    
  • Call the Actions class in your method

    Actions act = new Actions(driver);
    
  • Perform the action and use it in the test

    act.contextClick(LoginElement).perform();
    

    The whole action step would look like this:

    public itemsPo contextClick() {
    
         // Locate the element
         WebElement LoginElement = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Login));
    
         // Instantiate the Actions class
         Actions act = new Actions(driver);
    
         // Perform the contextClick() mehtod
         act.contextClick(LoginElement).perform();
    
         return this;
     }
    

    Or as a one-liner:

    (new Actions(driver).contextClick(LoginElement)).perform();
    

doubleClick() Action:

The same principle applies here as for the contextClick(). The only difference is, of course, the method used in the code.

Here we will use the http://api.jquery.com/dblclick/ website to test our double click action.

So, once again, steps for performing the Action:

  • Locate your elements! Use Ranorex Selocity to generate the needed selectors from the element on whom you are performing the context click.

    WebElement DoubleClickElement = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this. DoubleClickElement));`
    
  • Call the Actions class in your method

    Actions act = new Actions(driver);
    
  • Perform the action and use it in the test

    act.doubleClick(DoubleClickElement).perform();
    

    The whole action step would look like this:

    public itemsPo doubleClick() {
    
         // Locate the element
         WebElement doubleClickElement = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.doubleClick));
    
         // Instantiate the Actions class
         Actions act = new Actions(driver);
    
         // Perform the doubleClick() method
         act.doubleClick(doubleClickElement).perform();
    
         return this;
     }
    

    Or as a one-liner:

    (new Actions(driver).doubleClick(DoubleClickElement).perform();
    

Check out this article to find out more about using the Actions class in Ranorex Webtestit!