0

こんにちは、selenium と spock を使用してスモーク テストを作成しています。テストの失敗時にスクリーンショットを撮りたいと思っています。これを試す:

    public class ScreenshotTestRule implements MethodRule {
    public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    statement.evaluate()
                } catch (Throwable t) {
                    captureScreenshot(frameworkMethod.getName().replaceAll(" ", "-"))
                    throw t
                }
            }

            public void captureScreenshot(String fileName) {
                try {
                    new File("target/surefire-reports/").mkdirs()
                    File  screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE)
                    Files.copy(screenshot, new File("/target/surefire-reports/"+fileName+".png"))
                } catch (Exception e) {
                    print "Error while creating screenshot " + fileName
                    throw new RuntimeException(e)
                }
            }
        }
    }
}

しかし、私はこのエラーが発生しています:

java.lang.RuntimeException: groovy.lang.MissingFieldException: No such field: driver for class: lt.inventi.apollo.system.test.SmokeTest$ScreenshotTestRule
4

1 に答える 1

0
    private class ScreenshotTestRule implements MethodRule {

    WebDriver testDriver

    public ScreenshotTestRule(WebDriver driver) {
        this.testDriver = driver
    }

    public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    statement.evaluate()
                } catch (Throwable t) {
                    captureScreenshot(frameworkMethod.getName().replaceAll(" ", "-"))
                    throw t
                }
            }

            public void captureScreenshot(String fileName) {
                try {
                    new File("/target/test-screenshots/").mkdirs()
                    File  screenshot = ((TakesScreenshot) testDriver).getScreenshotAs(OutputType.FILE)
                    String imagePath = "target/test-screenshots/" + fileName + "-screenshot.png";
                    Files.copy(screenshot, new File(imagePath))
                } catch (Exception e) {
                    print "Error while creating screenshot " + fileName
                }
            }
        }
    }
}

コンストラクターをスローした単純なパス

于 2013-07-19T07:11:09.027 に答える