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

Make it possible to use Scenario without a hook #2903

Open
mpkorstanje opened this issue Jul 5, 2024 · 3 comments
Open

Make it possible to use Scenario without a hook #2903

mpkorstanje opened this issue Jul 5, 2024 · 3 comments
Labels
⚡ enhancement Request for new functionality

Comments

@mpkorstanje
Copy link
Contributor

mpkorstanje commented Jul 5, 2024

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

Currently when using a Scenario to attach file or log information to a report it is necessary to use a before hook to get the scenario object:

public class Attachments {

    Scenario scenario;

    @Before
    public void before(Scenario scenario) {
        this.scenario = scenario;
    }

    @When("the string {string} is attached as {string}")
    public void theStringIsAttachedAs(String text, String contentType) {
        scenario.attach(text, contentType, null);
    }

This is quite cumbersome.

✨ What's your proposed solution?

When executing a step definition, Cucumber knows what the currently active scenario is. So we can in theory use something like

    @When("the string {string} is attached as {string}")
    public void theStringIsAttachedAs(String text, String contentType) {
        Scenario.current().attach(text, contentType, null);
    }

Or with a static import.

    @When("the string {string} is attached as {string}")
    public void theStringIsAttachedAs(String text, String contentType) {
        currentScenario().attach(text, contentType, null);
    }

Cucumber would internally use a ThreadLocal to store the current active test case. This does come with the problem that it may be invoked on a different thread by accident (e.g. when using Awaitility) but with a clear error message I reckon the worst of this can be avoided.

⛏ Have you considered any alternatives or workarounds?

No response

📚 Any additional context?

This will be required once cucumber/compatibility-kit#83 is resolved to pass the acceptance test (or some filtering to ignore these extra messages).

@mpkorstanje mpkorstanje added the ⚡ enhancement Request for new functionality label Jul 5, 2024
@mpkorstanje mpkorstanje changed the title Make it possible to use Scenario.attach without a hook Make it possible to use Scenario without a hook Jul 5, 2024
@orcunbalcilar
Copy link
Contributor

A wrapper/proxy class at runtime for the desired class ?

@mpkorstanje
Copy link
Contributor Author

I'm not sure how that would work, or provide a solution. But if you want to try something out, do have a look at io.cucumber.java.Scenario.

@marioburatto
Copy link

Hi every one. I'm also in need of this solution. I have a suggestion of how it could be.

What if the method signature could be optionally like this:

    @When("the string {string} is attached as {string}")
    public void theStringIsAttachedAs(Scenario scenario, String text, String contentType) {
        scenario.attach(text, contentType, null);
    }

So PickleStepDefinitionMatch.java could allow parameterInfos to be up to 1 argument bigger than arguments, as long as the parameterInfos[0].type were a Scenario.java.

if (parameterInfos != null && arguments.size() != parameterInfos.size()) {

In such cases, we would add the Scenario to the result list and it would be sent to the target method:

List<Object> result = new ArrayList<>();
try {
    if(expectsScenario){
        result.add(new Scenario(state));
    }
    for (Argument argument : arguments) {
        result.add(argument.getValue());
    }
}

Please let me know your thoughts.
I can submit a PR if you find this solution suitable.

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

No branches or pull requests

3 participants