Monday 28 October 2013

Getting count of Different Web Element displayed in the page using WebDriver

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

//Getting count of Different Web Element  displayed
public class WebDriverElementCount {

public static WebDriver oBrowser;
public static String sUrl="http://in.yahoo.com";
public static void main(String[] args)
{
boolean bIsBrowserOpened;

bIsBrowserOpened=OpenBrowser();
if(bIsBrowserOpened)
{

Display_Element_Count();
CloseBrowser();
}

}
public static boolean OpenBrowser()
{
try
{
oBrowser=new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch(Exception e)
{
e.printStackTrace();
}
return true;
}
catch(Exception e)
{
System.err.println(e.getMessage());
return false;
}
}

public static void Display_Element_Count()
{
Get_Element_Count("Link","//a");
Get_Element_Count("Image","//img");
Get_Element_Count("TextBox","//input[@type='text']");
Get_Element_Count("Form Submit Button","//input[@type='submit' or @type='clear']");
Get_Element_Count("HTML 4/5 button","//button");
Get_Element_Count("Radio Button","//input[@type='radio']");
Get_Element_Count("CheckBox","//input[@type='checkbox']");
Get_Element_Count("List Box","//select");
Get_Element_Count("Activex Button","//object");
Get_Element_Count("Text Area","//textarea");
}
public static void Get_Element_Count( String sElementName,String sXpath_of_Element)
{
int iCount;
if(!sXpath_of_Element.isEmpty())
{
iCount=oBrowser.findElements(By.xpath(sXpath_of_Element)).size();
System.out.printf("\n No of (%s)  Element= %d" ,
sElementName,iCount);
}
}
public static void CloseBrowser()
{
oBrowser.close();
}

}


Reading Existing list of values from ListBox Using WebDriver

import java.util.List;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;

// - Reading Existing list of values from ListBox
public class WebDriver18 {
/**
* @param args
*/
public static WebDriver oBrowser;
public static String sUrl = "http://www.goibibo.com";
public static void main(String[] args)
{
boolean bIsBrowserOpened;
bIsBrowserOpened = OpenBrowser();
if (bIsBrowserOpened)
{
Get_ListBox_Items();
CloseBrowser();
}
}
public static boolean OpenBrowser()
{
try
{
oBrowser = new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
return true;
}

public static void CloseBrowser()
{
oBrowser.close();
}
public static void Get_ListBox_Items()
{
WebElement oListBox;
int iElement, iCount;
List<WebElement> oAllListValues;
oListBox = oBrowser.findElement(By.id("gi_source"));
oAllListValues = oListBox.findElements(By.tagName("option"));
iCount = oAllListValues.size();
System.out.println("No of Items found in list box ="+String.valueOf(iCount));
System.out.println("*********************************");
for (iElement=0; iElement<iCount; iElement++)
{
System.out.printf("\n Item (%d/%d) = %s",
iElement+1,iCount,
oAllListValues.get(iElement).getText());
}

}

}