Thursday, February 19, 2009

Testing Ajax pages with WebTest

I just learned that you need to add some delay to the test script after making an Ajax call. (Isn't it logical? Why didn't I think of that?).

For example, assuming there is an onChange handler in the combo box, you should add a sleep step after setSelectField. Groovy code below:

setSelectField(htmlId: 'foo', text: 'bar')
sleep(seconds: 5)

Friday, February 13, 2009

Shell Script in Groovy

It is possible to write a shell script using groovy. To do that, just write the script in a text editor, but remember to add the shebang line. For example, the following script will print "Hello World", groovy style:

#!/usr/bin/env groovy
println "Hello World"

The script can be named anything. It doesn't have to end with .groovy. Caveat: the script will not work if groovy is not in the PATH.

Tuesday, February 10, 2009

Writing Testable Code

If you search the web for advice on how to write testable code, you will end up with a lot of articles describing the strategies and the dos and donts. But, if there's only one thing you need to remember about making your code testable, then take this simple advice: write the tests first, then write your code.

As you start to write your tests, you'll start to think about how to use your code and the APIs that your code provides. You may find that your code has too many dependencies, instead of writing tests, your test case may end up with mostly the codes that build the object graph.

Think of how you will test your code as you design your application, then you'll realize that not all designs can be tested easily. You'll need to decide whether to use the design or to think of another design that is easier to test. All things being equal, choose the design that is easier to test.

I'm not suggesting that you should set strict rules about the writing tests. If you can't test all 10 areas of your code, test 9. If 9 is not possible, test 8. If you at least think about the tests before writing your code, you will have a higher chance of producing better codes.

This post and this elaborate on the issue in more details.