Comments (5)
Likely due to no shared scope here. karate.call
and karate.callSingle
do not default to shared scope. In fact, I don't think callSingle even has an option for that.
Can you try this instead:
* callonce read('header.feature')
from karate.
That works indeed, at least in the simplified example files. In my real tests I'd like to have the call configurable, though (we have a development mode that doesn't need it), so it really looks like
* if (useAuthentication) karate.callSingle('header.feature', { username: 'myuser' } )
Any idea how I can have that with callonce
?
from karate.
Yes, that can be achieved by putting the conditional within the called feature/js instead.
However, this would be arguably less readable since someone would have to check the called file to find out the condition, plus it's a wasted read/call to the file in dev. Tho, maybe that's all you need given that there's no shared scope overload for callSingle.
@ptrthomas , how would you feel about karate.callSingle
having an overload for shared scope to be in parity with karate.call
?
from karate.
@dvargas46 yes I think that makes sense. interesting why this has not come up before. I was looking at the existing reference example:
- https://github.com/karatelabs/karate/blob/03a840805ec0db5ec3f4330754545f1eefbcde0b/karate-demo/src/test/java/karate-config.js
- https://github.com/karatelabs/karate/blob/2e73fc38e397cf4e46c38b5256c0d91261ea5920/karate-demo/src/test/java/demo/headers/headers-single.feature
so even the examples, the pattern is - a) callSingle returns some data b) that data is used to configure headers "centrally" in karate-config.js
from karate.
@ptrthomas I think you are right, I believe what you outlined would be the recommended pattern to use here too.
For example, it could looks something like this to set the auth header globally instead using callSingle:
karate-config.js
function fn() {
const config = {}
const headers = {}
if (karate.env !== 'dev') {
// configure headers with authentication only in non-dev environments - callSingle only called once
const results = karate.callSingle('header.feature', {username: 'myUsername'})
headers.Authorization = results.authHeader
}
//
// any other headers or config variables can also be set ...
//
karate.configure('headers', headers)
return config
}
header.feature
@ignore
Feature: Header
Scenario: Return Authorization header
# or perform some operation with the input first, like Base64 encoding of credentials, etc.
* def authHeader = 'Basic 12345'
test.feature
Feature: Test
Background:
* url 'http://example.com'
# set any scenario specific headers as needed
* header X-TOKEN = 'token-needed-here'
Scenario: Some test
When method GET
Then status 200
from karate.
Related Issues (20)
- afterScenario hook not running when scenario fails HOT 1
- File upload is not working when using Web driver when running test on Sauce Lab or Remote Grid HOT 7
- Karate does not work with Java 22 (?) => OK with Java 21 HOT 6
- Bug: Calling scenario with array argument of different object leads to unwanted behaviour HOT 1
- The switch from the iframe to the sub-iframe is not working HOT 1
- Chrome is not working from jenkins and getting driver config / start failed: start failed, options: {type=chrome, showDriverLog=true, addOptions=["--remote-allow-origins=*"], target=null} HOT 1
- replace graal with karate-js engine HOT 23
- Variables are lost after callonce java.lang.Thread.sleep() HOT 1
- [Improvement] Rename "request" keyword ? HOT 1
- Multiarch image for karatelabs/karate-chrome HOT 1
- getting this exception org.graalvm.polyglot.PolyglotException: not found: src/test/resources/payload/user.json in Karate HOT 2
- In Karate 1.2.0.RC1, the karate property set in karate-config.js file is coming as null when fetched in a Java File HOT 1
- Karate - Setting header for the subsequent calls on the same scenario is not working after updating to 1.x.x HOT 1
- Driver input method cannot type '[' HOT 6
- The request "path" can not be overwrited in mock.feature
- CRC of file being uploaded HOT 1
- MockServer/MockHandler performance HOT 1
- Doubt regarding afterScenario and afterFeature HOT 1
- I am not able to switch to new window from parent window. Switch page is not working HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from karate.