Friday, 27 December 2013

Selenium Webdriver - Get Background color of an element in Hex - Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.*;

import java.util.*;
import java.lang.*;

import org.openqa.selenium.*;

public class class1 {
public static void main(String[] args) {
//Initiate Driver  
WebDriver driver = new FirefoxDriver();
driver.get("http://www.w3.org/Talks/2012/0416-CSS-WWW2012/Demos/transitions/demo-transitions-1.html#no_transition");
String text;
WebElement WebElement1 = driver.findElement(By.xpath("//li[@id='no_transition']"));
text =WebElement1.getCssValue("background-color").toString();
//Split css value of rgb
String[] numbers = text.replace("rgba(", "").replace(")", "").split(",");
int number1=Integer.parseInt(numbers[0]);
numbers[1] = numbers[1].trim();
int number2=Integer.parseInt(numbers[1]);
numbers[2] = numbers[2].trim();
int number3=Integer.parseInt(numbers[2]);
String hex = String.format("#%02x%02x%02x", number1,number2,number3);
System.out.println(hex);

}
}

Selenium Webdriver - Count Words of Paragraph -Example

public static void main(String[] args)
{

      WebDriver driver = new FirefoxDriver();
      driver.navigate().to("http://www.tizag.com/htmlT/htmlparagraphs.php");
       String text;
       int len;
       int i;
   
     text= driver.findElement(By.xpath("/html/body/table[3]/tbody/tr/td[2]/table/tbody/tr/td/div[4]/p")).getText().toString();
      len=text.length();
 
      int counter=0;
      for (i=0;i<=len-1;i++)
      {
   
     if(Character.isLetter(text.charAt(i)))
     {
     counter++;
     for(;i<=len-1;i++)
     {
     if(text.charAt(i)==' '){
     counter++;
                      }
              }
     }
     }
     System.out.print(counter);
     System.out.print("\n");

//For Counting Duplicate words
     int len2=counter/((int)2);
     text=text.trim();
     String[] words = text.split(" ");
     int counter1=0;
    for(i=0;i<=len2;i++)
    {
   
   
    for(int j=0;j<=len2;j++)
    {
    if(words[i]==words[j])
    {
    System.out.print(words[i]);
    System.out.print("---");
    System.out.print(words[j]);
        System.out.println();
    counter1++;
    }
   
       
    }
       
   
    }

Selenium Webdriver - How To get options of Dropdown -Example


WebDriver driver1 = new FirefoxDriver();
driver.navigate().to("http://www.tizag.com/htmlT/htmlselect.php");
WebElement dropdown = driver.findElement(By.xpath("/html/body/table[3]/tbody/tr/td[2]/table/tbody/tr/td/div[4]/select"));
  Select value = new Select(dropdown);
  List<WebElement> options =  value.getOptions();

   for(int i=0;i<options.size();i++)
  {
  String ValueToFind="Colorado -- CO";
  String OptionValue;
  OptionValue=options.get(i).getText();
  if(OptionValue.contains(ValueToFind))
  {
  System.out.print("Option is present ");
  }

  }