Data table support

35 views
Skip to first unread message

Mohit Manrai

unread,
Jul 29, 2013, 9:09:49 AM7/29/13
to subs...@googlegroups.com
Hi,

Does Substeps support data tables?

Here is an example of data table from cucumber website

Given the following users exist:
  | name  | email           | phone |
  | Aslak | as...@email.com | 123   |
  | Matt  | ma...@email.com  | 234   |
  | Joe   | j...@email.org   | 456   | 

I am trying to figure out how to pass this data table to substeps file/java code

Mohit

Ian Moore

unread,
Jul 29, 2013, 10:05:48 AM7/29/13
to subs...@googlegroups.com
Hi Mohit,

Yes it does, thanks for pointing that out as that's not very clear in the docs!

you would implement such a step like this:

@Step ("Given the following users exist:")
public void myStepImplementationMethod(final List<Map<String,String>> table) {

// table.get(1).get("phone") will  return "234"
}

Hope that helps!
regards
Ian

Mohit Manrai

unread,
Jul 29, 2013, 10:31:52 AM7/29/13
to subs...@googlegroups.com
Thanks Ian for quick reply!

What about substep files? How can I pass table parameter to *.substeps file?

For example:

Feature File:

Given the following users exist: | name | email | phone |
| Aslak | as...@email.com | 123 | | Matt | ma...@email.com | 234 | | Joe | j...@email.org | 456 |


Substeps File

Define: Given the following users exist:
          Sub Step 1 (I want to pass table data to this step)
          Sub Step 2 (I want to pass table data to this step)



Step Implementation(Java file)

@Step ("Sub Step 1")
public void myStepImplementationMethod(
final List<Map<String,String>> table) {

// table.get(1).get("phone") will  return "234"
}

@Step ("Sub Step 2")
public void myStepImplementationMethod2(
final List<Map<String,String>> table) {

// table.get(1).get("phone") will  return "234"
}

Regards,
Mohit

Ian Moore

unread,
Jul 29, 2013, 11:00:49 AM7/29/13
to subs...@googlegroups.com
Hi Mohit,

that was something we were looking to implement but became slightly unstuck with how it should work, so I'm afraid at present this easily possible, although there is a workaround I can describe.
It was a little while ago that we were looking at this and I've been distracted elsewhere since, I think the stumbling block was around if you were passing a table to a substep would the substep be called once for each row, or would the entire table be passed?  If it was iteration, the other potential issue is then how you reference that table inside the substep and in particular how you might reference individual column values.

It sounds like you simply wished to pass the entire table through ?

The workaround is to implement a step which puts the table parameter into the ExecutionContext:

Scenario: someScenario
     
Given my users are stashed
     
And the stashed users exist

Define: And the stashed users exist
         
Sub Step 1
         
Sub Step 2

@Step("Given my users are stashed")

public void myStepImplementationMethod(final List<Map<String,String>> table) {

     
ExecutionContext.put(Scope.SCENARIO, "my_users", table);
}

 
@Step("Sub Step 1")
public void mySubStepImplementationMethod() {
   
   
List<Map<String,String>> table = (List<Map<String,String>>)ExecutionContext.get(Scope.SCENARIO, "my_users");

}

I'll agree that this isn't ideal and isn't as neat as it could be.  I'll take another look at how we could perhaps implement this.
regards
Ian

Mohit Manrai

unread,
Jul 29, 2013, 11:22:57 AM7/29/13
to subs...@googlegroups.com
Thanks Ian
Reply all
Reply to author
Forward
0 new messages