异常处理及截图
上一篇 /
下一篇 2015-11-05 16:30:09
/ 个人分类:Selenium
http://www.yiibai.com/selenium/selenium_exception_handling.html
异常处理
当我们正在开发测试中,我们要确保,即使测试失败的脚本可以继续执行。如果最坏的情况都处理不好意外的异常会被抛出。
如果发生异常,由于无法找到元素,或者预期的结果不与实际值相符,我们应该抓住这个异常并结束测试的逻辑方式,以防脚本本身突然终止。
语法
实际的代码应该放在try块和异常后的动作应该放在catch块。请注意:“finally'块就算没有问题,不管脚本是否已经被抛出的异常都会执行。
try{//Perform. Action}catch(ExceptionType1exp1){//Catch block 1}catch(ExceptionType2exp2){//Catch block 2}catch(ExceptionType3exp3){//Catch block 3}finally{//The finally block always executes.}
示例
如果没有找到(因为任何好的理由)元素,我们应该确保走出的功能顺利。所以,总是需要有try-catch块,如果想要的跟做的是一样的。
publicstaticWebElementlnk_percent_calc(WebDriverdriver)throwsException{try{element=driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a"));returnelement;}catch(Exceptione1){// Add a message to your Log File to capture the errorLogger.error("Link is not found.");// Take a screenshot which will be helpful for analysis.Filescreenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(screenshot,newFile("D:\\framework\\screenshots.jpg"));throw(e1);}}
收藏
举报
TAG: