Friday, 8 May 2015

How to perform mouse over by using java script in selenium webdriver

package New;

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;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

import com.thoughtworks.selenium.webdriven.commands.IsElementPresent;

public class MouseOverByJavascript {
    WebDriver driver;

    @Test
    public void mouseoverjavascript() throws InterruptedException {
        driver.get("http://www.flipkart.com/");
        System.out.println("url is opened");
        driver.manage().window().maximize();
        WebElement element = driver.findElement(By
                .xpath("//a[@data-tracking-id='electronics']"));
        System.out.println("identified ");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        String javaScript = "var evObj = document.createEvent('MouseEvents');"
                + "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);"
                + "arguments[0].dispatchEvent(evObj);";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(javaScript, element);
        System.out.println("happened");
        Thread.sleep(8000);
    }

   
    @BeforeClass
    public void beforeClass() {

        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterClass
    public void afterClass() {
        driver.quit();
    }

}

No comments:

Post a Comment