Cucumber--@CucumberOptions


There are lot of attributes that are available for @CucumberOptions. I will be sharing some of them in this blog post.

Sample Feature file:

Feature: Life of IT Tester

Scenario Outline: I am a tester

Given I am a "<TesterType>" tester
When I go to work
Then I "<Work_Output>" it
And my boss "<Boss_Action>" me
But the developer "<Developer_Reaction>" me
Examples:
|  TesterType  |  Work_Output | Boss_Action | Developer_Reaction |
|   Good       |   Complete   |  Salutes    |   hates            |
|   Bad        |   Mess       |  Fires      |   Likes            |

|   avg        |   sufficient |  encourages |   respects         |


1. Strict: (true|false)

Just develop the Runner class and a feature file.

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(strict=true)
public class Cucumberrunner {

}
output:
Junit test will fail with cucumber.api.PendingException and  the methods that need to be implemented will be displayed in the console.

@RunWith(Cucumber.class)
@CucumberOptions(strict=false)
public class Cucumberrunner {

}
output:
Junit test will pass and  the methods that need to be implemented will be displayed in the console.


2. Plugin: (pretty)

Prints the Gherkin source with additional colors and stack traces for errors.


@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty"},strict=false)
public class Cucumberrunner {

}

output:

Feature: Life of IT Tester

  Scenario Outline: I am a tester               [90m# explore/171.feature:3 [0m
    [36mGiven [0m [36mI am a "<TesterType>" tester [0m
    [36mWhen [0m [36mI go to work [0m
    [36mThen [0m [36mI "<Work_Output>" it [0m
    [36mAnd [0m [36mmy boss "<Boss_Action>" me [0m
    [36mBut [0m [36mthe developer "<Developer_Reaction>" me [0m

    Examples:

  Scenario Outline: I am a tester [90m# explore/171.feature:12 [0m
    [33mGiven [0m [33mI am a "Good" tester [0m
    [33mWhen [0m [33mI go to work [0m
    [33mThen [0m [33mI "Complete" it [0m
    [33mAnd [0m [33mmy boss "Salutes" me [0m
    [33mBut [0m [33mthe developer "hates" me [0m

  Scenario Outline: I am a tester [90m# explore/171.feature:13 [0m
    [33mGiven [0m [33mI am a "Bad" tester [0m
    [33mWhen [0m [33mI go to work [0m
    [33mThen [0m [33mI "Mess" it [0m
    [33mAnd [0m [33mmy boss "Fires" me [0m
    [33mBut [0m [33mthe developer "Likes" me [0m

  Scenario Outline: I am a tester   [90m# explore/171.feature:14 [0m
    [33mGiven [0m [33mI am a "avg" tester [0m
    [33mWhen [0m [33mI go to work [0m
    [33mThen [0m [33mI "sufficient" it [0m
    [33mAnd [0m [33mmy boss "encourages" me [0m
    [33mBut [0m [33mthe developer "respects" me [0m

3 Scenarios ( [33m3 undefined [0m)
15 Steps ( [33m15 undefined [0m)
0m0.000s


You can implement missing steps with the snippets below:

@Given("^I am a \"([^\"]*)\" tester$")
public void i_am_a_tester(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^I go to work$")
public void i_go_to_work() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I \"([^\"]*)\" it$")
public void i_it(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^my boss \"([^\"]*)\" me$")
public void my_boss_me(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^the developer \"([^\"]*)\" me$")
public void the_developer_me(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

3. monochrome (true|false):
If monochrome  set to true: The console output is more readable.

@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty"},monochrome=true,strict=false)
public class Cucumberrunner {

}

output:
Feature: Life of IT Tester

  Scenario Outline: I am a tester               # explore/171.feature:3
    Given I am a "<TesterType>" tester
    When I go to work
    Then I "<Work_Output>" it
    And my boss "<Boss_Action>" me
    But the developer "<Developer_Reaction>" me

    Examples:

  Scenario Outline: I am a tester # explore/171.feature:12
    Given I am a "Good" tester
    When I go to work
    Then I "Complete" it
    And my boss "Salutes" me
    But the developer "hates" me

  Scenario Outline: I am a tester # explore/171.feature:13
    Given I am a "Bad" tester
    When I go to work
    Then I "Mess" it
    And my boss "Fires" me
    But the developer "Likes" me

  Scenario Outline: I am a tester   # explore/171.feature:14
    Given I am a "avg" tester
    When I go to work
    Then I "sufficient" it
    And my boss "encourages" me
    But the developer "respects" me

3 Scenarios (3 undefined)
15 Steps (15 undefined)
0m0.000s

4. Snippet style: Cucumber will generate the code snippets(methods) by default with underscore. The Snippet format can be changed to either camelcase or underscore.

@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty"},monochrome=false,strict=false,snippets=SnippetType.CAMELCASE)
public class Cucumberrunner {

}

output:

Unimplemented method names printed to the console are as follows:

You can implement missing steps with the snippets below:

@Given("^I am a \"([^\"]*)\" tester$")
public void iAmATester(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^i go to work$")
public void iGoToWork() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^i \"([^\"]*)\" it$")
public void iIt(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^my boss \"([^\"]*)\" me$")
public void myBossMe(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^the developer \"([^\"]*)\" me$")
public void theDeveloperMe(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty"},monochrome=true,strict=false,snippets=SnippetType.UNDERSCORE)
public class Cucumberrunner {

}

output:
You can implement missing steps with the snippets below:

@Given("^I am a \"([^\"]*)\" tester$")
public void i_am_a_tester(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^i go to work$")
public void i_go_to_work() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^i \"([^\"]*)\" it$")
public void i_it(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^my boss \"([^\"]*)\" me$")
public void my_boss_me(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^the developer \"([^\"]*)\" me$")
public void the_developer_me(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

Comments

Popular posts from this blog

Cypress Cheat Sheet

Selenium WebDriver-Working with Listbox and Multi-Select Listbox

Selenium WebDriver -Handling JavaScript alerts