views:

288

answers:

1

Is there any way in Selenium ide, so that we get list and handlers for all controls provided on any page ? So if we get that we can test that one by one using RC and it'll greatly helpful when there are more then 40 controls on page. In that case it'll become very tiresome to record for all.

+1  A: 

In Selenium you can use getXpathCount to get the number of matching elements and then loop through them. The following Java example will output the IDs of the checkboxes on the page:

int checkboxCount = selenium.getXpathCount("//input[@type='checkbox']").intValue();
for (int i = 1; i < checkboxCount + 1; i++) {
    System.out.println(selenium.getAttribute("//body/descendant::input[@type='checkbox'][" + i + "]@id"));
}

In the WebDriver API (to be merged into Selenium 2) there is a findElements method that returns a list of matching elements. The above example would look something like:

for (WebElement checkbox : driver.findElements(By.xpath("//input[@checkbox]"))) {
    System.out.println(checkbox.getAttribute("id"));
}
Dave Hunt
How could find, what all type of controls are there on page ?
VarunVyas
You could just check to see if a type of 'control' is present using by trying to find it. The example I gave would find checkboxes, but you could find other elements by modifying the XPath. If there were no checkboxes on the page, the given example would just output nothing.
Dave Hunt
Thanks for help :)
VarunVyas
You're welcome.
Dave Hunt