2

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
}
Katie
  • 45,622
  • 19
  • 93
  • 125

4 Answers4

2

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.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
1

You can also do this:

Feature: User login

Scenario: Successfully log in

Given I want to test on "www.google.com"
When I am logged out
Then I send a GET request to "/login"
And the response status should be "200"
Ranjith's
  • 4,508
  • 5
  • 24
  • 40
1

"I want" is not a step in the scenario, it is part of the narrative overview of the scenario or story.

Narrative: In my (role) I want (feature) to realise (benefit)

The feature should includes a number of scenarios which are made up of steps.

I suggest you take a look at "Imperative vs declarative BDD" and "ubiquitous language" in BDD. In general you should be aiming for ubiquitous (universal rather than technical) and declarative language when writing BDD.

Given I am logged out - Declarative style in ubiquitous language
When I send a GET request to "/login" - Imperative and geek domain language.
Then the response status should be "200" - Imperative and geek domain language.

In an ubiquitous language

Given I am logged out
When I log in
Then the response is logged in

Even better, universal third person language

Given an existing customer
When the customer authenticates 
Then the search page is shown

See also : http://grammarist.com/spelling/log-in-login/

Martin Spamer
  • 5,437
  • 28
  • 44
1

Feature File:

Background:

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

Step Def Class:

@Given("I want to test on (.*)")

public void I_want_to_test_on(String arg1)

{

  //Here you can write your code..driver.get(arg1);

}
Kiran Yadav
  • 46
  • 1
  • 5