Saturday 31 December 2016

Highlighting Elements under Action with Selenium

We can highlight the WebElements under action during the execution to help capture them while failure analysis.

It can be done using the below code.

Have the utility function in for framework ,use it for all the actions performed at the framework level. See the sample below.

Function to highlight the WebElement under execution:

public static void elementHighlight(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style',arguments[1]);,
element"color: red; border: 3px solid red;");

}


Use it as below, used in the plain function here, you need to embed it in your framework as to highlight all the elements under action.

public static void main(String[] argsthrows InterruptedException {
        System.setProperty("webdriver.gecko.driver","Path to geckodriver");
drivernew FirefoxDriver();
driver.get("http://www.google.com");
        WebElement searchBox = driver.findElement(By.name("q"));
elementHighlight(searchBox);
searchBox.sendKeys("Test");
WebElement searchBtn = driver.findElement(By.name("//button"));
searchBtn.click();
}

In the above example i am using Selenium 3.0.1 jars.
Note: If you are using Selenium 2.x version skip the below line from above code.
System.setProperty("webdriver.gecko.driver","Path to geckodriver");


5 comments: