Warm tip: This article is reproduced from stackoverflow.com, please click
cucumber cucumber-java cucumber-jvm gherkin cucumber-junit

How do I define "I Want" steps in cucumber using Java?

发布于 2020-05-02 21:00:42

How do I define the "I Want" steps from my feature using java?

I have my cucumber project setup like this:

Login.feature

Feature: User Login
    I want to test on "www.google.com"

Scenario: Successfully log in 
    Given I am logged out
    When I send a GET request to "/login"
    Then the response status should be "200"

Then I have my steps defined like this:

Steps.java

import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;

public class Steps {
    @Given("^I am logged out$")
    public void i_am_logged_out() {
        //do stuff
    }

    @When("^I send a GET request to \"([^\"]*)\"$")
    public void i_send_a_GET_request_to(String arg1) {
        //do stuff
    }

    @Then("^the response status should be \"([^\"]*)\"$")
    public void the_response_status_should_be(String arg1) {
        //do stuff
    }
}

How do I define the "I want" step in Java using cucumber-jvm?

Here's my attempt, but @When is not a valid annotation.

@Want("to test on \"([^\"]*)\"$")
public void to_test_on(String arg1) {
    //do stuff
}
Questioner
Katie
Viewed
23
Grasshopper 2016-09-02 21:49

The "I want to test......." is not in a correct location to be considered a valid step. Cucumber considers it to be a description of the feature and does nothing with it.. If you want initial common steps across scenarios you should add a 'Background'.

Just add a "@Given" annotation instead in front of that step.

Background:
    @Given I want to test on "www.google.com"

Else to run for only one scenario stick it along with the other steps.