SELENIUM Mouse hover by JAVASCRIPT EXECUTOR:
=============================================
package com.rameshsoft;
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;
public class MouseOverUsingJS {
WebDriver driver;
@Test
public void test() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\desktop1\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.get("https://www.flipkart.com");
Thread.sleep(3000);
WebElement ele=driver.findElement(By.xpath("//*[text()='Electronics']"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.border='4px groove green'",ele);
mouseHOver(ele);
}
public void mouseHOver(WebElement ele)
{
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript),ele);
System.out.println("mouse over axtion performed");
}
}
=============================================
package com.rameshsoft;
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;
public class MouseOverUsingJS {
WebDriver driver;
@Test
public void test() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\desktop1\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.get("https://www.flipkart.com");
Thread.sleep(3000);
WebElement ele=driver.findElement(By.xpath("//*[text()='Electronics']"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.border='4px groove green'",ele);
mouseHOver(ele);
}
public void mouseHOver(WebElement ele)
{
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript),ele);
System.out.println("mouse over axtion performed");
}
}
No comments:
Post a Comment