Favourite Quote

Every artist was first an amateur. - Ralph Waldo Emerson The more difficulties one has to encounter, within and without, the more significant and the higher in inspiration his life will be. - Horace Bushnell

Friday, October 22, 2010

Get All link Names ... using xpath

Here's an example of how to get all links & iterate through it.

package com.est.org;

import org.openqa.selenium.server.*;
import com.thoughtworks.selenium.*;
import com.thoughtworks.selenium.DefaultSelenium;


public class epcClient extends SeleneseTestCase
{
public static SeleniumServer selServer;
public static DefaultSelenium selenium = null;
final String browser = "*firefox";
final int MAX_TIME_TO_WAIT = 3000;
final int PORT = 4444;
final String URL = "http://abc.com/";

public void setUp() throws Exception
{
if(selServer == null)
{
try
{
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.trustAllSSLCertificates();
selServer = new SeleniumServer(rc);
selServer.start();
}catch(Exception e)
{
e.printStackTrace();
}
}
}

public void testWebservices()
{
selenium = new DefaultSelenium("localhost",PORT,browser,URL);
selenium.start();
selenium.open("/open");
pause(MAX_TIME_TO_WAIT);
selenium.windowFocus();
selenium.windowMaximize();
pause(MAX_TIME_TO_WAIT);
int count = selenium.getXpathCount("//a").intValue();
for(int i=1;i<=count;i++)
{
//Iterate the received links through getText
String getlink = selenium.getText("xpath=(//a)["+i+"]");
System.out.println("\n Here's the link that you have got !!! "+getlink);
selenium.click("link="+getlink);
pause(MAX_TIME_TO_WAIT);
selenium.click("//input[@type='radio'][5]");
pause(MAX_TIME_TO_WAIT);
selenium.click("//input[@name='Submit' and @value='submit']");
selenium.waitForPageToLoad("30000");
assertEquals(selenium.getTitle(),true);
selenium.open("/open");
pause(MAX_TIME_TO_WAIT);
selenium.windowFocus();
selenium.windowMaximize();
}

}


public void tearDown()
{
selenium.stop();
selServer.stop();
}

} automationwithselenium.blogspot.com-Google pagerank and Worth

No comments:

Post a Comment