Friday, January 14, 2011

HTML FILE TO IDENTIFY THE CSS AND XPATH FOR THE ABOVE MENTIONED RULES

Odd Meerasaab ThreeOdd
Even Two2EvenClick Me Three2Even
One3 Two3 Three3

This

headline is very

important


Administrator Login

Registered UserLogin

Welcome Administrator

Ramesh
Hi Meera Hi vamsi



The above is the HTML for which i am writing the Java class .
In this Java class i am trying to include how to use the css and xpath locators for all the mentioned html elements like div,select,input,links and many more.
In the java class read the comments well to understand the things so simple and there is a good comments set for each operation.



JAVA CLASS FOR THE DEMO OF USING CSS LOCATORS

package com.tests;

import com.thoughtworks.selenium.DefaultSelenium;

/**
* The CSSLocatorsDefinitionTest tells how the various css locators will work
* @author - meerasaab786@gmail.com (Meerasaaheb Mohmmad)
*/

public class CSSLocatorsDefinitionTest {

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, "*firefox","http://localhost:8383");
selenium.start();
selenium.open("http://localhost:8383/applicationname/cssLocators.html");

/**
* 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 css= 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 Here E stands for element.
* id stands for ID of that particular element
* Here the locator table#infoTable tr:nth-child("+i+")
* The first part (table#infoTable tr) indicates we are referring the row of a table
* If we want to refer the first row table tr:nth-child(1) indicates the first row reference
* Through nth-child(arg) is a method used for finding the paticular child of the parent.
* Here arg is the location of the child like 1,2,3,........
*/
while(selenium.isElementPresent("css=table#infoTable tr:nth-child("+i+")")){
noOfRows++;
i++;
}
System.out.println("The number of rows in the Table are : "+noOfRows);
/**
* Finding 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("css=table#infoTable tr:nth-child("+i+")")){
System.out.println(""+i+" Row Data "+selenium.getText("css=table#infoTable tr:nth-child("+i+")"));
i++;
}

/**
* I am included this because we have tested this functionality using XPATH locators
* This is the functionality for testing the no of times the rows have a column as "Two".
* contains(arg) is a method to check whether the particular reference has the data as arg in it.
* Here in our case the I am checking the row has the data of "Two" in the second column.
* The Symbol E'>'F is used to find the element F which is the child of E.
* In the below case css=table#infoTable tr:nth-child("+i+")>td
* E stand for css=table#infoTable tr:nth-child("+i+")
* and F stands for the td
* E>F means is finding the first td of the corresponding tr i,e 1,2,3,4.....
* The Symbol '~' is used for finding the element that is adjacent to it.
* E~F means finding the element F which is present adjecent to it(i,e E). *
*
*/
for(i=1;itd~td:contains(searchableString)")){
count++;
}
}
System.out.println("The no of rows in a table that has the Vendor column data has "+searchableString+" is : "+count);

/**
* Highlight is a method in selenium which is used for highlighting a specific Element
* selenium.highlight(String locator)
* Here i am highlighting the table i,e dispalyed in the web page cssLocators.html
* The below is a way of accessing the child element from the parent
* Syntax E F
* The space between E and F is a must
* Here E denotes the body and F denotes the child i,e a table with id infoTable
*
*/

selenium.highlight("css=body table#infoTable");
System.out.println("Getting the Table Data From The Body Tag");
System.out.println(selenium.getText("css=body table"));

/**
* Accessing the first child Element of a particular parent
* :first-child is a pseudo class defined for accessing the first child in the parent
* Similarly we have last-child also
*/

System.out.println("FirstChild of h1 "+selenium.getText("css=h1 p:first-child"));
System.out.println("FirstChild of h1 "+selenium.getText("css=h1 p"));

/**
* The below is the code snippet for getting all the childs that have paragraph tag inside the h1 tag
*/
System.out.println("FirstChild of h1 with descendent child "+selenium.getText("css=h1 * p"));

/**
* 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("css=span:contains('Administrator') + a:contains('Login')");
System.out.println("First Anchor Data "+selenium.getText("css=span:contains('Registered User') + a:contains('Login')"));

System.out.println("First Anchor Data with Tilde is :: "+selenium.getText("css=span:contains('Registered User') ~ a:contains('Login')"));

/**
* Highlighting the same adjecent element Using the CSS Mixing
* E + F
* Finding the element F which is exactly adjacent to the Element E.
* Locating the Element using its ID has the following syntax
* K[attribute=value]
* K[id='nameId'] Here i am finding the span element that has nameId as its id
*
*/
selenium.highlight("css=span[id='nameId'] + a:contains('Login')");
System.out.println("Anchor Data using attribute "+selenium.getText("css=span[id='nameId'] + a:contains('Login')"));
System.out.println("Anchor with text data "+selenium.getText("//a[contains(text(),'Login')]"));

