Reading list Switch to dark mode

    Parametrization in Cucumber

    Updated 16 July 2021

    In our previous blog on Cucumber Introduction, we understood the basic concept of Cucumber and behavior driven testing(BDD); In this blog we will have a glance at parametrization concept in cucumber.
    The basic requirement of automated testing is to use same test again and again but with different set of data. Although, cucumber is a BDD framework but it supports the concept of Data Driven Testing

    Cucumber supports Data Driven Testing using Scenario Outline and Examples keywords. Creating a feature file with Scenario Outline and Example keywords will help to reduce the code and testing multiple scenarios with different values.

    Scenario Outline

    This keyword lets you run the same scenario for two or more different input data. It basically replaces value assigned in the variable from the input values mentioned in the Examples input data set. Each row in the input data set acts as a scenario.

    For better understanding, we can consider an example of login page, where for every different Login credentials, a scenario has to be defined, and each step will be repeated for every input data.

    For such cases, the scenarios will look something like this:

    Start your headless eCommerce
    now.
    Find out More
    Scenario:
    
    Given user navigates to Login page
    
    When I enter correct username and password
    
    Then login should be successful
    
    Scenario:
    
    Given user navigates to Login page
    
    When I enter correct UName and Password
    
    Then login should be successful
    
    Scenario:
    
    Given user navigates to Login page
    
    When I enter correct LoginID and PSWD
    
    Then login should be successful

    As we can see in the above mentioned scenarios the input parameters is changing for every scenario. That’s where the importance of scenario outline comes into picture.
    By using Scenario Outline we can specify one test scenario and at the bottom of it we can provide a number of inputs. The scenario will get executed as many number of times as the number of inputs provided.

    Examples

    All scenario outlines are followed by Examples part that contains the set of data that has to be passed during the execution of tests.

    Scenario Outline and Examples keyword together will form a set of test cases something like this.

    Scenario Outline: Test user Login with different credentials
    	Given Open Firefox and navigate to Login page
    	When valid "<username>" and "<password>" is entered
    	Then User should be logged in successfully
    
    Examples:
        | username   | password |
        | [email protected] | Test@123 |
        | [email protected]| Testtest |

    After creating feature file, create a Test Runner class and a steps to run class for further execution.

    package Login_cucumber;
    import org.junit.runner.RunWith;
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(features = {"Cases"},glue={"StepsToTest"})
    public class TestRunner {
    }

    After running the class,create a class for steps to run the actual test in which the execution flow of the test will be mentioned.

    public class LoginSteps {
        
        WebDriver driver;    
     
        @Given("^Open Firefox and navigate to Login page$")
        public void given_Open_Firefox_and_navigate_to_Login_page() throws Throwable
        {
           driver = new FirefoxDriver();
           driver.get("http://LoginPage.com");
           driver.manage().window().maximize();")
        }
          
        @When("^valid \"(.*)\" and \"(.*)\ is entered$")
        public void when_valid_username_and_password_is_entered(String username,String password) throws Throwable
        {
            driver.findElement(By.xpath(".//*[@id='login']")).sendKeys(username);
            driver.findElement(By.xpath(".//*[@id='password']")).sendKeys(password);
        }
          
        @Then("^user should logged in successfully$") throws Throwable
        public void then_user_should_logged_in_successfully()
        {
        driver.findElement(By.xpath(".//*[@id='submit']")).click();
        System.out.println("User Logged in successfully");
        }
    }

    On running this class,the login credentials i.e username and password will get filled from feature file with new values each time till the Examples has values; in current example, test will run for two times as it has two test data.
    Instead of hard coding the test data, variables are defined in the Examples section and used in Scenario Outline section.

    This was the concept of parametrization in cucumber.

    Please share your valuable feedback in the comment section below. Keep Testing!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content