cucumber-jvm icon indicating copy to clipboard operation
cucumber-jvm copied to clipboard

Google Guice- Add on option to detect if the scenario is the last one in order to close session based dependencies

Open nir-tal-talkspace opened this issue 2 years ago • 2 comments

🤔 What's the problem you're trying to solve?

cleaning dependencies initialized by Google Guice container. when working with selenium it is "cheap" to close and open a new WebDriver for every scenario (working with the existing "io.cucumber.guice.ScenarioScoped" option), but when working with Appium it can take up 2-3 minutes for each session - in that case, we need to re-use the same session and install/uninstall the app to save this time. there is no "session" scope Cucumber Guice - and also there is no "destroy" method similar to what spring+ cucumber has. so there is no way to terminate the object in case it's used in the entire session.

the easiest thing I think will be to detect if the scenario is last and perform cleanup on the last after hook and bind the object into an instance variable in order to re-use it in all of the scenarios.

✨ What's your proposed solution?

add a boolean check to the scenario e.g

    @After
    public void closeIosDriver(Scenario scenario) {
        if (scenario.isLast()) {
            iosDriver.quit();
        }
    }

⛏ Have you considered any alternatives or workarounds?

the alternative is bad - working with static variables all over the place and cleaning them in the afterAll static hook which is not available for Guice container

📚 Any additional context?

using the latest cucumber + Guice + Cucumber Guice dependencies.


This text was originally generated from a template, then edited by hand. You can modify the template here.

nir-tal-talkspace avatar May 25 '22 08:05 nir-tal-talkspace

I appreciate the clear problem description!

Unfortunately I don't expect this will be possible in the short term. Cucumber itself currently has a lot of internal state (through thread locals) that would make passing a session object around impractical. There is some refactoring going on to clean that up but it is slow going.

The JUnit team also has plans to introduce session scoped resources so it would be nice if Cucumber could use those too. See:

https://github.com/junit-team/junit5/issues/2816

So for now I do think it might be worth looking at a more sophisticated work around.

there is no "session" scope Cucumber Guice - and also there is no "destroy" method similar to what spring+ cucumber has.

So Spring keeps the application context running and only shuts it down when the JVM exits. It is kept in a static cache. You don't really see this because there is a nice framework wrapped around it.

the alternative is bad - working with static variables all over the place and cleaning them in the afterAll static hook which is not available for Guice container

You may want to consider putting all session variables in class and keeping a static reference to an instance of that class. Then inject that instance as needed with Guice and clean it up with a shutdown hook or fter all hook.

It's admittedly not as pretty as it can be, but I don't see a short term feasible solution otherwise.

mpkorstanje avatar May 25 '22 09:05 mpkorstanje

Use your Appium session(s) inside a static ThreadLocal variable (works perfect when parallelism is enabled) and close it/them within an @AfterAll hook.

fslev avatar Aug 01 '22 20:08 fslev