How to perform the Key-down and Move to element (hover over element) Action


#1

keyDown() Action

The keyDown(Keys modifierKey) method takes the modifier Keys as the parameter (Shift, Alt, and Control Keys - those modify the purpose of other keys, hence the name). It is used to simulate the action of pressing a modifier key without releasing. Some expected values for the keyDown() method are - Keys.SHIFT, Keys.ALT and Keys.CONTROL, etc.

The workflow for executing this action would be:

  • Locate the element in which you need to, for example, send keys that are all in CAPS. Use Ranorex Selocity to quickly generate the selector of the desired element. We will use our demo shop to try out the keyDown() method, holding down the SHIFT key to make our input all in capital letters.
WebElement keyDownElement = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.keyDownInput));
  • Instantiate the action class
Actions act = new Actions(driver);
  • Perform the keyDown action followed by .sendKeys() method to type into the search box
act.keyDown(keyDownElement, Keys.SHIFT).sendKeys(text).perform();

The complete step would look like this:

public ActionsPo keyDownInput(String text) {
        // Locate the element
        WebElement keyDownInputElement = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.keyDownInput));

        // Instatntiate the Actions class
        Actions act = new Actions(driver);

        // call the keyDown() method, send keys to the input box and perform it
        act.keyDown(keyDownInputElement, Keys.SHIFT).sendKeys(text).perform();

        return this;
} 

Or as a one-liner:

(new Actions(driver).keyDown(this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.keyDownInput)),Keys.SHIFT)).sendKeys(text).perform();

moveToElement() Action

The moveToElement() Action is very helpful in performing the “Hover over” gesture required in many web applications. For example, some website (e.g. https://www.amazon.com) have a hover menu and in order to easily test those menus moveToElement() can be used.
Luckily, moveToElement is one of Ranorex Webtestit’s built-in snippets that you can generate with just one click.
Find out more about Ranorex Webtestit snippets here.

Steps required to perform this action:

  • Locate the element needed to perform the moveToElement() action and to simulate Hover over behavior. Use Ranorex Selocity to quickly generate the selector of the desired element.

  • Drag the generated Element to the code and select Hover over element snippet.

    06

  • The auto-generated code looks like this:

public ActionsPo hoverOver() {
       WebElement hoverMenuElement = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.hoverMenu));
       Actions moveToActions = new Actions(driver);
       moveToActions.moveToElement(hoverMenuElement).perform();

       return this;
}

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