API Testing with Karate
You heard it right, we are going to learn on how to create a API Testing suite using Karate, Letās go.....
So what is Karate ?
Well, Karate is an Automation Framework with which we can perform API Test Automation,Ā mocks, performance-testing and even UI automation into a single, unified framework. Moreover Karate is open source, and it follows Cucumber BDD styled syntax.Ā
Advantages of Karate
Easy to start automatingĀ
Even non-programmers can start using Karate with just very minimal learning curve
powerful JSON and XML assertions are inbuiltĀ
you just have to createĀ ā.featureā files, no need to create step definitions.Ā
If you're new to programming or automation testing Karate is easy to use since no Java knowledge is required. If this is you, Karate might be the perfect choice for your team.
How to start using Karate?
prerequisite:
Java JDK
Maven
IDE
First you should be creating a project, will be better if its a maven project
Start your IDE, Iāll be using intellijĀ
Create a new maven project
To make use of Karate in the created project, you need two dependencies, and those are
Karate itselfĀ
<dependency> Ā Ā
<groupId>com.intuit.karate</groupId>
Ā <artifactId>karate-apache</artifactId> Ā Ā <version>0.6.0</version>
</dependency>
next we need Junit to facilitate Junit testing
<dependency>
Ā Ā <groupId>com.intuit.karate</groupId> Ā Ā <artifactId>karate-junit4</artifactId> Ā Ā <version>0.6.0</version>
</dependency>
thatās it, we have all the basic things to start writing automation tests. Thatās simple right, yes it is
For testing purpose I am going to make use of Open source weather API(āhttps://www.metaweather.com/api/ā)
In the newly created project, create a new file calledĀ ākarate-config.jsā in the main src folder then, Create a package and add a feature file in it like I have done below,
Lets begin our testing by configuring the test suite, for that we need to add our test details(app url, environment details, etc) in ākarate-config.jsā file. I have added the following details for our test suite,
Whats next ? yes, we can start writing tests..
how to write test using karate ? will it be like rest-assured or jersey ? no.... nothing is complicated in karate, you donāt need to write even a single line of code to write basic tests, then how can we define our tests ? Cucumber feature files.. yeah, you heard me right. We just need to have cucumber feature files.Ā
Shall we write our first test using Karate ?
Our first test is to do a location search and get the lat long details from the API and assert the values.
itās not big, just normal Cucumber feature file with karate keywords in them.Ā
āpathā - this is the place where we give the URL and API EndpointsĀ
āparam queryā - this is where we can specify URL paramsĀ
āmethodā - this is where we need to specify the type of requestĀ
āstatusā -Ā Using this keyword we can assert the response codeĀ
Assertions:
And response.city == 'Chennai'
And response.latt_long == '13.05939,80.245667'
assertions are plain simple, you get the entire response json object, which you can traverse and do your assertions, like the ones that I have added in the above script
What we have to do when we need to do complex assertions or request body creation ? Karate covers that as well, you can create your own custom functions either in Java or Javascript and then use those functions within your feature files.. Shall we try one such custom utility function ?
We will make use of the Endpoint ālocation/search/āĀ and pass lat and long as params, which will give us list of cities that comes under same lat long co-ordinates, and then we can assert the response cities with our custom assertion method. This is how I have written the scenario for this test,
did you see the step which says,
* def responseLength = returnLength(response)
where does this returnLength comes from ? yup, thatās our custom function. Create a utility.js file within your src folder and write the following code in it,
function () { Ā
Ā Ā function returnLength(payload)
Ā Ā {
Ā Ā Ā Ā karate.log(payload.length);
Ā Ā Ā Ā return payload.length;
Ā Ā }
Ā Ā return {
Ā Ā Ā Ā returnLength : returnLength
Ā Ā };
}
Next we need to make sure that Karate knows about this custom function, for that we have to add this utility function in the karate-config.js file, make the following entry in your karate-config file,
config = karate.call('file:src/test/java/com/sample/api/tests/helpers/utility.js',config);
This will make sure that your custom function is known to karate. Thats it, you can call your function in feature files. Easy isnāt it ? yup sooooo easy.Ā
How to execute our test ?
The next big this is execution, Karate makes use of Junit. We need to have a Junit runner class within our project which points to our feature file, nothing more is needed.Ā
instead of cucumberoptions you can see that we are making use of the annotationĀ āKarateOptionsā and passing the feature files in it. And then we have a method with @Test annotation in it, which is making use of the Karate runner to execute the tests. And then we have aĀ āgenerateReportā method which is used to create Cucumber HTML reports,. We can execute this class as an Junit Test, and the results will be in Target folder.Ā
We are done... Simple right ?Ā
I have made use of Karate for our API regression suite, which has around 700 tests in them. We have our tests connected to our CI pipeline and our tests have been very efficient without any flakiness in it. I am so happy that I moved over to Karate from Rest-Assured. Hope you guys will also enjoy by making use of Karate. You can reach out to me if you need help while implementing Karate.Ā Ā














