views:

777

answers:

3

Is there a way for js code to force Selenium testing to fail? For (a probably bad) example, if I had the following:

return document.getElementById('foo').innerHTML == 'hello'

is there a way I could make the 'runScript' command fail depending on if the js code returned true or false? (I know that example could be used by other Selenium commands, but I wanted a more general solution.)

Do I need to learn how to extend Selenium to add another command?

I'm also relatively new to Selenium so is this something that using Selenium-rc will solve?

A: 

It depends how you want to use selenium. In selenese suite there are calls for verifyText, etc (just google for selenese suite and verifyText, waitForCondition). If you are using the selenium remote control in e.g. java than you do the assertions in the java code. With the remote control you send scripts to the page and you get back what the evaluation of the code returns. The comparsion/assertion is done in the java client.

Norbert Hartl
Have you considered voting up if you accept the answer? :)
Norbert Hartl
+2  A: 

assertExpression will give you what you're asking for.

For instance, the following line would cause your test to fail if the JavaScript expression you mention did not evaluate to true.

<tr>
<td>assertExpression</td>
<td>javascript{this.browserbot.getCurrentWindow().document.getElementById('foo').innerHTML == 'hello'}</td>
<td>true</td>
</tr>

You also might be interested in this thread on storing and comparing JavaScript values with Selenium.

Noah Sussman
A: 

eval('asdfklj;'); //Selenium Hits it. Honestly I've never used it myself. Need to learn it soon, but I'm assuming it can bomb do to javascript errors.

Ballsacian1