Dynamic data in Cucumber tables

3,061 views
Skip to first unread message

Rahul Sharma

unread,
May 23, 2012, 6:41:18 AM5/23/12
to cu...@googlegroups.com
Hi Guys,

It might have been asked many times before but I can't seem to find a good answer.

I am trying to do the following:

When I block a member on the site for "some reason" for 6 months
Then I should see the following record:
|Active|Time Limit|      Start Date            | End Date                  |
|true   | 6 months | 23-May-2012 12:37:29| 23-Nov-2012 12:37:29|

I am using to compare the above table with a similar table on the page.

Now , since the test blocks a member in the given step, the start date would be todays date. How can I replace the start date in the table above with something like Time.now('some_formatting') so that i can then match it against what's on the page?

I have seen some people suggesting using an extra step to assign current time to a an instance variable and then using it in the table. Is there any simpler way to do this?

Josh Chisholm

unread,
May 23, 2012, 8:08:52 AM5/23/12
to cu...@googlegroups.com
You could use natural language in your scenarios, then parse the date in your step definition with a gem like chronic:


...so you can use terms like "today" or "in 6 months" as your table values.


Josh


-- There are two rules:
 
1) Please prefix the subject with [Ruby], [JVM] or [JS]. This allows people to filter messages.
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en

Rahul Sharma

unread,
May 23, 2012, 8:23:17 AM5/23/12
to cu...@googlegroups.com
On 23 May 2012 13:08, Josh Chisholm <joshuac...@gmail.com> wrote:
On 23 May 2012, at 11:41, Rahul Sharma wrote:

Hi Guys,

It might have been asked many times before but I can't seem to find a good answer.

I am trying to do the following:

When I block a member on the site for "some reason" for 6 months
Then I should see the following record:
|Active|Time Limit|      Start Date            | End Date                  |
|true   | 6 months | 23-May-2012 12:37:29| 23-Nov-2012 12:37:29|

I am using to compare the above table with a similar table on the page.

Now , since the test blocks a member in the given step, the start date would be todays date. How can I replace the start date in the table above with something like Time.now('some_formatting') so that i can then match it against what's on the page?

I have seen some people suggesting using an extra step to assign current time to a an instance variable and then using it in the table. Is there any simpler way to do this?

You could use natural language in your scenarios, then parse the date in your step definition with a gem like chronic:


...so you can use terms like "today" or "in 6 months" as your table values.


Josh


Hi Josh,

Thanks for your response. I am already using Chronic and also Timecop in my project. The question is how can I use terms like Time.now or Chronic.parse('tomorrow') in the tables? 
-- There are two rules:
 
1) Please prefix the subject with [Ruby], [JVM] or [JS]. This allows people to filter messages.
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en

-- There are two rules:
 
1) Please prefix the subject with [Ruby], [JVM] or [JS]. This allows people to filter messages.
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en



--
Regards,

Rahul Sharma
Ph:+44 7800 736851

Yogesh Aggarwal

unread,
May 23, 2012, 8:27:21 AM5/23/12
to cu...@googlegroups.com
In addition how can we handle the same thing with cucumber-JVM. Does chronics exists for JVM also and also how to use this.

Andrew Premdas

unread,
May 23, 2012, 8:38:49 AM5/23/12
to cu...@googlegroups.com
On 23 May 2012 13:23, Rahul Sharma <rahulsh...@gmail.com> wrote:


On 23 May 2012 13:08, Josh Chisholm <joshuac...@gmail.com> wrote:
On 23 May 2012, at 11:41, Rahul Sharma wrote:

Hi Guys,

It might have been asked many times before but I can't seem to find a good answer.

I am trying to do the following:

When I block a member on the site for "some reason" for 6 months
Then I should see the following record:
|Active|Time Limit|      Start Date            | End Date                  |
|true   | 6 months | 23-May-2012 12:37:29| 23-Nov-2012 12:37:29|

I am using to compare the above table with a similar table on the page.

Now , since the test blocks a member in the given step, the start date would be todays date. How can I replace the start date in the table above with something like Time.now('some_formatting') so that i can then match it against what's on the page?

