Gatling集成Karate时Feeder数据无法传入Feature,取值显示Undefined的问题求助
I’ve run into this exact frustrating issue before—when your feeder data just refuses to make its way to the Karate feature file! Let’s break down the likely causes and actionable fixes step by step.
First: Confirm Your Gatling Session Has the Data
First things first—let’s rule out whether the feeder is even putting data into the Gatling session. Your Simulation already includes a println(session) line, so check the console output when you run the test. You should see a session entry like this:
Session(Scenario name,1,Map(uri -> https://dev-url.com, token -> your-actual-token, userId -> 12345), ...)
If the uri, token, or userId keys are missing from this output, the problem lies with your feeder, not the Karate integration:
- Double-check that
runEnvis properly assigned (e.g.,val runEnv = "dev"somewhere in your Simulation). A missingrunEnvwould create an invaliduri, but it should still appear in the session. - Verify that
getApiAuthTokenreturns valid values forgetTokenandgetUserID. Test with hardcoded values temporarily to isolate the issue:
If this hardcoded data shows up in the session, yourprivate val feeder = Iterator.continually(Map( "uri" -> "https://test-url.com", "token" -> "test-token-123", "userId" -> "999" ))getApiAuthTokenmethod is likely returning null or has an async/execution issue.
If Session Has Data, Fix Karate’s Variable Access
If the session contains the data but Karate still shows undefined, let’s adjust how variables are passed to the feature file:
Option 1: Explicitly Pass Variables to Karate
Instead of relying on the __gatling prefix, explicitly pass session variables to the Karate feature using .set()—this is often more reliable:
val scn: ScenarioBuilder = scenario("Scenario name") .feed(feeder) .exec(session => { println(session) session }) .exec(karateFeature("classpath:specs/api.feature") .set("uri", "${uri}") .set("token", "${token}") .set("userId", "${userId}") )
Then update your Feature file to use the variable names directly (no __gatling prefix):
Feature: Scenario name Background: * url uri * header Authentication = 'Token ' + token Scenario: Get User Details Given path 'users', userId When method get Then status 200 And print response
Option 2: Fix the __gatling Syntax
If you prefer using the __gatling prefix, some Karate versions require bracket notation instead of dot notation to access session variables. Try modifying your Feature file:
Background: * url __gatling['uri'] * header Authentication = 'Token ' + __gatling['token'] Scenario: Get User Details Given path 'users', __gatling['userId']
Check Version Compatibility
Make sure your Gatling and Karate versions play nice together:
- Karate 1.0.x works best with Gatling 3.7+
- Older Karate versions (<=0.9.6) have different
__gatlingvariable behavior. Upgrading to a recent stable version often resolves these integration quirks.
Bonus: Clean Up Your Karate Protocol Definition
Your current protocol has duplicate "/" entries, which might cause unexpected behavior (though not directly the undefined issue). Update it to target specific paths:
val protocol: KarateProtocol = karateProtocol( "/users" -> Nil, "/" -> pauseFor("get" -> 3, "post" -> 5) )
内容的提问来源于stack exchange,提问作者Taras Horuk

