prashant-ramcharan / courgette-jvm

Multiprocess | Parallel Cucumber-JVM | Parallelize your Java Cucumber tests on a feature level or on a scenario level.
MIT License
130 stars 38 forks source link

Exposing Scenario hooks #390

Closed sobi-ki closed 4 months ago

sobi-ki commented 5 months ago

Hey,

I hope you are doing well.

I have the following setup using Spring:

This calls the WebDriverConfiguration class

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WebDriverConfiguration.class)
@CucumberContextConfiguration
public class SpringCucumberTest {
}
@Configuration
@Scope(CucumberTestContext.SCOPE_CUCUMBER_GLUE)
@ComponentScan("org.xxx")
public class WebDriverConfiguration {

    @Autowired
    private WebDriverFactory webDriverFactory;

    @Scope(CucumberTestContext.SCOPE_CUCUMBER_GLUE)
    @Bean(destroyMethod = "quit")
    public WebDriver webDriver() throws MalformedURLException {
        return webDriverFactory.createWebDriver();
    }
}

createWebDriver is the setup of drivers.

I have a small challenge, and I hope you can help me out.

The current project needs to support API tests.

I have now set the Feature files with the tags: @fe run front end test @api run API test

The current limitation is that I am not able to expose the tags in the WebDriverConfiguration class.

Is there any way I can expose the cucumber tags? So that I don't need to launch the browser for the API test.

This will need to get tags from CourgetteChromeRunner, at Feature level and when running via Gradle/Maven

Thank you

prashant-ramcharan commented 5 months ago

Hello,

Have you tried using Spring ConditionalOnProperty?

This should only create the web driver bean if the tag you pass at runtime is @fe

@Configuration
@ConditionalOnProperty(name = "cucumber.tags", havingValue = "@fe")
public class WebDriverConfiguration {
}

// or

@Bean
@ConditionalOnProperty(name = "cucumber.tags", havingValue = "@fe")
public WebDriver webDriver() {
}

Pass it at runtime / or define it as a default in your app config

gradle test -Dcucumber.tags="@fe"
sobi-ki commented 5 months ago

Thank you for your response.

I do have a solution if running via gradle cmd, but the issue is when setting a tag of @fe, then I won't be able to fire the API test. The problem is that the tags are not seen when @Bean is called from CourgetteRunner class or when running test via Feature / Scenario level in Feature file.

prashant-ramcharan commented 5 months ago

Have you considered making your bean @Lazy ?

@Bean
@Lazy
public WebDriver webDriver() {
}

So only tests that require a webDriver bean will call it to launch a browser.

As your API tests doesn't need a webDriver bean, it simply won't be loaded / created and as a result, no browser will be launched.

You could also dynamically register the bean based on the Cucumber tags at runtime.

    @Before
    public void before(Scenario scenario) {
        if (scenario.getSourceTagNames().contains("fe")) {
            // register your webDriver bean here
        }
    }
sobi-ki commented 5 months ago

Thank you for your feedback. Not tried @Lazy will give it a go.