Zaxxon
Zaxxon

Reputation: 182

Cucumber/Java: When steps are not being found, but Given and Then are within the same file

I have the following Scenario Outline

    @Example
    Scenario Outline: 
        Given that I have gone to the Google page
         When I add <animal> to the search box
          And click the Search Button
         Then "<term>" should be mentioned in the results
        Examples: 
            | animal | term |
            | cat    | cat  |
            | dog    | dog  |

Step definition class has

    @Given("^that I have gone to the Google page$")
    public void that_I_have_gone_to_the_Google_page()  {
        Launch.launch();
    }
    
    @When("I add {string} to the search box")
    public void i_add_to_the_search_box(String arg1) throws Throwable {
        GoogleHomePage hPage = new GoogleHomePage(driver, dataMap);
        hPage.performGoogleSearch(arg1);
    }

    @And("^click the Search Button$")
    public void click_the_Search_Button() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^\"([^\"]*)\" should be mentioned in the results")
    public void should_be_mentioned_in_the_results(String resultTerm)  {
        GoogleResultPage grPage = new GoogleResultPage(driver, dataMap);
        grPage.verifyResult(resultTerm);
    }

And the execution has

  @Example @GoogleSearch
  Scenario Outline:                               # google/searchTerm.feature:17
    Given that I have gone to the Google page     # GoogleSearchSD.that_I_have_gone_to_the_Google_page()
    When I add cat to the search box
    And click the Search Button                   # GoogleSearchSD.click_the_Search_Button()
    Then "cat" should be mentioned in the results # GoogleSearchSD.should_be_mentioned_in_the_results(String)

  @Example @GoogleSearch
  Scenario Outline:                               # google/searchTerm.feature:18
    Given that I have gone to the Google page     # GoogleSearchSD.that_I_have_gone_to_the_Google_page()
    When I add dog to the search box
    And click the Search Button                   # GoogleSearchSD.click_the_Search_Button()
    Then "dog" should be mentioned in the results # GoogleSearchSD.should_be_mentioned_in_the_results(String)

2 Scenarios (2 undefined)
8 Steps (4 skipped, 2 undefined, 2 passed)

So you see that all the steps but the When are recognized/matched up. The When statement/code isn't being executed, hence the rest of the code is being skipped.

I have copied and pasted the suggested code snipe of the missing step. Not that it helps.

Versions I have in Eclipse Plug-in Registry:


Update 7/15/2021, 5:00 PM EDT

Just to see what would happen, I changed the Scenario Outline

          And I add "<animal>" to the search box
         When click the Search Button

and the former When statement works as an And, but the previous And doesn't work as a When.

So it does seem to be the actual When statement, and not what the statement is.

>mvn dependency:list | findstr cucumber
[INFO] -------------< org.cucumber.example:org.cucumber.example >--------------
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ org.cucumber.example ---
[INFO]    com.github.mkolisnyk:cucumber-runner:jar:1.3.5:test
[INFO]    io.cucumber:html-formatter:jar:13.0.0:compile
[INFO]    io.cucumber:docstring:jar:6.10.4:compile
[INFO]    com.github.mkolisnyk:cucumber-report-generator:jar:1.3.5:test
[INFO]    io.cucumber:cucumber-plugin:jar:6.10.4:compile
[INFO]    io.cucumber:cucumber-core:jar:6.10.4:compile
[INFO]    io.cucumber:datatable:jar:3.5.0:compile
[INFO]    io.cucumber:cucumber-gherkin:jar:6.10.4:compile
[INFO]    io.cucumber:cucumber-gherkin-messages:jar:6.10.4:compile
[INFO]    io.cucumber:cucumber-testng:jar:6.10.4:compile
[INFO]    info.cukes:cucumber-jvm-deps:jar:1.0.5:test
[INFO]    io.cucumber:cucumber-junit:jar:6.10.4:compile
[INFO]    info.cukes:cucumber-java:jar:1.2.5:test
[INFO]    net.masterthought:cucumber-reporting:jar:3.8.0:compile
[INFO]    io.cucumber:cucumber-java8:jar:6.10.4:compile
[INFO]    io.cucumber:messages:jar:15.0.0:compile
[INFO]    io.cucumber:tag-expressions:jar:3.0.1:compile
[INFO]    io.cucumber:create-meta:jar:4.0.0:compile
[INFO]    info.cukes:cucumber-html:jar:0.2.3:test
[INFO]    io.cucumber:cucumber-java:jar:6.10.4:compile
[INFO]    io.cucumber:cucumber-expressions:jar:10.3.0:compile
[INFO]    info.cukes:cucumber-junit:jar:1.2.5:test
[INFO]    info.cukes:cucumber-testng:jar:1.2.5:test
[INFO]    info.cukes:cucumber-core:jar:1.2.5:test
[INFO]    io.cucumber:cucumber-jvm-deps:jar:1.0.6:provided

Upvotes: 0

Views: 528

Answers (1)

Ga&#235;l J
Ga&#235;l J

Reputation: 15050

The correct syntax for Scenario Outline is:

 When I add "<animal>" to the search box
@When("^I add \"([^\"]*)\" to the search box$")
// Or with Cucumber Expressions:
@When("I add {string} to the search box")

The important part is that the < and > are only present in the scenario file, not in the step definition. Consider that <animal> will be replaced (as in a string replacement) automatically by each value of the examples provided.

Note: you have the same mistake in the Then.

Upvotes: 1

Related Questions