I have seen some people suggesting using an extra step to assign current time to a an instance variable and then using it in the table. Is there any simpler way to do this?

You could use natural language in your scenarios, then parse the date in your step definition with a gem like chronic:


...so you can use terms like "today" or "in 6 months" as your table values.


Josh


Hi Josh,

Thanks for your response. I am already using Chronic and also Timecop in my project. The question is how can I use terms like Time.now or Chronic.parse('tomorrow') in the tables? 

And the answer was you can't but you can rephrase your feature so that the date parsing is done in your step definitions where you can use ruby code.

e.g. rewrite your feature as

When I block Fred 
Then I should see Fred is blocked

If you start with this you can add the extra details like time period later e.g.

When I block Fred for duplicate posting
Then I should see Fred is blocked for 3 months

etc. 
-- There are two rules:
 
1) Please prefix the subject with [Ruby], [JVM] or [JS]. This allows people to filter messages.
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en

-- There are two rules:
 
1) Please prefix the subject with [Ruby], [JVM] or [JS]. This allows people to filter messages.
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en



--
Regards,

Rahul Sharma
Ph:+44 7800 736851

-- There are two rules:
 
1) Please prefix the subject with [Ruby], [JVM] or [JS]. This allows people to filter messages.
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en



--
------------------------
Andrew Premdas

Chetan Parmar

unread,
May 23, 2012, 8:09:38 AM5/23/12
to cu...@googlegroups.com
I am also looking for the same thing

Chetan Parmar

unread,
May 23, 2012, 8:21:48 AM5/23/12
to cu...@googlegroups.com
Hi

could anybody please provide me how to iterate this table in my statement

On Wed, May 23, 2012 at 5:38 PM, Josh Chisholm <joshuac...@gmail.com> wrote:

Chetan Parmar

unread,
May 24, 2012, 5:17:13 AM5/24/12
to cu...@googlegroups.com
could anybody please reply me with the basic code

it's very urgent for me

On Wed, May 23, 2012 at 5:38 PM, Josh Chisholm <joshuac...@gmail.com> wrote:

Chetan Parmar

unread,
May 24, 2012, 6:02:07 AM5/24/12
to cu...@googlegroups.com
Guys finally i got it

My feature is as following

@DataTableTest1
Scenario: Data Table Testr
     Given Data table test
     |test  |main1 |
     |test1 |main2 |

the following are respective code to read this values

 @When("^Data table test$")
    public void ILoggedInWith(DataTable table) throws Exception{
        try{
            System.out.println("**********************"+table.raw().size());
            System.out.println("**********************"+table.raw().get(0).get(0).toString());
            System.out.println("**********************"+table.raw().get(0).get(1).toString());
           
            System.out.println("**********************"+table.raw().get(1).get(0).toString());
            System.out.println("**********************"+table.raw().get(1).get(1).toString());
        }catch(Exception e){
            System.out.println("Login failed");
            throw e;
       }
It will read the data and in form of rows and columns

Thanks
chetan!

Bill Ross

unread,
May 24, 2012, 11:47:26 AM5/24/12
to cu...@googlegroups.com
Maybe you could just rewrite your step:

Then I should see blocked period extending from now to 6 months

Bill

Rahul Sharma

unread,
May 24, 2012, 11:58:59 AM5/24/12
to cu...@googlegroups.com
On 24 May 2012 16:47, Bill Ross <ro...@cgl.ucsf.edu> wrote:
Maybe you could just rewrite your step:

 Then I should see blocked period extending from now to 6 months

Bill
P.S. I moved your response inline.

Well, I have managed to solve it now. In my case, I was just matching a table from my scenario against a table on the page in my application that had dates as one of the columns in the table. So I would't want to just write a new step to just get the dates matched. I have done it like this(This might not be the greatest of ways but for an odd scenario I compromised)

Then I should see the following record:
 |Active|Time Limit|      Start Date            | End Date  |
 |true   | 6 months | DATE_TODAY| DATE_AFTER_SIX_MONTHS|

My step def looks like this:
DATE_TODAY = Chronic.parse('something')
DATE_AFTER_SIX_MONTHS = Chronic.parse('something'')



--
Reply all
Reply to author
Forward
0 new messages