At the point when we are creating tests, we ought to guarantee that the contents can proceed with their execution regardless of whether the test falls flat. A sudden special case would be tossed if the most pessimistic scenario situations are not dealt with appropriately.
In the event that a special case happens because of a component not found or if the normal outcome doesn't coordinate with actuals, we should get that exemption and end the test in a legitimate manner as opposed to ending the content unexpectedly.
Syntax
The genuine code ought to be put in the attempt square and the activity after special case ought to be set in the catch square. Note that the 'at last' square executes whether or not the content had tossed a special case or NOT.
try {
//Perform Action
} catch(ExceptionType1 exp1) {
//Catch block 1
} catch(ExceptionType2 exp2) {
//Catch block 2
} catch(ExceptionType3 exp3) {
//Catch block 3
} finally {
//The finally block always executes.
}
Example
On the off chance that a component isn't found (because of some explanation), we should step out of the capacity easily. So we generally need to have an attempt get square in the event that we need to exit easily from a capacity.
public static WebElement lnk_percent_calc(WebDriver driver)throws Exception {
try {
element = driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a"));
return element;
} catch (Exception e1) {
// Add a message to your Log File to capture the error
Logger.error("Link is not found.");
// Take a screenshot which will be helpful for analysis.
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("D:\\framework\\screenshots.jpg"));
throw(e1);
}
}
