CucumberJVM: 06 - Data Driven BDD with DataTable

Using DataTables isn't really preferred, but here's how to do it

Feature File

Feature: Deal data creation

Scenario: Free CRM Create a new deal scenario

#Using a Data Table
Given user is already on Login Page
When title of login page is Free CRM
Then user enters username and password
| naveenk | test@123 |
Then user clicks on login button
Then user is on home page
Then user moves to new deal page
Then user enters contact details
| test deal | 1000 | 50 | 10 |
Then close the browser

Definition File

package stepDefinitions;

import java.util.List;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import cucumber.api.DataTable;

public class DealStepDefinition {
    WebDriver driver;
    WebElement el;
    @Given("^user is already on Login Page$")
    public void user_is_already_on_Login_Page() {
        System.setProperty("", "W:\\w\\java\\lib\\SeleniumWebDrivers\\chromedriver\\2.39\\chromedriver.exe");
        driver = new ChromeDriver();
    @When("^title of login page is Free CRM$")
    public void title_of_login_page_is_Free_CRM() {
        String title = driver.getTitle();
        Assert.assertEquals("Free CRM software in the cloud powers sales and customer service", title);
//    @Then("^user enters \"([^\"]*)\" and \"([^\"]*)\"$")
//    public void user_enters_username_and_password(String username, String password) {
//        driver.findElement(By.xpath("//input[@name='username']")).sendKeys(username);
//        driver.findElement("password")).sendKeys(password);
//    }    

    @Then("^user enters username and password$")
    public void user_enters_username_and_password(DataTable credentials){
        List<List<String>> data = credentials.raw(); //raw gives list of strings, then pipe to a List.. of List<String>
        // For automatic transformation, change DataTable to one of
        // List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
        // E,K,V must be a scalar (String, Integer, Date, enum etc)


    @Then("^user clicks on login button$")
    public void user_clicks_on_login_button() {
        // technique
        el = driver.findElement(By.xpath("//input[@type='submit']"));;
        // might have to use JavaScript technique
//        el = driver.findElement(By.xpath("//input[@type='submit']"));
//        JavascriptExecutor js = (JavascriptExecutor)driver;
//        js.executeScript("arguments[0].click", el);
    @And("^user is on home page$")
    public void user_is_on_home_page() {
        String title = driver.getTitle();
        System.out.println("HomePage title is :" + title);
        Assert.assertEquals("CRMPRO", title);

    @Then("^user moves to new deal page$")
    public void user_moves_to_new_deal_page(){
        //move to frame ( view frame source )

//        wasn't working
//        driver.switchTo().frame("manpanel");
        el = driver.findElement(By.xpath("//frame[@name='mainpanel']"));
        //mouse over the menu item
        Actions action = new Actions(driver); //new action
        // findElement.... build action to MOVE to element....

//         wasn't working
//        driver.findElement(By.xpath("//a[contains(.,'New Contact']")).click();
        // THEN click
        action.moveToElement(driver.findElement(By.xpath("//a[contains(.,'New Deal')]"))).build().perform();

        el = driver.findElement(By.xpath("//a[contains(.,'New Deal')]"));;


    @Then("^user enters contact details$")
    public void user_enters_contact_details(DataTable dealData){
        List<List<String>> list = dealData.raw();
        driver.findElement(By.xpath("//input[@type='submit' and @value='Save']")).click();

    @Then("^close the browser$")
    public void close_the_browser(){



package uc.atmtransaction.Runner;

import org.junit.runner.RunWith;

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

        features = "src/main/java/Features/deals.feature"
        ,glue= {"stepDefinitions"}
        ,format= {"pretty","html:test-output","json:json-output/cucmber.json","junit:xml-output/cucumber.xml"}

public class TestRunner {




Feature: Deal data creation
Starting ChromeDriver 2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9) on port 23094
Only local connections are allowed.
Jun 12, 2018 7:24:32 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Free CRM software in the cloud powers sales and customer service
HomePage title is :CRMPRO

  Scenario: Free CRM Create a new deal scenario # src/main/java/Features/deals.feature:3
    #Using a Data Table
    Given user is already on Login Page         # DealStepDefinition.user_is_already_on_Login_Page()
    When title of login page is Free CRM        # DealStepDefinition.title_of_login_page_is_Free_CRM()
    Then user enters username and password      # DealStepDefinition.user_enters_username_and_password(DataTable)
    Then user clicks on login button            # DealStepDefinition.user_clicks_on_login_button()
    Then user is on home page                   # DealStepDefinition.user_is_on_home_page()
    Then user moves to new deal page            # DealStepDefinition.user_moves_to_new_deal_page()
    Then user enters contact details            # DealStepDefinition.user_enters_contact_details(DataTable)
    Then close the browser                      # DealStepDefinition.close_the_browser()

1 Scenarios (1 passed)
8 Steps (8 passed)