Handling keyboard and mouse events

While performing test automation some actions can not be performed directly in WebDriver, in such a case we need to use some tricky ways. Some of such scenarios are sending keyboard inputs to your test or mouse operations on web-elements, etc.

Special keyboard and mouse events are performed using the Advanced User Interactions API. It contains the Actions and the Action classes that are needed when executing these events.

We need to create new action builder instance by passing the WebDriver instance. The following are the most commonly used keyboard and mouse events provided by the Actions class.

Following are some example selenium methods of action class to handle such scenarios.

Example code of mouse events:

Example 1: To perform Mouse hover action

Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText(“menulink”));
actions.moveToElement(mainMenu);

WebElement subMenu = driver.findElement(By.cssSelector(“subLinklocator”));
actions.moveToElement(subMenu);
actions.click().build().perform();

Example 2: ‘build()’ method is used to compile all the list of actions into a single step and ready to be performed.

Actions action = new Actions(driver);

WebElement mainMenu = driver.findElement(By.linkText(“MainMenu”));

action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath(“submenuxpath”))).click().build().perform();

Example 3: To perform drag and drop

Actions action = new Actions(driver);

action.dragAndDrop(Sourcelocator, Destinationlocator).build().perform();

Example 4: To perform right click

Actions action= new Actions(driver);

action.contextClick(MyLink).build().perform();

Example 5: To perform double click

Actions action = new Actions(driver);

action.moveToElement(driver.findElement(By.id(“Name”))).doubleClick().build().perform();

Example 6: Hover and Click

Actions action = new Actions(driver);

action.moveToElement(elementToHover).click(elementToClick).build().perform();

Example 7: USING CUSTOM JAVASCRIPT MOUSE-EVENT

//The below JavaScript code creates, initializes and dispatches mouse event to an object on fly

String strJavaScript = “var element = arguments[0];”

            + “var mouseEventObj = document.createEvent(‘MouseEvents’);”

            + “mouseEventObj.initEvent( ‘mouseover’, true, true );”

            + “element.dispatchEvent(mouseEventObj);”;

//Then JavascriptExecutor class is used to execute the script to trigger the dispatched event.

((JavascriptExecutor) driver).executeScript(strJavaScript, element);

Example 8: USING DEVICE

//Getting mouse from driver object

Mouse mouse =((HasInputDevices)driver).getMouse();         

//Invokes mouseMove method by passing element coordinates as argument

mouse.mouseMove(((Locatable)element).getCoordinates());

 

# Below actions help in simulating mouse events in case of automation using Selenium Webdriver.

  • public Actions click()— Clicks at the current mouse location. Useful when combined with
  • public Actions click(WebElement onElement)— Clicks in the middle of the given element (onElement).
  • public Actions clickAndHold()— Clicks (without releasing) at the current mouse location.
  • public Actions clickAndHold(WebElement onElement)— Clicks (without releasing) in the middle of the given element (onElement)
  • public Actions contextClick()— Performs a context-click at the current mouse location.
  • public Actions contextClick(WebElement onElement)— Performs a context-click at middle of the given element (onElement)
  • public Actions doubleClick()— Performs a double-click at the current mouse location.
  • public Actions doubleClick(WebElement onElement)— Performs a double-click at middle of the given element (onElement)
  • public Actions dragAndDrop(WebElement source, WebElement target)— A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
  • public Actions dragAndDropBy(WebElement source, int xOffset, int yOffset)— a convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
  • public Actions moveByOffset(int xOffset, int yOffset)— Moves the mouse from its current position (or 0,0) by the given offset. If the coordinates provided are outside the viewport (the mouse will end up outside the browser window) then the viewport is scrolled to match.
  • public Actions moveToElement(WebElement toElement)— Moves the mouse to the middle of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.
  • public Actions moveToElement(WebElement toElement, int xOffset, int yOffset)— Moves the mouse to an offset from the top-left corner of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.
  • public Actions release()— Releases the depressed left mouse button at the current mouse location.
  • public Actions release(WebElement onElement)— Releases the depressed left mouse button, in the middle of the given element. Invoking this action without invoking clickAndHold()first will result in undefined behaviour.

 

Following are some example of keyboard events to handle such scenarios.

Example code of keyboard events:
-For the Alt key.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.Alt);

Note*: Here, “String” is the Xpath location and on the element, that we have found, keyboard’s Arrow left will be pressed.

-For the Left arrow key.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.ArrowDown);

-For the left arrow key.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.ArrowLeft);

-For the right arrow key.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.ArrowRight);

-For the up arrow key.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.ArrowUp);

-For the Backspace key.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.Backspace);

-For the function key F5.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.F5);

-For the Enter key.

driver.FindElement(By.XPath(“String”)).SendKeys(Keys.Enter);

 

keyDown and keyUp are the two main methods available under Selenium Webdriver Actions class.

  • public Actions keyDown(Keys theKey)— Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL). Does not release the key – subsequent interactions may assume it’s kept pressed. Note that the key is never released implicitly – eitherkeyUp(theKey) or sendKeys(Keys.NULL) must be called to release.
  • public Actions keyDown(WebElement element, Keys theKey)— Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL) after focusing on an element.
  • public Actions keyUp(Keys theKey)— Performs a modifier key release (SHIFT,Keys.ALT  or Keys.CONTROL). Releasing a non-depressed modifier key will yield undefined behaviour.
  • public Actions keyUp(WebElement element, Keys theKey)— performs a modifier key release after focusing on an element.
  • public Actions sendKeys(java.lang.CharSequence… keysToSend)— Sends keys to the active element. This differs from calling sendKeys(CharSequence…) on the active element in two ways: The modifier keys included in this call are not released and there is no attempt to re-focus the element. So sendKeys(Keys.TAB) for switching elements should work.
  • public Actions sendKeys(WebElement element, java.lang.CharSequence… keysToSend)— Sends keys to the given element.

Leave a comment