Friday, January 14, 2011

XPATH LOCATORS DEMO JAVA FILE



package com.demo.tests;

import com.thoughtworks.selenium.DefaultSelenium;

public class XPATHLocatorsDefinitionTest {


public static void main(String[] args) {
try{
int i = 1;
int count = 0;
int noOfRows = 0;
String optionLabel = "";
String searchableString = "Two";
DefaultSelenium selenium = new DefaultSelenium("localhost", 8888, "*iexplore","http://localhost:8383");
selenium.start();
selenium.open("http://localhost:8383/testApplication/cssLocators.html");
selenium.setSpeed("3000");
selenium.windowMaximize();
/**
* isElementPresent is a method for checking whether the element given in xpath is showing in the page or not
* It will return true if the element is displayed or false if the element is not displayed
* For every css locator u need to use / or // or xpath= prior to every locator.
* Finding the number of rows in a Table
* Here infoTable is the table ID
* We refer an element with the ID as follows E[@id=value] Here E stands for element.
* id stands for ID of that particular element
* Here the locator table[@id='infoTable']/tbody/tr["+i+"]
* The first part table[@id='infoTable']/tbody/tr["+i+"] indicates we are referring the row of a table
* If we want to refer the first row table tr[1] indicates the first row reference
* Through tr[arg] is a index location used for finding the paticular child of the parent.
* Here arg is the location of the child like 1,2,3,........with respect to table
*/

while(selenium.isElementPresent("//table[@id='infoTable']/tbody/tr["+i+"]")){
noOfRows++;
i++;
}
System.out.println("The number of rows in the Table are : "+noOfRows);

/**
* Fetching the complete rows Data
* Here all the rows Data is gathered
* We can pass through the index from 1,2,3.........
*/

i = 1;
while(selenium.isElementPresent("//table[@id='infoTable']/tbody/tr["+i+"]")){
System.out.println(""+i+" Row Data "+selenium.getText("//table[@id='infoTable']/tbody/tr["+i+"]"));
i++;
}

for(i=1;i<=noOfRows;i++){
System.out.println("Value of i "+i);
if(selenium.getText("//table[@id='infoTable']/tbody/tr["+i+"]/td[2]").indexOf("Two")!=-1){
count++;
}
}
System.out.println("The no of rows in a table that has the Vendor column data has "+searchableString+" is : "+count);


selenium.highlight("//table[@id='infoTable']");
System.out.println("Getting the Table Data From The Body Tag");
System.out.println(selenium.getText("//body/table[@id='infoTable']"));


/**
* This is the example for finding the particular Element in the page using its immediate adjacent Element
* This is very useful When u have same link like
* Administrator Login
* Registered User Login
* Like here Login is a common link but we need to identify it with the adjacent text beside the Login
* Here i am finding the
*/
System.out.println("Highlight Adjacent Elements ");
selenium.highlight("//span[contains(text(),'Administrator')]/following-sibling::a");
System.out.println("First Anchor Data "+selenium.getText("//span[contains(text(),'Registered User')]/following-sibling::a"));
System.out.println("Anchor with text data "+selenium.getText("//a[contains(text(),'Login')]"));


if(selenium.isElementPresent("//div[@id='adminWelcome']")){
System.out.println("Admin Welcome message Using E[@id=value] notation "+selenium.getText("//div[@id='adminWelcome']"));
}else{
System.out.println("Admin Welcome message Using E[@id=value] notation not found");
}

if(selenium.isElementPresent("//div[contains(@id,'Welcome')]")){
System.out.println("Admin Welcome message Using E[contains(@att,value)] notation "+selenium.getText("//div[contains(@id,'Welcome')]"));
}else{
System.out.println("Admin Welcome message Using E[contains(@att,value)] notation not found");
}

if(selenium.isElementPresent("//div[@class='adminRelated']")){
System.out.println("Admin Welcome message Using E[@class=value] notation "+selenium.getText("//div[@class='adminRelated']"));
}else{
System.out.println("Admin Welcome message Using E[@class=value] notation not found");
}

i = 1;
noOfRows = 0;
while(selenium.isElementPresent("//select[@id='country']/option["+i+"]")){
noOfRows++;
i++;
}
for(i=1;i<=noOfRows;i++){
optionLabel = selenium.getText("//select[@id='country']/option["+i+"]");
selenium.select("css=select#country", "label="+optionLabel);
selenium.select("css=select#country", "label="+optionLabel);
System.out.println(""+i+" Option Value : "+selenium.getSelectedValue("//select[@id='country']")+" Option Label "+optionLabel);
}

/**
* Checking for the element with the attributes disabled and checked
*/
if(selenium.isElementPresent("//input[@type='text' and @id='ipBox']")){
if(selenium.getAttribute("//input[@type='text' and @id='ipBox']@disabled").equals("true")){
System.out.println("The input field is disabled");
System.out.println("The input field value is : "+selenium.getValue("//input[@type='text' and @id='ipBox']"));
}else{
System.out.println("In not entering");
}
}else{
System.out.println("The input field is not displayed");
}
if(selenium.isChecked("//input[@type='checkbox' and @id='chkBox']")){
System.out.println("The input field is checked");
}else{
System.out.println("The input field is not checked");
}
}catch(Exception e){
e.printStackTrace();
}
}

}

No comments:

Post a Comment