"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

January 15, 2011

Selenium - Parsing Lists and Links in a Page

Next Example is to try looping through Lists and Links in a page
Followed the below steps
1. Created Sample Test Page link
2. Create a project and add required libraries. Verify earlier post

Below is the Example Code - Working Code

import static org.testng.Assert.assertTrue;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class TestLists
{
     
public static Selenium selenium;

@BeforeTest
public void openBrowser()
{
      System.out.println("Before Test");
      selenium = new DefaultSelenium("localhost", 4444, "*iehta","http://sqlandsiva.blogspot.com/2011/01/test-page-for-selenium-example.html");
}

@Test
public void LoopthruElement()
{
try
{
      System.out.println("Running Test1");
      selenium.start();
      selenium.open("http://sqlandsiva.blogspot.com/2011/01/test-page-for-selenium-example.html");
      int j=1;
      while(selenium.isElementPresent("//div[@id='OrderList']/ol/li["+j+"]"))
      {
            System.out.println("Value is " + selenium.getText("//div[@id='OrderList']/ol/li["+j+"]"));
            j++;
      }
      j=1;
     
      while(selenium.isElementPresent("//div[@id='UnOrderedList']/ul/li["+j+"]"))
      {
            System.out.println("Value is " + selenium.getText("//div[@id='UnOrderedList']/ul/li["+j+"]"));
            j++;
      }
      j=1;
      while(selenium.isElementPresent("//div[@id='Links']/a["+j+"]"))
      {
            System.out.println("Value is " + selenium.getText("//div[@id='Links']/a["+j+"]"));
            j++;
      }
     
}
catch(Exception ex)
{
                  ex.toString();
}
}
}

This is tested and working code. Run it as TestNG Test. Below is the output for the program













I used firebug XPATH copier to get exact Xpath location. Below is snapshot of the same















Happy Reading!!

No comments: