Saturday, August 16, 2008

Selenium: Recovery using try catch

All Test tools have a way to handle unexpected events like a popup alert or a page not found error. Like for example in QTP there is something called the recovery scenario manager. Here one could define a bunch of scenarios, one for each unexpected event and the recovery action to be taken if and when such an event presents itself. This would be something like if there is an pop-up alert with text "Session expired. Do you want to re-login?" then Click OK on the dialog box (this takes you to login page) and Login again.

Anyone who is serious of test automation would want to build a sturdy set of recovery scenarios.

So what do you do in case of selenium?
Not to worry. This feature could be easily imitated by a simple Try-catch block within your Selenium RC Java tests.

Class Test1{

public Test1(){
//unexpected dialog pops-up here
catch(Exception e)
}//end of test case
//end of class

Class recoveryModule{

public invokeRecovery(errorMessage){

if errorMessage.equaltIgnoreCase("unexpected confirmation")
if browser.getConfirmation.equal("Session expired. Do you want to re-login?")

//Other if blocks; one each for every unexpected event


For those who are working with drivers other than Java they should look up error handling in that language. The idea remains the same.


Test Engg said...

Is it possible for selenium to wait for an user input if an unexpected event occurs in the script(like a message box/ new window/ warning / alert or any other interruption)? For example if some new functionality is added to the code and when selenium script is run, the test shouldn't break but should wait for manual intervention where the condition in the new functionality occurs. The expectation is that any new functionality added to the existing code(by developers) does not break our automation scripts

Rakesh Kumar said...

From the above example, we can handle unexpected pop-up, but after handling how we can trigger the next steps of the testscript

jowdjbrown said...

So far the company has shipped about 75 systems. projector installation toronto

Shruti Ramalingam said...

Thanks for the great information in your blog Selenium Training in Chennai

annrajkamal said...

Hi Instead of writing the scenario in each try catch ,how can we write the recovery scenario ?