/**
* Accessing the element with its ID and classname
* Here we can use the locator as follows
* E[att=value] Matches the Element E with the attributes exact value
* E#id Matches the Element E with the ID
* E[att$=value] Matches the Element E with the attribute value ends with the provided value
* E[att*=value] Matches the Element E with the attribute value conatins value at any position
* E.cssClassName Matches the Element E with cssClassName provided
*/

if(selenium.isElementPresent("css=div#adminWelcome")){
System.out.println("Admin Welcome message Using Hash i,e E#id notation "+selenium.getText("css=div#adminWelcome"));
}else{
System.out.println("Admin Welcome message Using Hash i,e E#id notation not found");
}
if(selenium.isElementPresent("css=div[id=adminWelcome]")){
System.out.println("Admin Welcome message Using E[att=value] notation "+selenium.getText("css=div[id=adminWelcome]"));
}else{
System.out.println("Admin Welcome message Using E[att=value] notation not found");
}
if(selenium.isElementPresent("css=div[id$=Welcome]")){
System.out.println("Admin Welcome message Using E[att$=value] notation "+selenium.getText("css=div[id$=Welcome]"));
}else{
System.out.println("Admin Welcome message Using E[att$=value] notation not found");
}
if(selenium.isElementPresent("css=div[id*=admin]")){
System.out.println("Admin Welcome message Using E[att*=value] notation "+selenium.getText("css=div[id*=admin]"));
}else{
System.out.println("Admin Welcome message Using E[att*=value] notation not found");
}
if(selenium.isElementPresent("css=div.adminRelated")){
System.out.println("Admin Welcome message Using E[att*=value] notation "+selenium.getText("css=div.adminRelated"));
}else{
System.out.println("Admin Welcome message Using E[att*=value] notation not found");
}
/**
* Example for a descendant id('select')/descendant::*
*/
System.out.println("Descendant ::: "+selenium.getText("xpath=/descendant::tr"));
i = 1;
noOfRows = 0;
while(selenium.isElementPresent("css=select#country>option:nth-child("+i+")")){
noOfRows++;
i++;
}
for(i=1;i<=noOfRows;i++){ optionLabel = selenium.getText("css=select#country>option:nth-child("+i+")");
selenium.select("css=select#country", "label="+optionLabel);
System.out.println(""+i+" Option Value : "+selenium.getSelectedValue("css=select#country")+" Option Label "+optionLabel);
}

if(selenium.isElementPresent("//div[@id='country1']")){
System.out.println("Data");
System.out.println(selenium.getText("//div[@id='country1']"));
}else{
System.out.println("Not found");
}
if(selenium.isElementPresent("//select")){
System.out.println("Data in Select ");
System.out.println(selenium.getText("//select"));
}else{
System.out.println("Not found");
}

/**
* Clicking the checkbox in a td with the assumed link in the desired tr
*/
if(selenium.isElementPresent("//tr[descendant::td/a[contains(text(),'Click')]]/td/input[@name='chkBox']")){
selenium.click("//tr[descendant::td/a[contains(text(),'Click')]]/td/input[@name='chkBox']");
}

if(selenium.isElementPresent("css=tr:nth-child(2):contains('Click')>td>input")){
selenium.click("css=tr:nth-child(2):contains('Click')>td>input");
}
if(selenium.isElementPresent("//descendant::select")){
System.out.println("Yesssssssss "+selenium.getText("//descendant::select"));
}else{
System.out.println("Noooooooooooo");
}
System.out.println("Hidden Field Text "+selenium.getValue("//input[@id='hiddenName']"));
/*selenium.keyPress("akku", "34");
selenium.type("akku", "34");
selenium.keyPress("five", "34");
selenium.type("five", "34");*/


if(selenium.isElementPresent("css=table tr>td:nth-child(indexOftheMailSubject):contains('Ur desired Subject')")){
System.out.println("The element Found");
}else{
System.out.println("The element not Found");
}


selenium.select("//select[@name='locations:selectedLocation']","label=India");

}catch(Exception e){
e.printStackTrace();
}
}
}



No comments:

Post a Comment