{"id":1296,"date":"2016-08-06T06:42:47","date_gmt":"2016-08-06T06:42:47","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1296"},"modified":"2024-12-14T03:29:33","modified_gmt":"2024-12-14T03:29:33","slug":"how-to-highlight-web-elements-on-the-page-in-selenium-tests","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-highlight-web-elements-on-the-page-in-selenium-tests\/","title":{"rendered":"Highlight Web Elements on the page in Selenium tests?"},"content":{"rendered":"<h1>Highlight Web Elements on the page in Selenium tests?<\/h1>\n<p>Let&#8217;s see how to highlight web elements using the Selenium tool. We can use the Event listener to highlight web elements on the web page under test. To accomplish the task, we can use JavascriptExecutor and EventFiringWebDriver. Find the code below that draws a visible border to highlight web element interactions during selenium tests.<\/p>\n<h2><strong>Basic Things<\/strong><\/h2>\n<p><strong>WebDriverEventListener<\/strong> is an interface with method signatures for the set of actions performed each time the event occurs.<\/p>\n<p><strong>AbstractWebDriverEventListener<\/strong> is an abstract base class, you can use this base class if you want to implement a WebDriverEventListener. Methods provided by this class have empty implementations.<\/p>\n<p><strong>WebElementHighlighter<\/strong> is the class only interested in the <em>beforeFindBy()<\/em> method.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-959\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/WebElementHighlighter.png\" alt=\"Highlight Web element\" width=\"1109\" height=\"620\" title=\"\"><\/p>\n<h2><strong>Highlight Web Elements Code<br \/>\n<\/strong><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class WebElementHighlighter extends AbstractWebDriverEventListener {\r\n\r\npublic void beforeFindBy(By by, WebElement element, WebDriver driver){\r\n\r\nWebElement webelement = driver.findElement(by);\r\n\r\nif (driver instanceof JavascriptExecutor) {\r\n ((JavascriptExecutor)driver).executeScript(\"arguments[0].style.border='10px \r\nsolid green'\", webelement);\r\n }\r\n \r\n File scrFile = ((TakesScreenshot) driver)\r\n .getScreenshotAs(OutputType.FILE);\r\n try {\r\n FileUtils.copyFile(scrFile, new File(\"images\/HighlighterScreenCapture\" + \r\ncurrentTime() + \".jpg\"));\r\n } \r\n catch (IOException e) {\r\n e.printStackTrace();\r\n }\r\n }\r\n \r\n public String currentTime()\r\n {\r\n Date todayDate = new Date();\r\n SimpleDateFormat formatter = new SimpleDateFormat(\"ddMMyy_HHmmss\");\r\n String formattDate = formatter.format(todayDate);\r\n return formattDate;\r\n\r\n}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>Sample Selenium Tests<\/strong><\/h2>\n<p>Let&#8217;s code a sample search test method to enter some info on the Twitter page..<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class TwitterTest { \r\n public WebDriver webDriver = null; \r\n public EventFiringWebDriver driver = null; \r\n \r\n @BeforeClass\r\n public void init() {\r\n System.setProperty(\"webdriver.gecko.driver\", \"drivers\/geckodriver.exe\");\r\n webDriver = new FirefoxDriver(); \r\n driver = new EventFiringWebDriver(webDriver); \r\n WebElementHighlighter highlighter = new WebElementHighlighter();\r\n driver.register(highlighter); \r\n }\r\n\r\n\/\/ Twitter page test\r\n @Test \r\n public void SampleTest() throws Throwable { \r\n driver.get(\"https:\/\/www.twitter.com\/login\");\r\n driver.manage().window().maximize();\r\n driver.findElement(By.name(\"session[username_or_email]\")).clear();\r\n driver.findElement(By.name(\"session[username_or_email]\"))\r\n.sendKeys(\"TestingDocs.com\");\r\n WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(120, 1));\r\nwait.until(ExpectedConditions.titleContains(\"Twitter\"));\r\n } \r\n \r\n@AfterClass\r\n public void closeBrowser() {\r\n driver.quit();\r\n }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>The output of the Test<\/strong><\/h2>\n<p>Run the TestNG selenium test. The highlighter highlights the web elements during the test interactions.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6316\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Twitter-Login-page.png\" alt=\"Twitter Login page\" width=\"1824\" height=\"890\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Twitter-Login-page.png 1824w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Twitter-Login-page-300x146.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Twitter-Login-page-1024x500.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Twitter-Login-page-768x375.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Twitter-Login-page-1536x749.png 1536w\" sizes=\"auto, (max-width: 1824px) 100vw, 1824px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2><\/h2>\n<p>TestNG Tutorials on this website can be found at:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.testingdocs.com\/testng-framework-tutorial\/\">https:\/\/www.testingdocs.com\/testng-framework-tutorial\/<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Highlight Web Elements on the page in Selenium tests? Let&#8217;s see how to highlight web elements using the Selenium tool. We can use the Event listener to highlight web elements on the web page under test. To accomplish the task, we can use JavascriptExecutor and EventFiringWebDriver. Find the code below that draws a visible border [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[214],"class_list":["post-1296","post","type-post","status-publish","format-standard","hentry","category-selenium","tag-selenium","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1296"}],"version-history":[{"count":13,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1296\/revisions"}],"predecessor-version":[{"id":26326,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1296\/revisions\/26326"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}