空手道:[main]info com.tuit.karate-karate.env系统属性为:空

2022-05-10 00:00:00 java karate

使用生成的令牌尝试执行下一个场景时的令牌身份验证流获取错误:

[main]infocom.tuit.空手道-空手道系统属性为:空

我使用了以下代码:
功能文件:

Feature: Login Token Authentication http://symex.dyndns.org:6586/
Background: url 'http://symex.dyndns.org:6586/'
Scenario: Token Authentication flow

* path 'token'
* form field grant_type = 'password'
* form field client_id = 'demoapp'
* form field client_secret = 'demopass'
* form field username = 'xxxx
* form field password = 'xxxx'
* method post
* status 200

* def accessToken = response.access_token

Scenario: ForeignCurrencyStockBalance

* path 'api/v1/ForeignCurrencyStockBalance'
* header Authorization = 'Bearer ' + accessToken
# * param access_token = accessToken
* method GET
* status 200

Runner类:

package Runner;

import com.intuit.karate.junit4.Karate;
import com.intuit.karate.KarateOptions;

import org.junit.runner.RunWith;

@RunWith(Karate.class)
@KarateOptions(features = "classpath:Runner/login.feature")
public class LoginRunner{}

POM.XML:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.MyFirstAPI</groupId>
    <artifactId>MyFirst</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.version>3.6.0</maven.compiler.version>
        <karate.version>0.9.3</karate.version>
    </properties>    

    <dependencies>
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-apache</artifactId>
            <version>${karate.version}</version>
            <scope>test</scope>
        </dependency>            
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-junit4</artifactId>
            <version>${karate.version}</version>
            <scope>test</scope>
        </dependency>       
    </dependencies>

    <build>
        <testResources>
            <testResource>
                <directory>src/test/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <compilerArgument>-Werror</compilerArgument>
                </configuration>
            </plugin>
        </plugins>        
    </build>       
</project>

空手道-config.js

function() {
  var env = karate.env; // get system property 'karate.env'
  karate.log('karate.env system property was:', env);
  karate.configure("ssl", true)
  if (!env) {
    env = 'qa';
  }
  var config = {
    env: env,

  }
  if (env == 'dev') {
    // customize
    // e.g. config.foo = 'bar';
  } else if (env == 'e2e') {
    // customize
  }
  return config;
}

注意:令牌身份验证流执行成功

预期:应提取存储在变量中的令牌并执行下一个方案

实际:获取错误[Main]信息com.intuit.karate-空手道系统属性为:空


解决方案

infocom.tuit.karate-karate.env系统属性为:空

您收到的消息是由于您的空手道配置.js中的代码

karate.log('karate.env system property was:', env);

如果您以

身份运行maven命令
mvn test -Dkarate.env=e2e

您将得到类似

的内容

INFO com.intuit.karate - karate.env system property was: e2e

但这不会解决accesToken的问题。 如所述here by Peter Thomas 这些方案应独立运行。

每个方案都应该能够独立运行。

您可以在单独的功能中定义身份验证,并根据需要在场景中调用它

Scenario: ForeignCurrencyStockBalance
* path 'api/v1/ForeignCurrencyStockBalance'
* def authentication = read('classpath:authentication.feature')
* def userLogin = call authentication
* header Authorization = 'Bearer ' + userLogin.access_token
* method GET
* status 200

相关文章