How to highlight elements in WebDriver during runtime

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 
 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);  
           }  
      }  
 }  

Share this

Related Posts

Previous
Next Post »

14 comments

comments
January 18, 2018 at 2:10 AM delete

Java & Selenium : File download automation not working in headless mode with ChromeDriver solution needed ASAP

Reply
avatar
April 25, 2018 at 11:49 PM delete

Everyone wants to get unique place in the IT industry’s 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.

software testing course in chennai

Reply
avatar
July 13, 2018 at 10:36 PM delete

Wonderful post!!Thank you for sharing this info with us.
Keep 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

Reply
avatar
November 18, 2018 at 9:46 PM delete

This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up selenium Online Training Hyderabad

Reply
avatar
May 20, 2020 at 7:18 AM delete

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
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

Reply
avatar
June 18, 2020 at 10:29 PM delete

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.
Selenium Training in chennai | Selenium Training in anna nagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery

Reply
avatar
February 4, 2021 at 11:46 PM delete

This brilliant site really has the entirety of the data I needed concerning this subject and didn't have a clue who to inquire.
evrmag

Reply
avatar
September 29, 2022 at 2:59 AM delete

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

Reply
avatar
September 30, 2022 at 8:06 AM delete

Polite 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

Reply
avatar
February 5, 2023 at 1:00 AM delete This comment has been removed by the author.
avatar
February 5, 2023 at 1:05 AM delete

I 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

Reply
avatar
March 12, 2023 at 12:31 PM delete

If you want ad-free streaming and access to exclusive channels, without requiring users to pay a subscription fee. Then you must try Live Tv.

Reply
avatar
March 12, 2023 at 12:44 PM delete

If 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
avatar


your ad