Testing / Karate Framework Interview questions
What is the def keyword used for in Karate?
def declares a variable within a scenario, which can hold a JSON object, an array, a primitive value, or even a JavaScript function, since Karate's variables are backed by its embedded JavaScript engine.
* def userId = 42 * def payload = { name: 'Alice', role: 'admin' } * def greet = function(name){ return 'Hello ' + name } Given url 'https://api.example.com/users/' + userId
Variables defined with def are scoped to the scenario (or shared across scenarios if defined in a Background section) and can be referenced directly in later steps, including inside expressions like URLs, request bodies, and match assertions, which is what makes Karate feature files feel more like a lightweight scripting language than a static list of steps.
More Related questions...