Skip to content

Commit fdd4a3b

Browse files
committed
Additional fix for karatelabs#1558 allowing access to Java classes with callSingle() in karate-config.js
1 parent bbecea4 commit fdd4a3b

File tree

5 files changed

+8
-3
lines changed

5 files changed

+8
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3913,7 +3913,7 @@ Refer to this example:
39133913

39143914
You *can* use `karate.callSingle()` directly in a `*.feature` file, but it logically fits better in the global "bootstrap". Ideally it should return "pure JSON" and note that you always get a "deep clone" of the cached result object.
39153915

3916-
IMPORTANT: There are some restrictions when using `karate.callSingle()` especially within [`karate-config.js`](#karate-configjs). Ideally you should return only *pure* JSON data (or a primitive string, number etc.). Keep in mind that the reason this exists is to "cache" data, and *not* behavior. So if you return complex objects such as a custom Java instance or a JS function that depends on complex objects, this [will cause issues when you run in parallel](https://github.com/intuit/karate/issues/1558).
3916+
IMPORTANT: There are some restrictions when using `karate.callSingle()` especially within [`karate-config.js`](#karate-configjs). Ideally you should return only *pure* JSON data (or a primitive string, number etc.). Keep in mind that the reason this exists is to "cache" data, and *not* behavior. So if you return complex objects such as a custom Java instance or a JS function that depends on complex objects, this [might cause issues when you run in parallel](https://github.com/intuit/karate/issues/1558).
39173917

39183918
#### `configure callSingleCache`
39193919
When re-running tests in development mode and when your test suite depends on say an `Authorization` header set by [`karate.callSingle()`](#karatecallsingle), you can cache the results locally to a file, which is very convenient when your "auth token" is valid for a period of a few minutes - which typically is the case. This means that as long as the token "on file" is valid, you can save time by not having to make the one or two HTTP calls needed to "sign-in" or create "throw-away" users in your SSO store.

karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,8 @@ private Object recurseAndAttach(Object o, Set<Object> seen) {
10921092
return attach(value);
10931093
}
10941094
} catch (Exception e) {
1095-
logger.warn("failed to re-attach graal value: {}", e.getMessage());
1095+
logger.warn("failed to re-attach graal value and/or is not executable. Will try to use as host object: {}", e.getMessage());
1096+
return Value.asValue(value.asHostObject());
10961097
}
10971098
return null;
10981099
} else if (o instanceof JsFunction) {

karate-core/src/test/java/com/intuit/karate/core/parallel/karate-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function fn() {
66
var result = karate.callSingle('call-single-from-config.feature', config);
77
config.message = result.response.message;
88
// this will throw the [Multi threaded access requested by thread xxx but is not allowed for language(s) js.] error
9-
// config.Hello = result.Hello;
9+
config.Hello = result.Hello;
1010
var result2 = karate.callSingle('call-single-from-config2.feature', result);
1111
config.message2 = result2.message;
1212
return config;

karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-1.feature

+3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ Background:
55
# background http builder should work even for a dynamic scenario outline
66
* url serverUrl
77
* def data = [ { name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' } ]
8+
* def helloClass = Hello
89

910
Scenario Outline:
1011
* match functionFromKarateBase() == 'fromKarateBase'
1112
* path 'fromfeature'
1213
* method get
1314
* status 200
1415
* match response == { message: 'from feature' }
16+
* match helloClass.sayHello('from the other side') == 'hello from the other side'
17+
* match helloClass.sayHello(name) == 'hello ' + name
1518

1619
Examples:
1720
| data |

karate-core/src/test/java/com/intuit/karate/core/parallel/parallel.feature

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Scenario: one
1515
* match response == { one: '#string' }
1616
* def result = karate.callSingle('call-single-from-feature.feature')
1717
* match result.response == { message: 'from feature' }
18+
* match Hello.sayHello('Bob') == 'hello Bob'
1819

1920
Scenario: two
2021
* path 'two'

0 commit comments

Comments
 (0)