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
The reason why a RuntimeException is thrown is because the way PHPUnit's selenium driver works.
It considers certain situations as errors that perform a stop() of the test execution. In particular, the code that stops the test in that situation is the following:
I already opened an issue regarding this way of handling errors in the driver at https://github.com/sebastianbergmann/phpunit/issues/276
BTW, removing the calls to stop() in both doCommand() and getString() of /usr/share/php/PHPUnit/Extensions/SeleniumTestCase/Driver.php will make your code able of catching the exception and handling it as you prefer.
By using
getEval()
, you can execute the javascripthasAttribute()
function. UsingfindElement()
, will allow you to work with any type of locator pattern.Note that
getEval()
returns a string, not a boolean.Please excuse me if this does not apply to Selenium RC. In Selenium IDE, one can use the
assertElementNotPresent
command, which (despite the name) can determine whether a given attribute is present. It's only parameter is an element locator that can be of the form
element-id@attribute
.Of course this will only be appropriate if you know which elements should have the attribute in question. If not then I guess you'll have to iterate through element sets using XPath expressions.
You could first check if the element is present using XPath
//location/of/element[@onclick]