How to highlight elements in WebDriver during runtime
In web automation-testing, element highlighter plays a very important role. It helps to track the exact step to be performed. UFT/QTP like testing tool is capable to highlight an element in run time because you will get this feature inbuilt with this tool. In Selenium there is no such feature available so we need to write our own code to highlight element.
Highlighting element is also useful for debugging the code.One way to know steps being performed in the browser is to highlight the web page elements.
The core to use "JavaScriptExecutor" to inject javascript in your application and change the CSS for the element, like create a border for the element.
Sample code
The core to use "JavaScriptExecutor" to inject javascript in your application and change the CSS for the element, like create a border for the element.
Sample code
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].style.border='4px groove red'", Element);
Explanation
Arguments[0].style.border:- this script is injecting the CSS style tag into the element and making its border settings with a 4px wide red line with groove look.
Element: - This is the element around which border will be drawn.
I am creating a method which could be reusable for highlighting the test element.
Element Highlight method
public void HighlightElement(WebDriver driver, WebElement elm){
try{
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].style.border='4px groove red'", elm);
Thread.sleep(1000);
js.executeScript("arguments[0].style.border=''", elm);
}catch(Exception e){
System.out.println(e);
}
}px groove red'", email);
Video
Complete code
package com.selenium.sampleseleniumproject;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AppTest3 {
protected WebDriver driver;
@Test
public void Highlight() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.faceboo.com");
driver.manage().window().maximize();
WebElement email = driver.findElement(By.xpath("//*[@id='email']"));
WebElement pass = driver.findElement(By.xpath("//*[@id='pass']"));
HighlightElement(driver, email);
email.sendKeys("[email protected]");
HighlightElement(driver, pass);
pass.sendKeys("password");
// close browser
driver.close();
}
public void HighlightElement(WebDriver driver, WebElement elm) {
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.border='4px groove red'", elm);
Thread.sleep(1000);
js.executeScript("arguments[0].style.border=''", elm);
} catch (Exception e) {
System.out.println(e);
}
}
}
14 comments
commentsJava & Selenium : File download automation not working in headless mode with ChromeDriver solution needed ASAP
ReplyEveryone wants to get unique place in the IT industrys for that you need to upgrade your skills, your blog helps me improvise my skill set to get good career, keep sharing your thoughts with us.
Replysoftware testing course in chennai
Wonderful post!!Thank you for sharing this info with us.
ReplyKeep updating I would like to know more updates on this topic
Very useful content, I would like to suggest this blog to my friends.
selenium course
Selenium Course in Chennai
This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up selenium Online Training Hyderabad
ReplyI appreciate the admin of this blog and this concept is very comprehensive. Thank you!!!
ReplyCorporate Training in Chennai
Corporate Training institute in Chennai
Spark Training in Chennai
Oracle Training in Chennai
Unix Training in Chennai
Power BI Training in Chennai
Oracle DBA Training in Chennai
Corporate Training in Chennai
Corporate Training institute in Chennai
Thanks for all your share!!!The way of Expressing the concept is Good...I would like to Thank for all Writers...Keep Up the Good writing Style
ReplyJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
Very nice post..After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic.
ReplySelenium Training in chennai | Selenium Training in anna nagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
This brilliant site really has the entirety of the data I needed concerning this subject and didn't have a clue who to inquire.
Replyevrmag
Pristine sound quality and a powerful set of features including high-definition waveforms, four decks, audio effects, video mixing and hardware integration give .Djay professional AI 4.0.3 Crack
ReplyPolite Breakup Messages · In as much as I love you, in as much as I wish we spend the rest of our life together, I still have to tell you that it .Sorry Break Up Message For Boyfriend
ReplyI want to take a moment to thank the Universe for fun, wealth, and joy. I am happy to feel fulfilled in this moment. I am worthy of being loved .Powerful Affirmations
ReplyIf you want ad-free streaming and access to exclusive channels, without requiring users to pay a subscription fee. Then you must try Live Tv.
ReplyIf you want unlimited money , allow you to purchase and upgrade their cars without any restriction, then try Formula racing car. With realistic graphics and physics, you can customize your cars and compete in various races and championships to become the ultimate Formula racing champion.
Reply