You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将一个Feature文件表格中的值传递到另一个Feature文件的JSON请求中?

How to Pass Table Values from One Feature File to a JSON Request in Another (Karate)

Great question! Let's break down exactly how to make this work with Karate's built-in parameterization and feature calling capabilities.

Step 1: Understand How the Table is Passed Between Features

In your first Feature file, you've already set up the table kit and are using call read('classpath:2.2_parameter_pass.feature') kit to invoke the second feature. Karate automatically handles iterating over each row in the table—meaning the second feature will run once for every row in kit, with each column value available as a top-level variable in the second feature.

For example, the first row's homeMobileCountryCode (310) becomes a variable named homeMobileCountryCode that you can use directly in Feature 2.

Step 2: Use the Variables in Feature 2's JSON Request

In your second Feature file, you can reference these variables directly in your JSON payload using Karate's syntax (#(variableName) or ${variableName}). Here's how to structure your reusable feature to build the JSON request:

Feature: Reusable Feature for Creating API Requests
Scenario: Build and Send JSON Request with Table Parameters
  # Optional: Convert string values to appropriate types if needed (e.g., 'true' to boolean)
  * def considerIp = considerIp == 'true'

  # Define your API endpoint
  Given url 'https://your-target-api.com/endpoint'
  
  # Construct the JSON request using the passed table variables
  And request {
    "homeMobileCountryCode": #(homeMobileCountryCode),
    "homeMobileNetworkCode": #(homeMobileNetworkCode),
    "radioType": #(radioType),
    "carrier": #(carrier),
    "considerIp": #(considerIp),
    "cellTowers": #(cellTowers),
    "wifiAccessPoints": #(wifiAccessPoints)
  }
  
  # Send the request (adjust method to match your API: post, get, etc.)
  When method post
  
  # Validate the response (customize based on your API's expected behavior)
  Then status 200
  
  # Return the response back to Feature 1 if you need to use it later
  * return response

Step 3: Handle the Result in Feature 1

Back in your first Feature file, the result variable will be an array containing the response from each execution of Feature 2 (one entry per row in your table). Your print result statement will output all these responses, which you can then use for further assertions or processing if needed.

Key Notes to Remember

  • Case Sensitivity: Ensure the column names in your table exactly match the variable names you use in Feature 2 (Karate is case-sensitive).
  • Data Type Conversion: If your API expects a boolean but your table has string values like 'true', add a line to convert it (as shown in the example above).
  • Empty Values: Columns with empty values in the table will be passed as null to Feature 2. If you want to omit those fields from the JSON, you can use Karate's conditional logic (e.g., ? cellTowers : null to exclude if empty).

内容的提问来源于stack exchange,提问作者Preethi Ravi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:23:40