Converting Selenium IDE to WebDriver

I’m in the middle of converting a bunch of Selenium IDE tests into Java WebDriver tests. Selenium IDE can export its scripts in Java WebDriver format, but there are a bunch of issues that you need to resolve manually – UNLESS you have the following regex-fu.

First, while still in selenese (the IDE native format), make sure all DOM references have an explicit selector. Usually it’s id, but you could use “name”. Selenese will helpfully try both if you leave it out, but WebDriver will assume nothing, so you must be explicit. Most places I use id-ed elements it’s to store something, so I run this regex over my whole test script directory. There are bunch of tabs and linefeeds in there, so the best bet is to copy and paste a representative bit of code out of the selenese file into your find/replace script:

Find:

	<td>storeText</td>
	<td>([^=]+?)</td>

Replace:

	<td>storeText</td>
	<td>id=\1</td>

Next, we need to deal with the fact that IDE will do variable interpolation in strings, and WebDriver will not. The exporter doesn’t deal with this at all, so we need to turn things like “Hello ${username},” into “Hello” + username + “,”. This regex over the exported WebDriver scripts will do the trick.

Find:

\\\\$\\\\\{(.*?)\\\\\}

Replace:

" + \1 + "

Finally, the exporter will throw a huge number of try/catch blocks into the output so you can accumulate error messages if you want. If, like me, you just want the script to fail when an assertion fails, do a couple more passes over your WebDriver code to replace both of these expressions with empty strings:

\t\ttry \{\R
\t\t\} catch \(Error e\) \{\R\t\t  verificationErrors\.append\(e\.toString\(\)\);\R\t\t\}\R

Finally, put this function in your WebDriver test class. The IDE exporter will generate code that references it, but you must define it.

	private boolean isElementPresent(By by){
	    return driver.findElements(by).size() != 0;
	}

That does 90% of the work in a few seconds. After that, some test cases run straight away, others I still have to clean up, usually because I have some implicit id reference somewhere. Note: I run these regexes in Eclipse, other regex engines might require some tweaking to the patterns.

Leave a Reply

Your email address will not be published. Required fields are marked *