Wednesday 3 October 2012

Error Handling In Selenium RC

With help of error handling you can able to get details of failure.
If any of the steps in your tests fails then selenium will throw an exception and tests would stop. If you handle the exceptions using 'try catch', then you should be able to achieve what you are looking for. 

As an example, see the code below. This would handle the Element Not Found Error.And you can write that exception to file




public void test() throws Exception {
try {
selenium.open("http://seleniumhq.org/");
selenium.click("xpath=(//button[text()='Login'])[2]");
}catch (SeleniumException e){  //If any error occurs

try{
//Code to write error in file
FileWriter fstream = new FileWriter("e:/filename1.txt",true); //Will
                                           write error details here, if file doesn't exists it will create new    

BufferedWriter bw = new BufferedWriter(fstream);
bw.write("Test Project " + e.getMessage());
bw.newLine();
bw.close();
}catch(Exception a){  //For File handling Error
System.out.println("Error In File Handling");  
}
    }
}


Output
Test Project ERROR: Element xpath=(//button[text()='Login'])[2] not found