2

After token authentication flow when trying to execute the next scenario using the generated token getting error:

[main] INFO com.intuit.karate - karate system property was: null

I have used the following code:
Feature file:

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 class:

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>

Karate-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;
}

Note: Token Authentication flow executed successfully

Expected: should pic the token stored in a variable and execute next scenario

Actual: getting error[main] INFO com.intuit.karate - karate system property was: null

Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
pavizham
  • 31
  • 1
  • 3

1 Answers1

3

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

is the message you receive due to the code in your Karate-config.js

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

if you run your maven command as

mvn test -Dkarate.env=e2e

you will get something like

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

But this will not solve the issue with the accessToken. As explained here by Peter Thomas the Scenarios are supposed to run indipendently.

Each Scenario should be able to run stand-alone.

You can define the authentication in a separated feature and call it in the scenario were you need it as

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
Maurizio Lo Bosco
  • 1,199
  • 16
  • 17