tirsdag 3. mai 2011

Appending contextual logs to your selenium/tellurium tests.

So we have some selenium tests we run, and they either succeeed or fail. When it fails we usually know what it is, and some other times we need to go into the logs to figure out what went wrong. Since we had a lot of things going wrong for us, we found it a bit annoying that we had to go into the logs all the time.

So this applies to any test case using junit4, but is probably only relevant to applications that runs and logs what happens to it :) Typically I think this could suit a selenium test. We used telliurium+groovy as the dsl for out tests. We wrote a watchman called LogWatchMan that extends the "org.junit.rules.TestWatchman". We override the starting-method and call

"ssh ${user}@${host} wc -l ${path}".execute().text 

Where the ${path} placeholder is the path to the log and ${user}@${host} is the user and the host. This will give us the count of lines in the given log(s). Take the count of lines and store it in a map with the logname. You will use the number of lines when you override finished

This requires you to do a password less login with ssh, because you don't want to have to answer a prompt. (or you could alternativly implement something like Expect in groovy).

Check out how to do a passwordless login here; http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/internet/node31.html

Right, so when the tests have been run, the finished-method in the watchman will be called. In this method you will iterate over your previous result and repeat the action for each of the logs you've checked and get the line count of the logs. You will compare the line numbers, and if the line count is different, perform the following call

"ssh ${user}@${host} sed -n ${countBefore},${countNow}p '${path}'".execute().text

Where the ${countBefore} placeholder is our previous count of the given log, and the ${countNow} placeholder is the current count of the given log. The ${path} placeholder is in this case the path to the log. And then you write out the log to a file, f.ex.

This gives very little overhead to the tests, and will give you the contextual log of whatever system you are testing.

If you feel like implementing it yourself, and need an example, don't hesitate to get in touch.

0 kommentarer:

Copyright © >> /dev/null
Blogger Theme by BloggerThemes Design by Diovo.com