Search code examples
selenium-webdrivercucumbercucumber-jvm

Cucumber-jvm shared Driver example


Do somebody have a sharedDriver example with cucumber-jvm? SharedDriver has implemented as described by Ashlak, but how can I instantiate the driver and share between steps, page objects?

Shared driver class:

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());
        }
    }
}

If I have a LoginPage, Registration page with steps class, how should I use this sharedDriver?

Thanks!


Solution

  • Let me answer my question.

    SharedDriver class (see above) is good, the only thing is to configure the cucumber picocontainer, use SharedDriver instead of WebDriver driver and instantiate page objects with this driver. Job will be handled by picocontainer.