我正在尝试通过利用geb替换一些自定义的 java selenium 扩展。当我尝试在云中使用网格(即SauceLabs)时,我遇到了一些障碍。当我的测试完成时,最好发回更新以指示测试是失败还是成功。要利用它,我需要 RemoteWebDriver 实例中的 sessionId。这可以在自定义Reporter中获得,但是我无法确定此界面是否成功。由于我正在扩展GebReportingSpec,因此我尝试创建自己的自定义版本,该版本具有自定义 Junit 规则来跟踪成功或失败:
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)
}
}
但是,无论我的测试是否失败,似乎都会调用观察者上的成功方法。这是我的示例测试:
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)
我有什么方法可以让这条规则在自定义报告器中正常工作?或者有没有办法在扩展中获取浏览器实例?我已按照本指南创建自定义注释和侦听器,但我无法访问 Browser 对象。我试图在浏览器的声明中添加一个@Shared,但它并没有在 Geb 配置中拉取那个。