karatelabs / karate

Test Automation Made Simple
https://karatelabs.github.io/karate
MIT License
8.08k stars 1.94k forks source link

Unable to print env within JS in a shared context #1356

Closed digoo closed 3 years ago

digoo commented 3 years ago

File: varAtJS.zip Could be related to #1322 #1327 #1280 but not duplicate as far I have tested.

It happened on releases: 0.9.5 and 0.9.6 and with develop branch. It used to work about ~20 days ago with develop branch.

It's a simple test:

Feature: 

Scenario: test
  * Common.printEnv()

Where we have at karate-config.js a common routine file being loaded and the var we want to log:

function fn() {    
  var env = karate.env;
  var Common = karate.call('classpath:routine/routineTest.feature');

  if (!env) {
    env = 'dev';
  }
  var config = {
    env: env,
    Common: Common
  }
  return config;
}

And the common routine file has a JS function trying to read the env variable:

Feature: Common routine file

Scenario: Common routines
    * def printEnv =
    """
       function() {
           karate.log('The Env:', env);
       } 
    """

Then it fails with:

command: /Users/rodrigo/test/myproject/src/test/java/test/test.feature:5
17:34:27.549 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - found scenario at line: 5
17:34:27.900 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate.Runner - <<fail>> feature 1 of 1: src/test/java/test/test.feature
---------------------------------------------------------
feature: src/test/java/test/test.feature:5
report: target/surefire-reports/src.test.java.test.test.json
scenarios:  1 | passed:  0 | failed:  1 | time: 0.0148
---------------------------------------------------------
17:34:27.920 [com.intuit.karate.cli.Main.main()] INFO  com.intuit.karate.Runner - waiting for 1 parallel features to complete ...
17:34:27.921 [com.intuit.karate.cli.Main.main()] INFO  com.intuit.karate.Runner - all features complete

HTML report: (paste into browser to view) | Karate version: 2.0.0
file:///Users/user/test/myproject/target/surefire-reports/karate-summary.html
===================================================================

Karate version: 2.0.0
======================================================
elapsed:   0.95 | threads:    1 | thread time: 0.01 
features:     1 | ignored:    0 | efficiency: 0.02
scenarios:    1 | passed:     0 | failed: 1
======================================================
failed features:
src.test.java.test.test: test.feature:6 - evaluation (js) failed: Common.printEnv(), javax.script.ScriptException: ReferenceError: "env" is not defined in <eval> at line number 2
stack trace: jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)

HTML report: (paste into browser to view) | Karate version: 2.0.0
ptrthomas commented 3 years ago

one line change: var Common = karate.call(true, 'classpath:routine/routineTest.feature');

refer docs: https://github.com/intuit/karate#call-vs-read see 8the row

digoo commented 3 years ago

Thank you Peter for pointing to the missing detail!

I have tried with karate.callSingle() but it seems to not work. It's that designed to work that way? If not I could try to include this option there too I would just need your help to point a headstart on the code for me. Thank you once again.

ptrthomas commented 3 years ago

@digoo not designed that way. I think it is fine as it is. if you need to pass any variables, do it explicitly:

karate.callSingle('some.feature', { some: 'value' });