Skip to main content

Data Table in Cucumber

Data table in cucumber are used to handle large amount of data. Data tables can be used in many different ways because it provide many different method to use.

Data tables are powerful but now most intuitive as you either need to deal with a list of maps or a map of lists. First row of data table is always the header row, to specify header of each column. All other rows are data rows.

In step definition we use List, Map or any other java collection to receive data from data tables. You should replace the DataTable dataTable argument with any of:

  • List<E>
  • List<List<E>>
  • List<Map<K,V>>
  • Map<K,V>
  • Map<K, List<V>>

It also tells us that each type, E, K, V must be of any of these types:

  • String
  • Integer
  • Float
  • Double
  • ByteShort
  • Long
  • BigInteger
  • BigDecimal

Consider an example of registration functionality, where you have so many fields to enter. An inefficient way of doing this is to write step for each data entry. To manage such chunk of input better way is to use data tables. Using data tables we can provide set of input together.

Feature File

 @datatable
  Scenario: Verify Registration Functionlality
    When I fill in "input[id='email_create']" with "goswami.tarun77+77@gmail.com"
    And I click on "button[id='SubmitCreate']"
    Then I should see heading "Create an account"
    When I enter following details
      | Title      | Mr.     |
      | First Name | Tarun   |
      | Last Name  | Goswami |
      | Password   |  123456 |

Step Definition For Data Table

	@When("I enter following details")
	public void i_enter_following_details(DataTable dt) 
	{
		List<List<String>> data = dt.asLists();
		System.out.println(data.get(0).get(0));
		System.out.println(data.get(0).get(1));
		System.out.println(data.get(1).get(0));
		System.out.println(data.get(1).get(1));
		System.out.println(data.get(2).get(0));
		System.out.println(data.get(2).get(1));
		System.out.println(data.get(3).get(0));
		System.out.println(data.get(3).get(1));
		
		if(data.get(0).get(1).equals("Mr."))
			driver.findElement(By.cssSelector("input[id='id_gender1']")).click();
		else
			driver.findElement(By.cssSelector("input[id='id_gender2']")).click();	
			
		driver.findElement(By.cssSelector("input[id='customer_firstname']")).sendKeys(data.get(1).get(1));
		driver.findElement(By.cssSelector("input[id='customer_lastname']")).sendKeys(data.get(2).get(1));
		driver.findElement(By.cssSelector("input[id='passwd']")).sendKeys(data.get(3).get(1));
	}

Here, we have used the asList() method of the Cucumber DataTable API to convert the Data Table into a List of List of Strings. 

Data Table and Scenario Outlines are Different

  • In Scenario Outline we use keyword Scenario Outline and Examples. There is no such keyword in Data Table.
  • Scenario outline steps execute multiple times for all the examples but Data Table execute only once.

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.