2012-07-23 40 views
1

我在Eclipse中使用Selenium Web Driver和JUnit。我需要創建簡單的檢查,如果頁面上存在一些文本 - 如果是,比我需要生成錯誤。我想要在JUnit失敗追蹤中顯示此錯誤。這是我的代碼:JUnit故障跟蹤中的顯示錯誤

public class Check { 
@Rule 
public ErrorCollector errCollector = new ErrorCollector(); 

@FindBy(tagName="body") 
@CacheLookup 
private WebElement titl; 
@FindBy(partialLinkText="Exception") 
@CacheLookup 
private WebElement excep; 


public void check_false(String t, String r) { 
     try { 
      String bodyText = titl.getText(); 
      Assert.assertFalse("Exception found", bodyText.contains(t)); } 
     catch (AssertionError e) { 
      System.err.println(r + ": " + e.getMessage()); 
      System.out.println(excep.getText()); 
      errCollector.addError(e); 
      }  
      } 

如果我得到的錯誤,它會顯示在Eclipse康壽,但如果測試JUnit中顯示爲無錯,並且不顯示異常消息。我如何正確檢查?

回答

0

我使用

AssertJUnit.assertFalse( 「異常發現」,bodyText.contains(T));

這是一個從http://testng.org/doc/index.html 看到http://testng.org/javadoc/org/testng/AssertJUnit.html

在Eclipse中時,我的測試失敗,在JUnit窗口,我得到了stackstrace。從未嘗試收集錯誤。這將引發

AssertionFailedError異常

如果測試失敗,但如果你抓住它,我不知道是否堆棧跟蹤將在JUnit窗口。

+1

我在此視頻培訓中發現了這種方式 - http://www.youtube.com/watch?v=KAmII54wpAY&feature=player_embedded。 – khris 2012-07-23 09:35:15

0

這可能不是你想要的,在這種情況下,我很抱歉,但你可以簡單地fail it,並提供一個可選的消息,爲什麼。

public void checkText(String actual, String expected){ 
    try{ 
    if(!expected.equals(actuals){ 
     fail("Expected : [ " expected + " ] , actual [" + actual + "]" 
    }catch(SomeOtherException soe){ 
     fail(soe.getMessage()); 
    } 
} 
+0

我使用這個,但它不是我真正的意思和需要 – khris 2012-07-23 11:25:23