Mouse and keyboard actions

In Selenium WebDriver, actions can be either mouse actions or keyboard actions. The selenium-based Actions API provides support to click on a particular location with or without elements and use keyboard shortcuts efficiently.

  • The build() method generates all the composite actions. The following is the syntax for this function:
    action.build();

    The following code snippet explains to you how you can generate actions using the build() method:

    Actions action = new Actions(driver);
    action.click(driver.findElement(By.locatorType("path")));
    action.build();
  • The click() method performs a mouse click on the current mouse pointer location. The following is the syntax for this function:
    action.click();
    action.click(driver.findElement(By.locatorType("path")));

    Unlike clicking on the mouse's current location, this function allows you to click on an element by locating it (using the Actions class). Please check the following snippet for both cases:

    driver.get("http://www.google.com");
    Actions action = new Actions(driver);
    action.click().build().perform();
    action.click(driver.findElement(By.id("gsri_ok0"))).build().perform();
  • The clickAndHold() method lets you click and hold the current mouse pointer location. The following is the syntax for this function:
    action.clickAndHold();
    action.clickAndHold(driver.findElement(By.locatorType("path")));

    Here, the selected source element is on hold unless the pressed key is released using the release() method. The following is a piece of code to click and hold an element:

    action.clickAndHold().build().perform();
    action.clickAndHold(driver.findElement(By.locatorType("path"))).build().perform();
  • The contextClick() method allows you to pop up the contextual menu, in instances such as when the right-click mouse operation occurs. The following is the syntax for this function:
    action.contextClick();
    action.contextClick(driver.findElement(By.locatorType("path")));

    To do a context-click, it is important to locate an element before activating this action. Let's get through a snippet to understand the working of the contextClick() method:

    driver.get("http://docs.seleniumhq.org");
    Actions action = new Actions(driver);
    WebElement ele = driver.findElement(By.xpath("//div[@id='mainContent']/p[1]/i"));
    action.moveToElement(ele).contextClick().build().perform();
  • The doubleClick() method double-clicks on the current mouse pointer location. It also allows you to double-click on an element by locating it. The following is the syntax for this method:
    action.doubleClick();
    action.doubleClick(driver.findElement(By.locatorType("path")));

    To double-click on an element, link text, link, context, or HTML5-based elements, the doubleClick() actions method is the right candidate. The following is a code snippet that selects a context by locating an element:

    driver.get("http://docs.seleniumhq.org");
    Actions action = new Actions(driver);
    WebElement ele = driver.findElement(By.xpath("//div[@id='mainContent']/p[1]/i"));
    action.doubleClick(ele).build().perform();
  • The dragAndDrop() function allows the user to drag an item from the source element and drop it on the target element. The following is the syntax for this function:
    action.dragAndDrop(WebElement source, WebElement target);

    Obviously, this method allows you to click and hold an element from a draggable source location, drag the element, and finally drop it in a suitable target location. Let's see how we can perform this action in the following example:

    Actions action = new Actions(driver);
    WebElement source = driver.findElement(By.locatorType("path"));
    WebElement target = driver.findElement(By.locatorType("path"));
    action.dragAndDrop(source, target).build().perform();
  • The dragAndDropBy() function allows the user to drag an item from the source element and drop it on the target location using x and y coordinates. The following is the syntax of this function:
    action.dragAndDropBy(WebElement source, int xOffset, int yOffset);

    Unlike the dragAndDrop() method, this function uses offset values as a target location to release elements. Let's see how we can perform this action in the following example:

    Actions action = new Actions(driver);
    WebElement source = driver.findElement(By.locatorType("path"));
    action.dragAndDropBy(source, 456, 234).build().perform();
  • The keyDown() method allows the user to press or hold a specific key without releasing it. The following is the syntax for this function:
    action.keyDown(Keys theKey);

    Let's see an example that selects multiple keys using the control button in the pressed state:

    Actions action = new Actions(driver);
    WebElement source = driver.findElement(By.locatorType("path"));
    WebElement target = driver.findElement(By.locatorType("path"));
    action.keyDown(Keys.CONTROL);
    action.click(source);
    action.click(target);
    action.keyUp(Keys.CONTROL);
    action.perform();
  • The keyUp() method releases the key that is already in a pressed condition. The following is the syntax for this function:
    action.keyUp(Keys theKey);

    The following example illustrates how we can move to the bottom of the page view using the Ctrl + End key combination and finally release the Ctrl key:

    Actions action = new Actions(driver);
    action.keyDown(Keys.CONTROL).sendKeys(Keys.END).keyUp(Keys.CONTROL).build().perform();
  • The moveByOffset() method moves the mouse pointer to a specific location using the x and y coordinates:
    action.moveByOffset(int xOffset, int yOffset);

    By default, the mouse offset will be located in the top-left corner of the page, that is, (0,0). Customizing the x and y coordinates moves the mouse pointer to the desired location, as follows:

    Actions action = new Actions(driver);
    action.moveByOffset(234, 345).build().perform();
  • The moveToElement() method moves the mouse pointer to a specific element. Besides this, the mouse pointer can also be moved from a source element to the target location using xy coordinates. The following is the syntax for this function:
    action.moveToElement(WebElement source);
    action.moveToElement(WebElement source, int xOffset, int yOffset);

    This method directly scrolls the page view by highlighting the mouse pointer to the middle of the given element. Let's see how this method works with the following example:

    Actions action = new Actions(driver);
    WebElement source = driver.findElement(By.locatorType("path"));
    action.moveToElement(source).build().perform();
    action.moveToElement(source, 234, 345).build().perform();
  • The perform() method executes actions. The following is the syntax for this function:
    acton.perform();

    Though the build() function generates actions, the perform() method is the key to executing action commands. The following is a piece of code to generate and execute actions:

    Actions action = new Actions(driver);
    WebElement source = driver.findElement(By.locatorType("path"));
    action.click(source);
    action.build();
    action.perform();
  • The release() method drops an item fetched by the action of left-clicking the mouse. This function also releases an item to a specific element by locating it. The following is the syntax for this function:
    action.release();
    action.release(WebElement target);

    Let's see how the release() method works with the drag and drop functionality in the following code snippet:

    WebElement source = driver.findElement(By.locatorType("path"));
    WebElement target = driver.findElement(By.locatorType("path"));
    Action dragAndDrop = action.clickAndHold(source)
    .moveToElement(target)
    .release(target)
    .build();
    dragAndDrop.perform();
  • The sendKeys() method controls the keyboard functions using keys. This function can also perform actions by locating elements. The following is the syntax for this function:
    action.sendKeys(Keys theKeys);
    action.sendKeys(Keys theKeys, Keys theKeys);
    action.sendKeys(WebElement target, Keys theKeys);

    This method supports Selenium users in three different flavors. Multiple combinations of keyboard shortcuts can be easily achieved using the sendKeys() method, as shown in the following code snippet:

    Actions action = new Actions(driver);
    WebElement target = driver.findElement(By.locatorType("path"));
    action.sendKeys(Keys.TAB).build().perform();
    action.sendKeys(Keys.CONTROL, Keys.END).build().perform();
    action.sendKeys(target, Keys.TAB).build().perform();

    Some of the helpful tasks using the above methods are given as follows:

    • Zoom In: Use HTML tags for the advanced zoom-in and zoom-out features. The zoom-in feature gives better focus on a page under test. The following snippet explains well the zoom-in action on both Mac and Windows platforms:
      WebElement html = driver.findElement(By.tagName("html"));
      // WINDOWS
      html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
      // MAC
      html.sendKeys(Keys.chord(Keys.COMMAND, Keys.ADD));
    • Zoom Out: The zoom out function is carried out by hitting the Ctrl+- key combinations. The following snippet explains well the zoom-out action:
      WebElement html = driver.findElement(By.tagName("html"));
      // WINDOWS
      html.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
      // MAC
      html.sendKeys(Keys.chord(Keys.COMMAND, Keys.SUBTRACT));
    • Zoom 100%: This snippet ensures that the page is fully loaded with 100 percent zoom capacity:
      WebElement html = driver.findElement(By.tagName("html"));
      // WINDOWS
      html.sendKeys(Keys.chord(Keys.CONTROL, "0"));
      // MAC
      html.sendKeys(Keys.chord(Keys.COMMAND, Keys."0"));
    • Enter: To submit a form or press the Enter key, the following piece of code will be helpful:
      driver.findElement(By.locatorType("path")).sendKeys(Keys.RETURN);
      driver.findElement(By.locatorType("path")).sendKeys(Keys.ENTER);
    • Drag and Drop: Consider an application containing the drag and drop feature, in which an element is being dragged from one location to another. The source and target elements ought to be declared first so that the actions library prepares the drag and drop actions. Let's see the types of methods that allow you to perform a drag and drop functionality throughout the web page UI, as follows:

      Method 1: In this model, the dragAndDrop action method is directly used to perform a drag and drop action by moving an item from the source element to the expected target element location:

      WebElement source = driver.findElement(By.locatorType("path"));
      WebElement target = driver.findElement(By.locatorType("path"));
      
      Actions action = new Actions(driver);
      Action dragAndDrop = action.dragAndDrop(source, target).build();
      dragAndDrop.perform();

      Method 2: Here, the source and target elements are predefined, following a series of actions to click, hold, move, and release an element:

      WebElement source = driver.findElement(By.locatorType("path"));
      WebElement target = driver.findElement(By.locatorType("path"));
      Actions action = new Actions(driver);
      Action dragAndDrop = action.clickAndHold(source)
      .moveToElement(target)
      .release(target Element)
      .build();
      dragAndDrop.perform();	

      Method 3 (Using Java Robot for HTML5 pages): The Java Robot class is an optional method to drag and drop an element, similar to the Actions API. Let's see an example web page built with HTML5 technology that doesn't support Selenium's Actions method to drag and drop an element:

      @Test
      public void dragAndDrop() throws AWTException, InterruptedException {
      
      driver.get("http://demo.kaazing.com/forex/");
      Actions action = new Actions(driver);
      WebElement sourceElement = driver.findElement(By.xpath("(//li[@name='dragSource'])[13]"));
      Action drag = action.clickAndHold(sourceElement).build();
      drag.perform();
      
      WebElement targetElement = driver.findElement(By.xpath("//section[@id='section1']/div[2]"));
      Point coordinates = targetElement.getLocation();
      Robot robot = new Robot(); //Robot for controlling mouse actions
      robot.mouseMove(coordinates.getX(), coordinates.getY() + 120);
      Thread.sleep(2000);
      robot.mouseMove(coordinates.getX(), coordinates.getY() + 110);
      Thread.sleep(5000);
      }
    • Mouse Hover: When a user tries to hover over elements in a web page (for example, link text), the mouse hover events are automatically triggered to perform an action. Let's look at a snippet to mouse hover an element with the link text locator:
      Actions action = new Actions(driver);
      WebElement HoverLink = driver.findElement(By.linkText("value"));
      action.moveToElement(HoverLink);
      action.perform();

Note

Refer to the following link to check all the available Actions keyboard keys:

https://sites.google.com/site/seleniumworks/keyboard-actions

..................Content has been hidden....................

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