如何在空手道 DSL 的场景大纲中传递动态变量

How to pass dynamic variable in Scenario outline in Karate DSL

我有一种情况需要在空手道中传递不同种类的 Date 类型变量。 为此,我创建了一个 JAVA 方法并在功能文件中调用它,如下所示。

我在 Scenario Outline 中看到它的 cucumber 限制不能支持动态变量。我也读过 https://github.com/intuit/karate#the-karate-way 但不知何故,我不知道如何解决以下情况。

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'|

你一定漏掉了什么,看起来你有几个错别字。

让我们举一个适合我的简单例子:

Feature:

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

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

Examples:
| name |
| foo  |

所以重点是 - 您可以插入一个使用 Examples table 中的数据的函数来动态生成更多数据。

如果您仍然卡住,请按照以下流程操作:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

下面的代码适合我:

  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           |

特点:测试一些东西

场景大纲:测试场景大纲 * 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'|