Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nir-tal-talkspace opened this issue May 25, 2022 · 2 comments

Comments

@nir-tal-talkspace
Copy link

nir-tal-talkspace commented May 25, 2022

🤔 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.

@mpkorstanje
Copy link
Contributor

mpkorstanje commented May 25, 2022

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:

junit-team/junit5#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.

@fslev
Copy link

fslev commented Aug 1, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants