Cucumber - Data Tables

While working on automation, we may face variety of scenarios. Each scenario carries a different meaning and needs.
Since the beginning, we have been taking an example of login functionality for a social networking site, where we just had two input parameters to be passed. Let’s think of some more possibility. How about “New user registration” functionality? Typically, what can be the input parameters while registering a new user for a social networking site? Something like the following −
User Name
Email Address
Password
Re-enter password
Birthdate
Gender
Phone number
Feature − New user registration.
Verify that the new user registration is unsuccessful after passing the incorrect inputs.
Given I am on a new user registration page.
When I enter the user name and an e-mail address as email address and password as, and re-enter password as and Birthdate as and Gender as and phone number as then the user registration should be unsuccessful.
It looks a bit messy at first glance. So, is there any better way to manage such chunk of inputs? Answer can be “Data Table”. Data table is a set of input to be provided for a single tag. This tag can be GIVEN, WHEN, or THEN.
Let’s write the above scenario with the help of data table and it will look like the following −
Given I am on a new user registration page
When I enter valid data on the page
| Fields                 | Values              |
| First Name             | Tom                 |
| Last Name              | Kenny               |
| Email Address          | someone@someone.com |
| Re-enter Email Address | someone@someone.com |
| Password               | Password1           |
| Birthdate              | 01                  |

Then the user registration should be successful.
Let’s automate an example of a data table.
Step 1 − Create a Maven Test Project named “DataTableTest”.
Go to File → New → Others → Maven → Maven Project → Next.
Go to File → New → Others → Maven → Maven Project → Next.
Provide group Id (group Id will identify your project uniquely across all projects).
Provide group Id (group Id will identify your project uniquely across all projects).
Provide artifact Id (artifact Id is the name of the jar without version. You can choose any name which is in lowercase).
Provide artifact Id (artifact Id is the name of the jar without version. You can choose any name which is in lowercase).
Click on Finish.
Click on Finish.
Open pom.xml −

Go to package explorer on the left hand side of Eclipse.
Expand the project CucumberTest.
Locate pom.xml file.
Right-click and select the option, Open with “Text Editor”.

Open pom.xml −
Go to package explorer on the left hand side of Eclipse.
Go to package explorer on the left hand side of Eclipse.
Expand the project CucumberTest.
Expand the project CucumberTest.
Locate pom.xml file.
Locate pom.xml file.
Right-click and select the option, Open with “Text Editor”.
Right-click and select the option, Open with “Text Editor”.
Add dependency for Selenium: This will indicate Maven, which Selenium jar files are to be downloaded from the central repository to the local repository.

Open pom.xml is in edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.
Inside the dependencies tag, create dependency tag. (<dependency></dependency>).
Provide the following information within the dependency tag.
Add dependency for Selenium: This will indicate Maven, which Selenium jar files are to be downloaded from the central repository to the local repository.
Open pom.xml is in edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.
Open pom.xml is in edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.
Inside the dependencies tag, create dependency tag. (<dependency></dependency>).
Inside the dependencies tag, create dependency tag. (<dependency></dependency>).
Provide the following information within the dependency tag.
Provide the following information within the dependency tag.
<dependency> 
   <groupId>org.seleniumhq.selenium</groupId> 
   <artifactId>selenium-java</artifactId> 
   <version>2.47.1</version> 
</dependency>
Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber files are to be downloaded from the central repository to the local repository.

Create one more dependency tag.
Provide the following information within the dependency tag.

Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber files are to be downloaded from the central repository to the local repository.
Create one more dependency tag.
Create one more dependency tag.
Provide the following information within the dependency tag.
Provide the following information within the dependency tag.
<dependency> 
   <groupId>info.cukes</groupId> 
   <artifactId>cucumber-java</artifactId> 
   <version>1.0.2</version> 
   <scope>test</scope> 
</dependency>
Add dependency for Cucumber-JUnit − This will indicate Maven, which Cucumber JUnit files are to be downloaded from the central repository to the local repository.

Create one more dependency tag.
Provide the following information within the dependency tag.

Add dependency for Cucumber-JUnit − This will indicate Maven, which Cucumber JUnit files are to be downloaded from the central repository to the local repository.
Create one more dependency tag.
Create one more dependency tag.
Provide the following information within the dependency tag.
Provide the following information within the dependency tag.
<dependency> 
   <groupId>info.cukes</groupId> 
   <artifactId>cucumber-junit</artifactId> 
   <version>1.0.2</version> 
   <scope>test</scope> 
</dependency>
Add dependency for JUnit − This will indicate Maven, which JUnit files are to be downloaded from the central repository to the local repository.

Create one more dependency tag.
Provide the following information within the dependency tag

Add dependency for JUnit − This will indicate Maven, which JUnit files are to be downloaded from the central repository to the local repository.
Create one more dependency tag.
Create one more dependency tag.
Provide the following information within the dependency tag
Provide the following information within the dependency tag
<dependency> 
   <groupId>junit</groupId> 
   <artifactId>junit</artifactId> 
   <version>4.10</version> 
   <scope>test</scope> 
