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

}


Selenium : Check Variable Type

Selenium : Check Variable Type


class Typetester 
 {
    void printType(byte x) {
        System.out.println(x + " is an byte");
    }
    void printType(int x) {
        System.out.println(x + " is an int");
    }
    void printType(float x) {
        System.out.println(x + " is an float");
    }
    void printType(double x) {
        System.out.println(x + " is an double");
    }
    void printType(char x) {
        System.out.println(x + " is an char");
    }
}
 
 
 
Then:
 

Typetester t = new Typetester();
t.printType( yourVariable );
 

Selenium : Cover basic Browser opening and using Xpath


Selenium : Cover basic Browser opening and using Xpath 


Good Post From Gautirao's Blog





package my.example.test;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class BasicAppTest {

WebDriver currentBrowser;

@Test
public void runTestForInternetExpolrer()
 {
// Instantiate internet explorer browser instance
this.currentBrowser =new InternetExplorerDriver();
this.clickOnSubmitButton();
}

@Test
public void runTestForFireFox()
 {
this.currentBrowser = new FirefoxDriver();
this.searchSomethingOnGoogle();
}

private void searchSomethingOnGoogle() 
{
 
// this ensure the driver queries dom  for 20 seconds before throwing //NoSuchElementFoundException if an element is not found
this.currentBrowser.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
//Types http://google.co.uk in the address bar and hits enter
this.currentBrowser.get("http://google.co.uk");
//findElement method of the driver takes and By instance . We can search for the element //using By.id, By.xpath,By.name etc
WebElement searchInput = this.currentBrowser.findElement(By.xpath("//input[@name='q']"));
searchInput.sendKeys("Hello World");
WebElement myButton = this.currentBrowser.findElement(By.xpath("//input[@name='btnG' and @value='Google Search']"));
myButton.click();
}

@After
public void tearDown() throws Exception 
{
this.currentBrowser.quit();
}
}