【问题标题】:How to pass dynamic variable in Scenario outline in Karate DSL如何在空手道 DSL 的场景大纲中传递动态变量
【发布时间】:2020-12-17 06:24:13
【问题描述】:

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

我读到它的黄瓜限制不能支持场景大纲中的动态变量。我还阅读了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'|

【问题讨论】:

    标签: karate


    【解决方案1】:

    以下代码对我有用:

      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           |
    

    【讨论】:

      【解决方案2】:

      功能:测试一些东西

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

      【讨论】:

        【解决方案3】:

        您一定遗漏了什么,而且您似乎有一些拼写错误。

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

        Feature:
        
        Background:
        * def fun = function(x){ return 'hello ' + x }
        
        Scenario Outline:
        * match fun(name) == 'hello foo'
        
        Examples:
        | name |
        | foo  |
        

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

        如果还是卡住,请按照这个流程:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

        【讨论】:

        • 感谢您的回复。我们不能在这里使用条件逻辑吗?我的意思是,只有当我的示例值为 0 时,我才想覆盖 startDateTime 的值
        • @SunnySachdeva 为什么不呢。 JS中的所有可能,试试看
        猜你喜欢
        • 2023-02-10
        • 1970-01-01
        • 2019-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        相关资源
        最近更新 更多