Cucumber-jvm共享驱动程序示例

时间:2017-12-11 17:07:09

标签: selenium-webdriver cucumber cucumber-jvm

有人和cucumber-jvm有一个sharedDriver示例吗? SharedDriver已经实现了Ashlak的描述,但是如何实例化驱动程序并在页面对象之间共享?

共享驱动程序类:

public class SharedDriver extends EventFiringWebDriver {
    private static final WebDriver REAL_DRIVER = WebDriverFactory.create();

    private static final Thread CLOSE_THREAD = new Thread() {
        @Override
        public void run() {
            REAL_DRIVER.quit();
        }
    };

    static {
        Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
    }

    public SharedDriver() {
        super(REAL_DRIVER);
    }

    @Override
    public void quit() {
        if (Thread.currentThread() != CLOSE_THREAD) {
            throw new UnsupportedOperationException("You shouldn't quit this WebDriver. It's shared and will quit when the JVM exits.");
        }
        super.quit();
    }

    @Before
    public void deleteAllCookies() {
        manage().deleteAllCookies();
    }

    @After
    public void embedScreenshot(Scenario scenario) {
        try {
            byte[] screenshot = getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png");
        } catch (WebDriverException somePlatformsDontSupportScreenshots) {
            System.err.println(somePlatformsDontSupportScreenshots.getMessage());
        }
    }
}

如果我有一个LoginPage,注册页面有步骤类,我应该如何使用这个sharedDriver?

谢谢!

1 个答案:

答案 0 :(得分:1)

让我回答我的问题。

SharedDriver类(见上文)是好的,唯一的是配置黄瓜picocontainer,使用SharedDriver而不是WebDriver驱动程序并使用此驱动程序实例化页面对象。工作将由picocontainer处理。