-1

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!

brobee
  • 231
  • 1
  • 5
  • 25
  • Please read [ask], especially the part about [mcve] (MCVE), and [How much research effort is expected?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) This will help you debug your own programs and solve problems for yourself. If you do this and are still stuck you can come back and post your MCVE, what you tried, and the execution result including any error messages so we can better help you. Also provide a link to the page and/or the relevant HTML. – JeffC Dec 11 '17 at 18:19
  • @brobee you should have a look at dependency injection for sharing state and object creation. Picocontainer is the least intrusive and uses constructor injection. Cucmber already has readymade support for it. Refr to this http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer. If you pass the shareddriver object to the pageobject constructor it will be created and passed by pico. The same driver instance will be passed to other pageobjects with similar constructors that are called in the same sceanario. – Grasshopper Dec 11 '17 at 18:23

1 Answers1

1

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.

brobee
  • 231
  • 1
  • 5
  • 25