Sunday 26 April 2015

Java Webdriver - Mouseover using javascript

Most of the times we will be using 'Actions' class API methods to perform the mouseover events in the automation scripts developed using webdriver.
Mouseover operation can also be performed by using javascript(i.e. using 'JavascriptExecutor').
Javascript mouseover can be used as the workaround in the automation script when ever webdriver 'Action' class api methods are failing to perform the mouseover.

Webelement holding the ObjectLocator path.
WebElement element = driver.findElement(By.xpath("//a"));

Type cast the driver instance to 'JavascriptExecutor'.
JavascriptExecutor js = (JavascriptExecutor) driver;

Performs the mouseover operation using javascript.
js.executeScript("arguments[0].onmouseover()", element);

Below code demonstrates mouseover using 'Javascript'.
Sample code:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MouseOver {
    static WebDriver driver = null;
   
    public static void main(String[] args) throws InterruptedException {
       
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
       
        driver.get("http://net-comber.com/mouseovr.html");       
        Thread.sleep(2000);
               
        //MouseOver Logic --- starts ---
            WebElement element = driver.findElement(By.xpath("//a"));
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].onmouseover()", element);

        //MouseOver Logic --- ends ---
           
        Thread.sleep(3000);                         
        driver.close();
        driver.quit();
    }   
}

Below code demonstrates mouseover using 'Actions' class API methods of webdriver.

Sample code:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MouseOver {
    static WebDriver driver = null;
   
    public static void main(String[] args) throws InterruptedException {
       
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
       
        driver.get("http://net-comber.com/mouseovr.html");       
        Thread.sleep(2000);
               
        //MouseOver Logic --- starts ---
            WebElement element = driver.findElement(By.xpath("//a"));
            Actions action = new Actions (driver);
            action.moveToElement(element).build ().perform ();
        //MouseOver Logic --- ends ---
           
        Thread.sleep(3000);                         
        driver.close();
        driver.quit();
    }   
}

if "action.moveToElement(element).build ().perform ();" is not working then add below code
"action.moveToElement(element,0,0).build ().perform ();".

Environment:
Above script is executed on
OS: Windows 7
Brower: Firefox 37
Selenium Webdriver : 2.45.0
Java : 7

No comments:

Post a Comment