Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Actions Class[Mouse Actions]

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 546
    Comment on it

    Actions Class[Mouse Actions]

    In order to perform exact mouse related operations such as moving a mouse on the menu so that sub menus are displayed or dragging and dropping an element etc, we use Actions Class.

    Moving A Mouse Pointer

    In order to perform mouse related operations first we will create the instance of actions class by specifying web driver instance as argument for the action class constructor. Then we call required method present in the actions class.
    In this case we use moveToElement().

    whenever we call any method of actions class then we must call perform().

    Example:

    WebDriver driver = new FireFoxDriver();
    driver.manage().timeouts().implicitlyWait(20,timeunit.SECONDS);
    driver.get("http://www.makemytrip.com");
    WebElement hotelsmenu = driver.findElement(By.xpath("//span[text()='Hotels']"));
    Actions action = new Actions(driver);
    action.moveToElement(hotelsmenu).perform();
    driver.findElement(By.linkText("International Hotels")).click();
    

    Right Clicking on a Link

    In order to right click on a link so that we can select the required option such as "open link in new window" we use a method called "contextClick()" of the actions class.

    driver.contextClick(hotelsmenu).perform();
    

    When we right click on a link it will display the context menu and web driver can't recognize the context menu.

    In order to select the option present in the context menu we use the shortcut such as "T"[New tab], "W"[New window] etc, these shortcuts are typed using sendkeys() of the actions.

    Example:

    WebDriver driver = new FireFoxDriver();
    driver.manage().timeouts().implicitlyWait(20,timeunit.SECONDS);
    driver.get("http://www.makemytrip.com");
    WebElement hotelsmenu = driver.findElement(By.xpath("//span[text()='Hotels']"));
    Actions action = new Actions(driver);
    action.moveToElement(hotelsmenu).perform();
    action.sendkeys("W").perform();
    driver.quit();
    

    Getting ToolTipText

    The tool tip text can be created by using "title" property or using "alt" or "hidden division pop-up".

    Getting ToolTipText if it is title:

    If we inspect the element we can identify whether tooltiptext is created using title or alt.

    If it is title property then we use getAttribute().

    Example:

    driver.get("http://www.flipkart.com/mobiles/nokia-brand");
    String ToolTipText = driver.findElement(By.partialLinkText("Nokia x10i")).getAttribute("title");
    System.out.println(ToolTipText);
    

    Getting ToolTip if it is hidden division:

    WebDriver can't perform operation if the specified element is invisible or hidden. If the tooltiptext is created using hidden division first we will write a code to move the mouse pointer on the element using actions class so that tooltiptext is displayed, then we use findElement() method to identify hidden div. tooltiptext.

    In order to get the tooltiptext content we use getText().

    Example:

    WebDriver driver = new FireFoxDriver();
    driver.manage().timeouts().implicitlyWait(20,timeunit.SECONDS);
    driver.get("http://www.makemytrip.com");
    Actions action = new Actions(driver);
    WebElement Help = driver.findElement(By.id("Tooltip_h"));
    action.moveToElement(Help).perform();
    String Tooltiptext = driver.findElement(By.id("help_overlay")).getText();
    System.out.println(Tooltiptext);
    

    Drag And Drop

    In order to perform drag and drop we can call dragAndDrop() method of actions class which takes the source and target element as arguments.

    Example:

    public class Drag
    {
    driver.get("http://www.dhtmlgoodies.com/submitted-scripts/i-google-l-drag-drop/");
    Actions action = new Actions(driver);
    WebElement src = driver.findElement(By.xpath("//h1[text()='Block1']"));
    WebElement trg = driver.findElement(By.xpath(action.dragAndDrop(src,trg)).perform();
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: