2013-03-12 66 views
2

我試圖用geb替換一些自定義java硒擴展名。當我嘗試利用雲中的網格時(即SauceLabs),我碰到了一堵磚牆。當我的測試完成後,最好發送更新以指示測試是否失敗或成功。爲了利用這個,我需要RemoteWebDriver實例的sessionId。這可以在自定義Reporter中獲得,但我無法確定此接口的成功。因爲我延長GebReportingSpec,我試圖創建自己的定製版本,其中有一個自定義Junit的規則以跟蹤成功或失敗:Geb Reporter /擴展檢索測試狀態

public class TestSuccess extends TestWatcher { 
    boolean success; 
    String message; 

    @Override 
    protected void starting(Description d) { 
    message = d.getMethodName(); 
    } 

    @Override 
    protected void succeeded(final Description description) { 
    System.out.println("Test Success [succeeded] " + description); 
    this.success = true; 
    } 

    @Override 
    protected void failed(final Throwable e, final Description description) { 
    System.out.println("Test Success [failed] " + description); 
    this.success = false; 
    } 

    public boolean isSuccess() { 
    return success; 
    } 

    @Override 
    public String toString() { 
    return message + " success: <" + success + ">."; 
    } 
} 

然後我又說,我CustomReportingSpec:

class CustomReportingSpec extends GebReportingSpec { 
    /* I also tried creating this as a RuleChain with: 
    * @Rule TestRule chain = RuleChain.outerRule(
      super._gebReportingSpecTestName).around(new TestSuccess()); 
    * however, this results in a NPE. Placing the super rule in the around 
    * still results in a NPE. 
    */ 
    @Rule public TestSuccess _gebTestSuccesswatcher = new TestSuccess(); 

    // I never see this called 
    void report() { 
    System.out.println("Custom Reporting Spec: " + _gebTestSuccesswatcher + "\t") 
    super.report() 
    } 
} 

我也試圖在一個自定義的記者設置:

public CustomReporter extends ScreenshotAndPageSourceReporter implements Reporter { 
    @Rule 
    public TestSuccess _gebTestSuccesswatcher = new TestSuccess(); 

    @Override 
    public void writeReport(Browser browser, String label, File outputDir) { 
    System.out.println("Custom Reporter: " + _gebTestSuccesswatcher); 
    super.writeReport(browser, label, outputDir) 
    } 
} 

但是,無論我的測試是否失敗,成功的方法上笏似乎被稱爲cher。這裏是我的樣品測試:

class OneOff extends CustomReportingSpec { 
    def "Check One off"() { 
    when: 
     go "http://www.google.com" 
    then: 
     1 == 2 
    } 
} 

和輸出:

Custom Reporter: null success: <false>. 
Test Success [succeeded] Check One off(OneOff) 

正如你所看到的成功的方法,這種失敗的測試完成時被調用。如果我修改測試通過(即1 == 1),這裏是我的輸出:

Custom Reporter: null success: <false>. 
Test Success [succeeded] Check One off(OneOff) 

有什麼辦法,我得到這個規則在定製記者正常工作?或者有沒有辦法讓擴展中的瀏覽器實例?我遵循this guide創建自定義註釋和偵聽器,但我無法訪問瀏覽器對象。我試圖在瀏覽器的聲明中添加一個@Shared,但它並沒有在Geb配置文件中使用。

+0

如果你需要的只是會話ID,你不能通過'browser.driver.sessionId'(從規範中)得到嗎? – 2013-03-12 21:14:13

+0

儘管如此,我還是想避免在每個功能測試結束時調用一個方法來更新測試,並且在出現故障時自動更新會很好 - 這是我真正關心的功能。 – Scott 2013-03-12 21:15:06

+0

你可以編寫一個基類,或一個註冊攔截器的Spock擴展,或者一個'org.junit.rules.MethodRule'。他們每個人都可以訪問spec實例,因此也可以訪問會話ID。 – 2013-03-12 21:21:14

回答

2

由於Spock的TestRule支持中的已知限制,您的TestSuccess類無法正常工作。由於Spock和JUnit的測試執行模型之間存在細微的差異,即使測試失敗,從TestRule調用base.evaluate()也不會在Spock中引發異常。在很多情況下,這不會有什麼區別,但對於TestWatcher它會。

這是Spock規則支持中唯一已知的限制,希望我們能找到一種方法在某些時候克服它。當使用MethodRule時,不存在這種語義不匹配。

如果你想通過JUnit規則(我認爲很好)來實現你的需求,MethodRule可能是更好的選擇。與TestRule相反,MethodRule提供對測試實例的訪問權限,這將允許您使用browser.driver.sessionId獲取會話ID。