Scenario Outline and Examples in BDD Cucumber

What is Scenario Outline and Example in Cucumber BDD Frameworks

Data Driven testing is very genuine expectations from any automation framework. To perform data driven testing in BDD framework we need to use scenario outline and examples. To help automation community, we created this tutorial.

In this tutorial we will learn about Scenario Outline and Examples. Scenario Outline gives us flexibility to perform data driven testing in BDD Framework. In simpler words it allows us to perform same test over multiple data.

This tutorial explains about what is Scenario Outline and example, why we use and how to write code. Also each step is explained in detail by snapshot.

Scenario Outline: Scenario Outline is Gherkin Keyword which is used in feature file writing. Scenario Outline is used to perform same scenario testing on different data set. Scenario Outline must have “Examples” keyword and in example we pass test data.

Sample feature File:

Feature: Test Login feature with multiple credentials
  @AB1
  Scenario Outline:TestCase:"<TCID>"Test Newtour website with Multiple credentials
    Given Open the browser and open application
    When user enter "<username>" and "<password>"
    Then user should be successfully login into Application
    Examples:
      | TCID | username | password |
      | TC1  | mercury  | mercury  |
      | TC2  | mercury  | mercury  |
      | TC3  | mercury | mercury  |
      | TC4  | mercury  | mercury |

 In above feature file login scenario performed for four users (mercury, mercury1, mercury2 and mercury3). In examples we passed user details ie username and password.

Prerequisite : For this tutorial, you have to do following set up

  1. Java  and IDE should be set up on machine (If Java not installed then click on this link)
  2. Cucumber BDD Project should be set up.( To create first Cucumber project click on this link)

Step 1: Go to feature folder, create new feature file and write scenario with scenario outline.

Feature: Test Login feature with multiple credentials
  @AB1
  Scenario Outline:TestCase:"<TCID>"Test Newtour website with Multiple credentials
    Given Open the browser and open application
    When user enter "<username>" and "<password>"
    Then user should be successfully login into Application
    Examples:
      | TCID | username | password |
      | TC1  | mercury  | mercury  |
      | TC2  | mercury  | mercury  |
      | TC3  | mercury | mercury  |
      | TC4  | mercury  | mercury |

Step 2: Write test data and Examples and map steps variable using “<username>” and “<password>”.

Step 3: Implement scenario steps in stepdefination file

package stepDef;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class stepDefs {
    public WebDriver driver=null;
 @Given("^Open the browser and open application$")
 public void OpenBrowser(){
     WebDriverManager.chromedriver().version("83.0.4103.61").setup();
     driver = new ChromeDriver();
     driver.get("http://demo.guru99.com/test/newtours/index.php");
     driver.manage().window().maximize();
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 }
    @When("^enter the Username and Password$")
    public void enter_the_Username_and_Password() throws Throwable {
        driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("mercury");
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("mercury");
        driver.findElement(By.xpath("//input[@name='login']")).click();
    }
    @When("^user enter \"(.*)\" and \"(.*)\"$")
    public void userEnterUsernameAndPassword(String username, String password) throws Throwable {
        driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(username);
       driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password);
        driver.findElement(By.xpath("//input[@name='login']")).click();
    }
    @Then("^user should be successfully login into Application$")
    public void user_should_be_successfully_login_into_Application() throws Throwable {
        Assert.assertEquals("Login: Mercury Tours",driver.getTitle());
        driver.close();
    }
}

Step 4: Open TestRunner Class and pass scenario tag and run.

tags={"@AB1"},
this is the snapshot of Runner class. Runner class is used to run BDD automation framework.
BDD – Cucumber Test Runner

Complete Runner Class Code:

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"src/test/resources/features"},
        glue = {"stepDef"},
        tags={"@AB1"},
      plugin = {"json:target/cucumber.json"})
public class TestRunner {
}
 

Step 5: Run runner class and verify result.

Scenario Outline used to perform same set of test on different data. This is test execution status.
Console view
 
Masterthought is most popoular automation report which is widely used in automation industry.
Master Thought Cucumber Report of Scenario Outline Test

Hope you have followed above steps and successfully implemented scenario outline and examples in your BDD framework. If you face any issue then feel free to write us query@thoughtcoders.com.

0 0 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact