2016-06-14 19 views
3

我正在自動化一個場景,其中登錄到應用程序,單擊適當的頁面並註銷。如何決定何時在硒webdriver中使用try catch塊

在這裏,如何估計何時何地嘗試捕獲需要使用?

+2

這取決於你的代碼,當你調用任何方法,只要該方法會拋出異常,你需要抓住他們...... –

回答

0

這取決於具體情況。

1)對於要處理錯誤(如「FileOutputStream中」個案會拋出「FileNotFoundException異常」

try{FileOutputStream out = new FileOutputStream(
        new File(System.getProperty("user.dir") + "\\needful\\output.xlsx")); 
      workbook.write(out); 
      workbook.close(); 
      out.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

如果你希望你的代碼運行,即使當某些功能失敗2)。 (如審覈認定)

Assert.assertEquals(21, 25); //here the code execution stops 

//here the code fails yet the script will continue its execution. 
try{ 
Assert.assertEquals(21, 25); 
}catch(Exception e){ 
e.printStackTrace(); 
} 

3)如果你想加入「終於{}」功能,使「最後條款中的代碼就一定會執行,即便異常是從內引發嘗試或捕捉塊「。 - Link

public void open_File(){ 
     FileReader reader = null; 
     try { 
      reader = new FileReader("file\path\"); 
      //do something here 
     } catch (IOException e) { 
      //do something clever with the exception 
     } finally { 
      if(reader != null){ 
       try { 
        reader.close(); 
       } catch (IOException e) { 
        //do something clever with the exception 
       } 
      } 
      System.out.println("--- File Process Ended ---"); 
     } 
    } 
+0

對不起,沒明白編輯3點 – user3921855

+0

第三點現在檢查。 – selva

+0

謝謝你會試試這個 – user3921855