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

Karate框架运行时传参及OTP输入功能技术咨询

Karate Runtime Parameters & OTP Handling Answers

Great questions! Let's break down each one clearly for you:

1. Does Karate support passing runtime parameters?

Absolutely! Karate has native syntax and multiple straightforward ways to work with runtime parameters. Here are the most common approaches:

Command-line parameter passing

You can pass system properties via the command line using -D flags, then access them directly in your feature files with the karate.properties() method:

# Example: Pass OTP and environment as runtime parameters
mvn test -Dkarate.env=staging -Duser.otp=654321

In your feature file, retrieve the parameter like this:

Feature: OTP Verification Flow
  Scenario: Use runtime OTP parameter
    * def otp = karate.properties('user.otp')
    Given url 'https://your-api.com/verify-otp'
    And request { otp: '#(otp)' }
    When method post
    Then status 200

In-code parameter injection

If you're executing tests via the Karate Java API, you can inject parameters directly into the test context:

Map<String, Object> testParams = new HashMap<>();
testParams.put("otp", "123456");
Results testResults = Runner.path("classpath:features")
    .configParam("otp", testParams.get("otp"))
    .parallel(5);

Access the injected value in your feature with karate.config.otp.

2. Handling OTP input (interactive prompt or runtime parameter)

Karate has built-in functionality for this—no need to write custom JavaScript! Here's how to handle both use cases:

Interactive prompt for manual testing

For local testing where you need to input the OTP manually, use karate.prompt() which triggers a native system input dialog:

Feature: End-to-End OTP Flow
  Scenario: Trigger OTP and verify
    # Step 1: Call 3rd API to send OTP to the user's phone
    Given url 'https://your-api.com/send-otp'
    And request { phoneNumber: '+919876543210' }
    When method post
    Then status 200

    # Step 2: Prompt user to enter the received OTP
    * def otp = karate.prompt("Please enter the OTP sent to your phone:")

    # Step 3: Call 4th API with the entered OTP
    Given url 'https://your-api.com/verify-otp'
    And request { otp: '#(otp)', phoneNumber: '+919876543210' }
    When method post
    Then status 200

Runtime parameter for automated/CI testing

For non-interactive environments (like CI pipelines), stick with the runtime parameter method from the first question—this eliminates the need for manual input entirely.

The karate.prompt() method is a native Karate feature, so you don't need any external JavaScript to implement interactive input.


内容的提问来源于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.25 03:30:25