Using variables and data tables
Karate allows saving values into variables so we can use the same JSON bodies or values multiple times within one test and don’t have to change them in various places if we want to test with different values. Data tables, on the other hand, allow for a more readable definition of JSON data.
Let’s look at how we can use a variable to reduce the amount of code from our previous scenario.
Using variables
Variables are a very powerful tool in Karate tests. They can hold basically any value type, so they are very flexible.
To declare a simple variable, you can use the def
keyword, meaning define. In the following case example, we store the Benjamin
string in the myName
variable and use it in the print
statement:
Scenario: Declaring a variable * def myName = 'Benjamin' * print 'Hello from', myName
When we run it, this logs Hello from Benjamin
, showing us the...