2016-01-22 105 views
0

請問我究竟做錯了什麼。AssertTrue in try/catch

我檢查過,但都沒有效果。 我也檢查了以前的代碼,但我沒有得到一個錯誤,所以我的代碼工作正常,但只是輕微的錯誤的地方。

代碼運行良好,並預期assertTrue的表現,但是當我把它放在try/catch,我只得到日誌中的catch塊,甚至當文字被發現。

我相信,如果assertTrue找到文本,它應該轉到try塊的下一行代碼,並通過測試而不是catch塊。不要誤解我的意思,我沒有收到任何錯誤,只是它打印出錯誤的信息。

以下代碼包括控制檯中的打印輸出消息。

public boolean verifyTextPresent(String value) throws Exception { 

    Thread.sleep(5000);  
    try{ 
     boolean txtFound = driver.getPageSource().contains(value); 
     log.log(value + " : text Found, .......continue"); 
     return txtFound;   
    }catch(Exception e) 
    { 
     log.log(value + " :NOT Found, check element again ot Contact developer."); 
     return false; 
    } 
} 

public static void verifySignOutBtn() throws Exception 
{ 
    log.header("VERIFY IF SIGN_OUT EXIST AND CLICKABLE........."); 

    callMethod.myAccountPageNative(CONSTANTElements.SIGN_IN_LINK); 
    Thread.sleep(2000);  

    log.header("LOCATE SIGN_OUT BTN, AND CLICK ......"); 
    callMethod.elementPresent_Click(By.cssSelector(CONSTANTElements.SIGN_OUT_BTN)); 
    Thread.sleep(4000);   

    log.header("VERIFY SIGN_OUT NAVIGATES TO HOME PAGE WHEN CLICKED......");   
    try{   
     Assert.assertTrue(callMethod.verifyTextPresent("SIGN IN"), "SIGN IN");   
     log.log("User Successfully Signed Out......."); 
     log.log("Test Passed!..."); 
     //callMethod.close(); 
    } 
    catch(Throwable e) 
    { 
     log.log("User NOT Successfully Signed Out.... Contact developer."); 
     log.log("Test Failed!..."); 
     //callMethod.close(); 
    } 
    callMethod.close(); 
} 

}

Msg in console: 
SIGN IN : text Found, .......continue 
User NOT Successfully Signed Out.... Contact developer. 
Test Failed!... 

混亂的部分是,爲什麼它打印出catch塊,而不是在try塊的下一行?

+0

爲什麼你認爲'嘗試布爾txtFound = driver.getPageSource()。contains(value); log.log(value +「:text Found,....... continue」);}'不會打印'Found'? – jhamon

+0

@jhamon .........感謝您的回覆......不要錯。 verifyTextPresent()的行爲如預期。我只添加了該方法,以便您可以看到如何編寫該方法。問題是爲什麼打印catch塊而不是verifySignOutBtn()中的try塊?請再次閱讀我的問題.... tanx – Joe

+0

好吧,有2個try/catch塊,你不坐在哪一個是有缺陷的。有了這些信息,我會說'veryTextPresent'總是返回false。編輯:LordAnomander是對的,交換'assertTrue'參數。 – jhamon

回答

1

唯一可能的解釋是,verifyTextPresent(String value)回報false(你從來沒有真正檢查boolean txtFound值)和assertTrue失敗(拋出一個AssertionError這是不是在你的catch塊處理好了)。爲了找到答案,這條線

log.log(value + " : text Found, ......." + txtFound); 

或只是打印在catch塊堆棧跟蹤替換此

log.log(value + " : text Found, .......continue"); 

例如。

+0

@Wurgspab OMG!從來沒有相信我從來沒有見過。你是該死的。 verifyTextPresent()返回false,因爲我從來沒有實際驗證它是否被發現。並且,我添加了try/catch來擺脫我正在獲取的AssertionError並捕獲Throwable。現在修改了verifyTextPresent()並且工作完美。以下是代碼。 – Joe

+0

'public boolean verifyTextPresent(String value)throws Exception { \t \t \t \t Thread.sleep(5000); \t \t \t boolean txtFound = driver.getPageSource()。contains(value); \t \t嘗試{ \t \t \t如果(txtFound =真) \t \t \t log.log(值+ 「:文本中找到,.......繼續」); \t \t \t return txtFound; \t \t \t } \t趕上(例外E) \t \t { \t \t \t log.log(值+ 「:沒有找到,再次或聯繫顯影劑檢查元素」。); \t \t \t return false; \t \t} \t \t}' – Joe

+0

只是做了對不起.... :) – Joe

2

難道不是相反嗎?

Assert.assertTrue("Message if it is false", callMethod.verifyTextPresent("SIGN IN")); 
+2

如果它是'org.junit.Assert',你肯定是對的。但是,那麼代碼如何編譯呢? –

+0

我用.......導入static org.junit.Assert。*; verifyTextPresent()在我的方法類中,而verifySignOutBtn()在我的主類中。希望這能回答你的問題。 – Joe