views:

111

answers:

2

I have some buttons with an onclick attribute and some that don't. I want to check if the specified element has the onclick attribute. How can I do this?

getAttribute() returns the attribute value when it has one. When it doesn't, it throws a RuntimeException and stops the test (even when I wrap it in a try/catch block).

$onclickValue = $this->getAttribute("$locator@onclick"); //works when the attribute exists
+1  A: 

You could first check if the element is present using XPath //location/of/element[@onclick]

Dave Hunt
That might work, but you are also forced to use XPath to locate the element.
Andrew
+1  A: 

By using getEval(), you can execute the javascript hasAttribute() function. Using findElement(), will allow you to work with any type of locator pattern.

$hasAttribute = $this->getEval('this.browserbot.findElement("' . $locator . '").hasAttribute("onclick")');
if ($hasAttribute === 'true') {
    //attribute exists
}

Note that getEval() returns a string, not a boolean.

Andrew