• Cucumber Scenario Outline Example

    In our previous post, we learned how we can start creating Cucumber for our test. In this post, we will learn what is cucumber scenario outline and an example on how it works.

    Cucumber Scenario Outline in Gherkin

    Based from Gherkin Reference, the Scenario Outline keyword can be used to repeat the same steps with different values or arguments being passed to the step definitions. This is helpful if you want to test multiple arguments in the same scenario. For example, in our last post, we have a Palindrome string example that verifies if the string being passed is a Palindrome or not:

    Scenario: Valid Palindrome
        Given I entered string "Refer"
        When I test it for Palindrome
        Then the result should be "true"
    
    Scenario: Invalid Palindrome
        Given I entered string "Coin"
        When I test it for Palindrome
        Then the result should be "false"
    

    We can repeatedly do something like above and check for each words that we want to test, but that will make our hard to maintain with a lot of repetitions. Instead, we can use scenario outline to add different inputs or arguments to the same scenario. We can re-write like this:

      Scenario Outline: Check if String is Palindrome
        Given I entered word <wordToTest>
        When I test it for Palindrome
        Then the output should be <output>
        Examples:
          | wordToTest | output  |
          | "Refer"    | "true"  |
          | "Coin"     | "false" |
          | "Space"    | "false" |
          | "racecar"  | "true"  |
    
    

    As you can see from above, instead of keyword Scenario, we use Scenario Outline. And we use <column-name-here> inside the Given statement and Then statement to determine their values. We also add the Examples table. This is a tabular format of data that contains the values that will be passed to the Scenario. The scenario will run for each row of the Example table. For example, when Cucumber starts to run this program, first, it will use the word “Refer” to check for palindrome and the output should be “true”. Next, it will run the same scenario, but using the word “Coin” and output “false”. The scenario will run for all the rows of the table.

    Below is the code in Step Definition that we have added in our existing class:

    @Given("I entered word {word}")
    public void iEnteredStringWord(String word) {
        testPalindrome = word;
    }
    
    @Then("the output should be {string}")
    public void theOutputShouldBeResult(String output) {
        theResultShouldBe(output);
    }
    

    Here’s the complete feature file that we have used:

    Feature: Determine if String is Palindrome or not. A string is a palindrome if it reads
      the same backwards as forwards.
    
      Scenario: Valid Palindrome
        Given I entered string "Refer"
        When I test it for Palindrome
        Then the result should be "true"
    
      Scenario: Invalid Palindrome
        Given I entered string "Coin"
        When I test it for Palindrome
        Then the result should be "false"
    
      Scenario Outline: Check if String is Palindrome
        Given I entered word <wordToTest>
        When I test it for Palindrome
        Then the output should be <output>
        Examples:
          | wordToTest | output  |
          | "Refer"    | "true"  |
          | "Coin"     | "false" |
          | "Space"    | "false" |
          | "racecar"  | "true"  |
    
    

    And here’s our complete PalindromeStepDef.java file:

    public class PalindromeStepDef {
    
        private String testPalindrome;
        private boolean isPalindrome;
    
        @Given("I entered string {string}")
        public void iEnteredString(String toTest) {
            testPalindrome = toTest;
        }
    
        @When("I test it for Palindrome")
        public void iTestItForPalindrome() {
            isPalindrome = testPalindrome.equalsIgnoreCase(new StringBuilder(testPalindrome).reverse().toString());
        }
    
        @Then("the result should be {string}")
        public void theResultShouldBe(String result) {
            boolean expectedResult = Boolean.parseBoolean(result);
            if (expectedResult) {
                Assert.assertTrue(isPalindrome);
            } else {
                Assert.assertFalse(isPalindrome);
            }
        }
    
        @Given("I entered word {word}")
        public void iEnteredStringWord(String word) {
            testPalindrome = word;
        }
    
        @Then("the output should be {string}")
        public void theOutputShouldBeResult(String output) {
            theResultShouldBe(output);
        }
    }
    

    Below is the output in Console:

    scenario outline output

    Next post is about creating Cucumber Data Tables in Java. You can download the source code here

    Related Post