</dependency>
Verify binaries.

Once pom.xml is edited successfully, save it.
Go to Project → Clean − It will take a few minutes.

Verify binaries.
Once pom.xml is edited successfully, save it.
Once pom.xml is edited successfully, save it.
Go to Project → Clean − It will take a few minutes.
Go to Project → Clean − It will take a few minutes.
Step 2 − Create a package named dataTable under src/test/java
Step 3 − Create a Feature file.
Create a feature file, named as dataTable.feature inside the package dataTable (see section scenario outline for more detailed steps).
Create a feature file, named as dataTable.feature inside the package dataTable (see section scenario outline for more detailed steps).
Write the following text.
Feature − Data table
Verify that the new user registration is unsuccessful after passing incorrect inputs.
Scenario:
Given I am on the new user registration page
When I enter invalid data on the page

Write the following text.
Feature − Data table
Verify that the new user registration is unsuccessful after passing incorrect inputs.
Scenario:
Given I am on the new user registration page
When I enter invalid data on the page
| Fields                 | Values              |
| First Name             | Tom                 |
| Last Name              | Kenny               |
| Email Address          | someone@someone.com |
| Re-enter Email Address | someone@someone.com |
| Password               | Password1           |
| Birthdate              | 01                  |

Then the user registration should be unsuccessful
Save the file.
Save the file.
Step 4 − Create step definition file.
Create the step definition file named as ‘dataTable.java’ inside the package dataTable (see section scenario outline for more detailed steps).
Create the step definition file named as ‘dataTable.java’ inside the package dataTable (see section scenario outline for more detailed steps).
Write the following code.
Write the following code.
package dataTable; 

import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select;

import cucumber.annotation.en.Given; 
import cucumber.annotation.en.Then; 
import cucumber.annotation.en.When; 
import cucumber.table.DataTable; 

public class stepdefinition { 
   WebDriver driver = null;
	
   @Given("^I am on new user registration page$") 
   public void goToFacebook() { 
      //Intiate web browser instance. driver = new FirefoxDriver();
      driver.navigate().to("https://www.facebook.com/"); 
   } 
	
   @When("^I enter invalid data on the page$") 
   public void enterData(DataTable table){ 
      //Initialize data table 
      List<list> data = table.raw();
      System.out.println(data.get(1).get(1)); 
      
      //Enter data
      driver.findElement(By.name("firstname")).sendKeys(data.get(1).get(1));
      driver.findElement(By.name("lastname")).sendKeys(data.get(2).get(1));
      driver.findElement(By.name("reg_email__")).sendKeys(data.get(3).get(1));     
      driver.findElement(By.name("reg_email_confirmation__")).
         sendKeys(data.get(4).get(1)); 
      driver.findElement(By.name("reg_passwd__")).sendKeys(data.get(5).get(1)); 
      
      Select dropdownB = new Select(driver.findElement(By.name("birthday_day"))); 
      dropdownB.selectByValue("15"); 
		
      Select dropdownM = new Select(driver.findElement(By.name("birthday_month")));
      dropdownM.selectByValue("6"); 
		
      Select dropdownY = new Select(driver.findElement(By.name("birthday_year")));
      dropdownY.selectByValue("1990"); 
		
      driver.findElement(By.className("_58mt")).click(); 
      // Click submit button driver.findElement(By.name("websubmit")).click(); 
   } 
	
   @Then("^User registration should be unsuccessful$") 
   public void User_registration_should_be_unsuccessful() {
      if(driver.getCurrentUrl().equalsIgnoreCase("https://www.facebook.com/")){
         System.out.println("Test Pass"); 
      } else { 
         System.out.println("Test Failed"); 
      } 
      driver.close(); 
   } 
}
Save the file.
Save the file.
Step 5 − Create a runner class file.
Create runner class named as runTest.java inside the package.
Create runner class named as runTest.java inside the package.
Write the following code.
Write the following code.
package dataTable; 

import org.junit.runner.RunWith; 
import cucumber.junit.Cucumber; 

@RunWith(Cucumber.class) 
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
 
public class runTest { }
Save the file.
Save the file.
Run the test using the option

Select runTest.java file from the package explorer.
Right-click and select the option, Run as.
Select JUnit test.

Run the test using the option
Select runTest.java file from the package explorer.
Select runTest.java file from the package explorer.
Right-click and select the option, Run as.
Right-click and select the option, Run as.
Select JUnit test.
Select JUnit test.
You may observe the following things upon successful execution.
Facebook website gets loaded.
Facebook website gets loaded.
Data will be entered on the registration page.
Data will be entered on the registration page.
Submit button will be clicked.
Submit button will be clicked.
We will see that home page will not displayed and “Test Pass” will be written on the console.
We will see that home page will not displayed and “Test Pass” will be written on the console.
 Print
 Add Notes
 Bookmark this page