Karate framework - API & UI Automation

Why take this course?
To accomplish the tasks outlined in your list, we'll go through each step in detail. Let's start by creating a karate-config.js
file and then use it within our scenarios, update the runner class to recognize the Karate configuration, and finally, understand how reports are generated by the Karate framework.
Step 1: Create karate-config.js
Create a JavaScript (or JSON) file named karate-config.js
in your project's root directory. This file will hold all the configuration settings for the Karate framework. Here's an example of what it might look like:
function karateConfig() {
var config = {
// Set base URL if required
baseUrl: 'https://example.com',
// Define your endpoints here
services: {
user: {
create: '/api/users',
update: '/api/users/{id}'
}
},
// Variables that can be reused in tests
vars: {
username: 'testuser',
password: 'testpass'
},
// Groups of scenarios to run in parallel (optional)
parallel: true,
// Custom runner settings (optional)
runner: {
// Your custom runner logic here
}
};
return config;
}
Step 2: Use variables defined in karate-config.js
inside the scenario
In your feature files, you can now use the variables defined in karate-config.js
by calling them within the Scenario:
block using the * def
construct or directly within the Gherkin steps. Here's an example:
Feature: User creation
Scenario: Create a new user with given username and password
* def config = karateConfig()
Given url '${config.baseUrl}' + config.services.user.create
And header Authorization = 'Basic ' + credentialEncoder(config.vars.username, config.vars.password)
And method post
And body {
"username": config.vars.username,
"password": config.vars.password
}
When method post
Then status 201
Step 3: Update runner class to recognize Karate config file
To update the runner class to recognize the karate-config.js
file, you don't typically need to modify the runner class itself if you're using the Karate DSL directly in your feature files. Karate automatically picks up configurations from a file named karate-config.js
(or karate-config.json
) when it is present in the same directory as your karate-config.js
file or any of its parent directories.
Step 4: Report Generation
Karate generates reports by default after the tests are executed. The default report generated by Karate is an HTML report that you can find in the target/reports directory (for Maven projects) or in the reports folder (if you're using Gradle). This report includes all the tests run, their statuses, and any errors or failures encountered.
To customize reports or generate additional reports, such as Cucumber JSON reports, you can use Karate's options to specify the types of reports you want to generate:
// In your test runner file
karate.options({
reports: {
'html': {
directory: 'target/reports', // change this path as you wish
timeout: 10, // optional configuration
title: 'My API Tests' // optional configuration
},
'junit': 'target/reports/junit.xml', // for JUnit integration
'rerun': 'target/reports/rerun.txt' // rerun failed scenarios
}
});
Remember to execute the Karate tests using your test runner that includes this configuration.
Conclusion
By following these steps, you will have created a configuration file for Karate, used its variables in your feature files, ensured that your custom runner logic is picked up (if necessary), and understand how to generate reports with Karate. This setup will allow you to run API tests efficiently and with the flexibility provided by the Karate framework.
Course Gallery




Loading charts...