Sunny Sachdeva
Sunny Sachdeva

Reputation: 289

How to pass dynamic variable in Scenario outline in Karate DSL

I have a situation where I need to pass a different variety of Date type variables in Karate. For this, I created a JAVA method and calling it in a feature file as shown below.

I read that its cucumber limitation which can not support dynamic variables in Scenario Outline. I also read https://github.com/intuit/karate#the-karate-way but somehow, I am not getting any idea how to solve the below situation.

Scenario Outline: test scenario outline
    * def testData = Java.type('zoomintegration.utils.DataGenerator')
    * def meetDate = testData.futureDate(2)


    * def jsonPayLoad =
    """
    {
      "meetingSource": <meetingSource>,
      "hostId": <host>,
      "topic": <topic>,
      "agenda": <topic>,
      "startDateTime": <meetingDate>",
      "timeZone": "Asia/Calcutta",
      "duration": <duration>
    }
    """
    * print jsonPayLoad

    Examples:
    |meetingSource|host|topic|duration|meetingDate|
    |ZOOM          |  abc  |Quarter meeting|30|0|
    |SKYPE         |  abc  |Quarter meeting|30|'1980-08-12'|
    |MS            |  abc  |Quarter meeting|30|'2030-12-12'|

Upvotes: 2

Views: 3211

Answers (3)

Sunny Sachdeva
Sunny Sachdeva

Reputation: 289

Feature: test something

Scenario Outline: test scenario outline * def testData = Java.type('zoomintegration.utils.DataGenerator') * def meetDate = testData.futureDate(2)

* def jsonPayLoad =
"""
{
  "meetingSource": <meetingSource>,
  "hostId": <host>,
  "topic": <topic>,
  "agenda": <topic>,
  "startDateTime": <meetingDate>,
  "timeZone": "Asia/Calcutta",
  "duration": <duration>
}
"""
* eval if (jsonPayLoad.startDateTime == 0) jsonPayLoad.startDateTime = meetDate
* print jsonPayLoad

Examples:
  |meetingSource|host|topic|duration|meetingDate|
  |ZOOM          |  abc  |Quarter meeting|30|0|
  |SKYPE         |  abc  |Quarter meeting|30|'1980-08-12'|
  |MS            |  abc  |Quarter meeting|30|'1999-08-12'|

Upvotes: 1

Gade Raju
Gade Raju

Reputation: 60

Below code works for me:

  Scenario Outline: test scenario outline
    * def testData = Java.type('zoomintegration.utils.DataGenerator')
    * def meetDate = testData.futureDate(<meetingDate>)

    * def jsonPayLoad =
    """
    {
      "meetingSource": <meetingSource>,
      "hostId": <host>,
      "topic": <topic>,
      "agenda": <topic>,
      "startDateTime": #(meetDate),
      "timeZone": "Asia/Calcutta",
      "duration": <duration>
    }
    """
    * print jsonPayLoad

    Examples:
      | meetingSource | host | topic           | duration | meetingDate |
      | ZOOM          | abc  | Quarter meeting | 30       | 1           |
      | SKYPE         | abc  | Quarter meeting | 30       | 2           |
      | MS            | abc  | Quarter meeting | 30       | 3           |

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58058

You must be missing something, and it looks like you have a few typos.

Let's take a simple example that works for me:

Feature:

Background:
* def fun = function(x){ return 'hello ' + x }

Scenario Outline:
* match fun(name) == 'hello foo'

Examples:
| name |
| foo  |

So the point is - you can plug in a function that uses data from your Examples table to dynamically generate more data.

If you are still stuck, please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Upvotes: 0

Related Questions