Sunday, 23 October 2016

Handling Keyboard and Mouse Events with Selenium WebDriver


In many cases we will come across a scenario where we want to automate special type of Keyboard and Mouse events and for which Selenium Web Drover Provided Advance User Interface API with class names Actions and  Action.Handling keyboard events and mouse events in Selenium is very simple so let us start with it.

Let us see how we can use the Special Events in our Selenium Tests
Video Tutorial -


Test Scenario - Consider you want to perform mouse over action on the About Me menu as per given in below screenshot. As you observe the my site menu when you mouse over , it will change back ground color so lets see how we can test it using Selenium.



Test Case -


ackage mypackage;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class myclass {

public static void main(String[] args) {
String baseUrl = "http://www.qaautomated.com/";
       System.setProperty("webdriver.chrome.driver", 
         "C:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

        driver.get(baseUrl);           
        WebElement aboutMe= driver.findElement(By.id("aboout_me"));
    
        
        Actions builder = new Actions(driver);
        Action mouseOverAboutMe = builder
                .moveToElement(aboutMe)
                .build();
        
        String bColor = td_Home.getCssValue("background-color");
        System.out.println("Before Mouse hover: " + bColor );        
        mouseOverAboutMe.perform();        
        bColor = td_Home.getCssValue("background-color");
        System.out.println("After Mouse hover: " + bColor );
        driver.quit();
}
}

Most Frequently Used  Keyboard and Mouse Events APIs -

1. clickAndHold() - Clicks without releasing at the pointed mouse location.

2. contextClick() -
Performs a context-click (Right Click) at the pointed mouse location.

3. doubleClick()-
Performs a double-click at the pointed mouse location.

4. dragAndDrop(source, target) -
Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
source- web element to emulate button down at.
target- web element to move to and release the mouse at.

5.dragAndDropBy(source, x-offset, y-offset) - Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
source- web element to emulate button down at.
xOffset- offcet ofhorizontal move.
yOffset- offset o fvertical move.

6.keyDown(modifier_key) -
Does not release the modifier key - subsequent interactions may assume it's kept pressed.
modifier key  sample =Keys.ALT, Keys.SHIFT, or Keys.CONTROL etc.

7.keyUp(modifier _key) -
Releases already pressed modified key.

8.moveByOffset(x-offset, y-offset) -
Moves the mouse from its current position by the given offset.
x-offset- horizontal offset. ( negative value =  moving the mouse left)
y-offset- vertical offset(negative value = moving the mouse up)

9. moveToElement(toElement) -
Moves the mouse to the middle of the element

10.release() -Releases the pressed left mouse button at the current mouse location pointer.

11.sendKeys(onElement, charsequence) -
Sends a series of keystrokes onto the element.

Please share this with your friends and leave your feedback in comment section.

5 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

  2. Hello author,

    I found your blog while searching for the updates in Selenium Keyword,I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about selenium tutorials for beginners with reference of your blog.

    ReplyDelete
  3. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
    <a href="http://www.traininginmarathahalli.in/selenium-training-in-bangalore/”> Selenium Training in Marathahalli </a>|

    ReplyDelete
  4. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals. Selenium Training In Bangalore | Best Selenium Training in Bangalore

    ReplyDelete