Wednesday, September 4, 2013

Selenium : HighLighting WebElements

Selenium : WebDriver 2.0 HighLight


 public static void highlightElement(WebDriver driver, WebElement element)
 {
   JavascriptExecutor js = (JavascriptExecutor) driver;
   for (int i = 0; i < 3; i++)
   {       
    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",element, "color: yellow; border: 4px dashed orange;");
     try
     {
      Thread.sleep(150);
      } catch (InterruptedException e) {
       e.printStackTrace();
       }
          js.executeScript("arguments[0].setAttribute('style',arguments[1]);",element, "");
      try
      {
        Thread.sleep(150);
        } catch (InterruptedException e) {
          e.printStackTrace();
           }
         }
 js.executeScript("arguments[0].setAttribute('style', arguments[1]);",element, "");
   }

}






Here is the full program implementation of the same :


import javax.swing.JOptionPane;

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;


public class Sample1
{

       public static void main(String[] args)  
       {
              WebDriver oDriver;
              WebElement oTitle;        
              String sRes="Not Found";
             
              oDriver=new FirefoxDriver();
              oDriver.get("https://www.google.co.in");
              oTitle=oDriver.findElement(By.id("pmocntr2"));//gs_htif0
             
              if (oTitle!=null)
              {
                      sRes="Found";
                      highlightElement(oDriver,oTitle);      
              }     
             
              JOptionPane.showMessageDialog(null, sRes);                   
       }
      
       public static void highlightElement(WebDriver driver, WebElement element)
       {
               JavascriptExecutor js = (JavascriptExecutor) driver;
           for (int i = 0; i < 3; i++)
           {        
               js.executeScript("arguments[0].setAttribute('style', arguments[1]);",element, "color: yellow; border: 4px dashed orange;");
               try
               {
                           Thread.sleep(150);
                     } catch (InterruptedException e) {
                           e.printStackTrace();
                     }
               js.executeScript("arguments[0].setAttribute('style', arguments[1]);",element, "");
               try
               {
                           Thread.sleep(150);
                     } catch (InterruptedException e) {
                           e.printStackTrace();
                     }
           }
           js.executeScript("arguments[0].setAttribute('style', arguments[1]);",element, "");
       }

}


No comments:

Post a Comment