Behat Tables (Data Tables)

Profile picture for user devraj

Tables in Behat are used to handle large amount of data. This is same as Data Table in Cucumber.

Don’t confuse tables with scenario outlines - From syntax you will find them identical, but they have a different purpose. Scenario Outlines declare multiple different values for the same scenario, and it iterate over example data multiple times while tables on the other hand are used to expect a set of data.

Table can be used in many different ways in real world application. We will discuss few of them, first let's consider an example of Contact Us functionality for contact page of application umami which comes bundled with acquia dev desktop:

  Scenario: Verify Registration Functionality
    Given I am on homepage
    When I follow "Contact"
    When I enter following details
      | Your name     | Your email address        | Subject      |
      | Tarun Goswami | | Test subject |

Step definitions for first 2 steps are already defined in Mink. For last step which is for Table step definition is:

 * @When I enter following details
public function iEnterFollowingDetails(TableNode $table)
    $page = $this->getSession()->getPage();
    foreach ($table as $row)
        $name = $row['Your name'];
        $email =   $row['Your email address'];
        $subject = $row['Subject'];


Here TableNode is a class and $table is it's object, if you will iterate throw it's using another variable $row and print it using var_dump you will see the output like this:

 array(3) {
      │   ["Your name"]=>
      │   string(13) "Tarun Goswami"
      │   ["Your email address"]=>
      │   string(25) ""
      │   ["Subject"]=>
      │   string(12) "Test subject"
      │ }

Here we got an associative array which is like Dictionary in another language which is used to store key value pairs. The var_dump() function dumps information about one or more variables. The information holds type and value of the variable(s). Here string is type of data and 13, 25 and 12 are no of characters our Strings have. So, our values are stored corresponding to column name we have given in feature file table.