Wednesday, January 13, 2016

Data Driven Testing Using Cucumber JVM

Data Driven framework can be implemented easily using cucumber-jvm. We don't need to write much code for data driven framework.

We can implement this using only mention the Scenario Outline and Examples on feature file.

So feature file should looks like below:
Feature: This is a sample feature file

Scenario Outline: This is a scenario to test datadriven test on Cucumber JVM.
Given scenario data
When executed from Runner Class
Then UserName: "<UserName>" and Password: "<Password>" shows on console.

Examples:
| UserName | Password |
| TestUser1 | TestPassword1 |
| TestUser2 | TestPassword2 |

Whenever our glue code or step file start to execute for this scenario, code automatically repeat the execution for each set of test data. Here our test data set count is two so our test should execute for two times.

So our step file looks like below:

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class SumFeatureTest {

@Given("^scenario data$")
public void scenarioData() throws Throwable {
System.out.println("Scenario Have Some Data");
}

@When("^executed from Runner Class$")
public void executedFromRunnerClass() throws Throwable {
System.out.println("Executed From Runner Class");
}

@Then("^UserName: \"([^\"]*)\" and Password: \"([^\"]*)\" shows on console\\.$")
public void testuserAndTestPasswordShowsOnConsole(String UserName, String Password) throws Throwable {
System.out.println("UserName : "+UserName+" Password : "+Password);
}
}
 
Our Runner class code is :

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
plugin = { 
"html:target/cucumber-html-report",
"json:target/cucumber.json", 
"pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json",
"junit:target/cucumber-results.xml",
"progress:target/cucumber-progress.txt"
        },features ={"./sample.feature"},
glue ={"com/automation/steps"},strict = true,
dryRun= false,monochrome = true, snippets= SnippetType.CAMELCASE)

public class Runner {
}