Karate 任何一个例子都可以使用一个java类实例来调用一次,并在具有不同功能文件的空手道中的所有场景中使用

Karate 任何一个例子都可以使用一个java类实例来调用一次,并在具有不同功能文件的空手道中的所有场景中使用,karate,Karate,我一直在使用空手道演示中的DBUtils类,我知道这个类与空手道无关。我有一个问题,比如给出的一个例子,每个场景的后台都调用了DBUtlis类,应该在所有featurefiles后台中提到它: 任何类似于我们配置一次并在所有场景中使用该DB实例变量的东西??如果是,请举例说明 彼得发表以下评论后更新: Feature: DB Background Background: * def name = "Sandeep"; Feature: Get Account Details Bac

我一直在使用空手道演示中的DBUtils类,我知道这个类与空手道无关。我有一个问题,比如给出的一个例子,每个场景的后台都调用了DBUtlis类,应该在所有featurefiles后台中提到它:

任何类似于我们配置一次并在所有场景中使用该DB实例变量的东西??如果是,请举例说明

彼得发表以下评论后更新:

Feature: DB Background

Background:
    * def name = "Sandeep";
Feature: Get Account Details

Background:

    * def actualname = PersonName;

@golden
Scenario: user 1 details
 * def expectedFormat = read('../requestFiles/format.json')

        Given url 'https://reqres.in/api/users'
        And params ({id: '1'})
        When method Get
        Then match response.data.email == "george.bluth@reqres.in"
        Then print '###################################name is: ', actualname
        Then print '###################################name is: ', PersonName
19:31:33.416 [ForkJoinPool-1-worker-0] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['data']['email']
19:31:33.416 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
19:31:33.432 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
配置:

主要功能文件:

在另一个功能文件中重用DB实例

请确认这是否是正确的方法

试运行字符串:

var result = karate.callSingle('classpath:featureFiles/dbBackground.feature', config);

  config.PersonName = result.name;
Feature: DB Background

Background:

    * def name = "Sandeep";

    @golden
Scenario: user sample details
 * def expectedFormat = read('../requestFiles/format.json')

        Given url 'https://reqres.in/api/users'
        And params ({id: '1'})
        When method Get
        Then match response.data.email == "george.bluth@reqres.in"
主要功能:

Feature: DB Background

Background:
    * def name = "Sandeep";
Feature: Get Account Details

Background:

    * def actualname = PersonName;

@golden
Scenario: user 1 details
 * def expectedFormat = read('../requestFiles/format.json')

        Given url 'https://reqres.in/api/users'
        And params ({id: '1'})
        When method Get
        Then match response.data.email == "george.bluth@reqres.in"
        Then print '###################################name is: ', actualname
        Then print '###################################name is: ', PersonName
19:31:33.416 [ForkJoinPool-1-worker-0] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['data']['email']
19:31:33.416 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
19:31:33.432 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
其他功能:

Feature: DB Background

Background:
    * def name = "Sandeep";
Feature: Get Account Details

Background:

    * def actualname = PersonName;

@golden
Scenario: user 1 details
 * def expectedFormat = read('../requestFiles/format.json')

        Given url 'https://reqres.in/api/users'
        And params ({id: '1'})
        When method Get
        Then match response.data.email == "george.bluth@reqres.in"
        Then print '###################################name is: ', actualname
        Then print '###################################name is: ', PersonName
19:31:33.416 [ForkJoinPool-1-worker-0] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['data']['email']
19:31:33.416 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
19:31:33.432 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
控制台结果显示为空:

Feature: DB Background

Background:
    * def name = "Sandeep";
Feature: Get Account Details

Background:

    * def actualname = PersonName;

@golden
Scenario: user 1 details
 * def expectedFormat = read('../requestFiles/format.json')

        Given url 'https://reqres.in/api/users'
        And params ({id: '1'})
        When method Get
        Then match response.data.email == "george.bluth@reqres.in"
        Then print '###################################name is: ', actualname
        Then print '###################################name is: ', PersonName
19:31:33.416 [ForkJoinPool-1-worker-0] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['data']['email']
19:31:33.416 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
19:31:33.432 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep

更新的干运行2:

var result = karate.callSingle('classpath:featureFiles/dbBackground.feature', config);

  config.PersonName = result.name;
Feature: DB Background

Background:

    * def name = "Sandeep";

    @golden
Scenario: user sample details
 * def expectedFormat = read('../requestFiles/format.json')

        Given url 'https://reqres.in/api/users'
        And params ({id: '1'})
        When method Get
        Then match response.data.email == "george.bluth@reqres.in"
输出:

Feature: DB Background

Background:
    * def name = "Sandeep";
Feature: Get Account Details

Background:

    * def actualname = PersonName;

@golden
Scenario: user 1 details
 * def expectedFormat = read('../requestFiles/format.json')

        Given url 'https://reqres.in/api/users'
        And params ({id: '1'})
        When method Get
        Then match response.data.email == "george.bluth@reqres.in"
        Then print '###################################name is: ', actualname
        Then print '###################################name is: ', PersonName
19:31:33.416 [ForkJoinPool-1-worker-0] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['data']['email']
19:31:33.416 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep
19:31:33.432 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is:  Sandeep

是的,您可以在
karate config.js
中初始化它,然后它将成为一个全局变量


另请看
karate.callSingle()

嘿,彼得,我用一个例子更新了原始帖子。让我知道这是不是正确的方法?@SandeepPadala是的。我不明白你为什么需要包装。不会
config.db=result.db更简单,现在您所有的功能都可以直接使用
db
。明白了!!非常感谢,万岁!!嘿,Peter,我看到空值用于干运行,请检查我的代码片段并说明我的错误在哪里?@SandeepPadala应该是
def DbUtils=Java.type('ApiTest.DbUtils')
,如果仍然卡住,请遵循以下流程: