About Karate:
Karate is an open source library used to test the web services based on the plain English in the text file such as feature file. You could automate the web service testing based on the below highlighted points associated with Karate;
- Support both SOAP and REST
- Schema validation
- Assert the response
- Response data chaining to next request
- Data elements comparison (Support both JSON and XML)
- Data driven approach using .js file
- Test Report generation
Please refer the long list of supported feature sets using the below URL;
https://github.com/intuit/karate
http://tinyurl.com/karatera (REST Assured vs Karate)
Karate Setup:
- Create a maven specific project in your Eclipse
- Please use the below dependencies to download the Karate library in your Eclipse IDE;
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>0.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>0.7.0</version>
<scope>test</scope>
</dependency>
How to use Karate:
Create a Runner file (Java class file) based on the below details;
package karate.test;
import com.intuit.karate.junit4.Karate;
import cucumber.api.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Karate.class)
@CucumberOptions(features = “Features/MockTest.feature”)
public class Runner {
}
Create a feature file (Example: Test.feature) based on the below details;
Feature: REST API testing using Karate with Mockoon
Background:
* url endpoint
Scenario: Test REST request and assert the response
Given path queryName
When method GET
Then status 200
And print response
And match response contains {name:’Gopi’}
And def accountNumber = response.multipleaccounttype[0].savings
And print accountNumber
Calling the data such as URL and path name from karate-config.js file as mentioned below; This file should be added in the project src location;
function () {
var port = 3000;
var baseUrl = ‘https://www.hubino.com/resources‘
var config = {
endpoint : baseUrl + ‘:’ + port + ‘/accountList’,
queryName: ‘/name’,
};
karate.configure(‘connectTimeout’, 30000);
karate.configure(‘readTimeout’, 60000);
return config;
}
About REST Testing using Karate based on the above code snippet:
- Calling the URL from karate-config.js file using the syntax of ‘* url endpoint’
- Pass the path name in the ‘Given’ condition
- Trigger the GET method using ‘When method GET’
- Assert the status as ‘200’ in the ‘Then’ condition
- Print the response received from the web server
- Assert the response contains the name is Gopi in the ‘And’ condition
- Assign a variable using def and getting the account number using ‘And’ condition
- Print the fetched account number in the response
About karate-config.js file:
- Declare the URL details in a variable to reuse in another feature files
- Declare the path name in queryName variable to reuse in feature file
- Return the values
After running the runner file(Runner.java), Karate will start executing the feature file based on the step by step detailed information and publish the test results in HTML format as mentioned below;
I’ve used mockoon to mock the REST API using the below details;
Thank you!!!
Image Credit – https://www.lifehacker.com.au