我正在尝试使用 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)
有什么方法可以让此规则在自定义报告器中正常工作吗?或者有没有办法在扩展中获取浏览器实例?我关注了this guide创建自定义注释和监听器,但我无法访问浏览器对象。我尝试将 @Shared 添加到浏览器的声明中,但它没有在 Geb 配置中提取该 @Shared。
最佳答案
由于 Spock TestRule
支持中的已知限制,您的 TestSuccess
类无法正常工作。由于 Spock 和 JUnit 的测试执行模型之间存在细微差别,从 TestRule
调用 base.evaluate()
不会在 Spock 中引发异常,即使测试失败也是如此。在许多情况下,这不会产生任何影响,但对于 TestWatcher
来说却会产生影响。
这是 Spock 规则支持中唯一已知的限制,希望我们能在某个时候找到克服它的方法。使用MethodRule
时不存在这种语义不匹配。
如果您想在 JUnit 规则的帮助下实现您的需求(我认为这很好),MethodRule
无论如何可能是更好的选择。与 TestRule
相反,MethodRule
提供对测试实例的访问,这将允许您使用 browser.driver.sessionId
获取 session ID。
关于groovy - 用于检索测试状态的 Geb 报告器/扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372076/