Search This Blog

About Me

Chennai, TamilNadu, India
I am a craftsperson by passion and quality assurer by profession. I love to build - a craft or a piece of code. I look for reusability in crafts and usability in APPs. I always find something new to create and something new to learn. I believe in liberal world, flexible schedules and boundaryless place . https://automationautomated.blogspot.com/p/am-into-software-test-automation-and-r.html

Monday, April 10, 2017

Simple API Automated Testing + KARATE

Simple API Automated Testing + KARATE     

Intuit has open-sourced ‘Karate’, a framework that makes the tall claim that the business of testing web-APIs can actually be — fun. Really???

 Lets try to find out .... 

Join my journey with Karate for API automation testing

This has following components

  • Java - Maven
  • Cucumber - BDD
  • Junit
  • Karate package

Karate enables you to script a sequence of calls to any kind of web-service and assert that the responses are as expected. It makes it really easy to build complex request payloads, traverse data within the responses, and chain data from responses into the next request. Karate's payload validation engine can perform a 'smart compare' of two JSON or XML documents without being affected by white-space or the order in which data-elements actually appear, and you can opt to ignore fields that you choose.

Since Karate is built on top of Cucumber-JVM, you can run tests and generate reports like any standard Java project. But instead of Java - you write tests in a language designed to make dealing with HTTP, JSON or XML - simple.

SetUp:
We are going to setup a Java Maven - Cucumber JVM project 
1. Start up your favorite IDE. (I’ll be using Eclipse for this example.)
2. Go to File>New>Maven Project and take the defaults on first screen.
3. Under New Maven Project, create a click on Add Archetype
4. Enter the following info:
Archetype Group Id= com.intuit.karate
ArchetypeArtifactId= karate-archetype
ArchetypeVersion=0.2.7

5. Click OK.
6. It should find the Karate-archetype. Click on Next.
7. On the next screen, enter:
GroupId=com.automationautomated.karatedemo
ArtifactId=karatedemo

You will get following dependency automatically added 
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-junit4</artifactId>
            <version>0.2.7</version>
            <scope>test</scope>
        </dependency>

If you don't have Cucumber plugin to eclipse add it by using 


Sample REST API to Test :
We’ll be using some public APIs available as the test application.
 This comes with a nice REST API that we can easily interact with as an example to test JSON.

Restful REST web-service to get and search States and territories of a Country 

http://services.groupkt.com/state/get/{countryCode}/all
This rest web service will return a list countries in JSON format, each country object has 
{
      "country" : "IND",
      "name" : "Andhra Pradesh",
      "abbr" : "AP",
      "area" : "160205KMS",
      "largest_city" : "Visakhapatnam",
      "capital" : "Hyderabad"
    }

REST API test design with Karate and Cucumber :
Create a package country under src/test/java 
Inside the country package create a file  getStates.feature  and getStatesTest.java
getStates.feature
#Sample Karate Feature Definition Template
@karatesample
Feature: Sample Karate API Automation

Scenario: Get all states of a country
Given url 'http://services.groupkt.com/state/get/IND/all'
When method get
Then status 200

getStatesTest.java
package country;

import org.junit.runner.RunWith;

import com.intuit.karate.junit4.Karate;

import cucumber.api.CucumberOptions;

@RunWith(Karate.class)
@CucumberOptions(plugin = {"pretty""html:target/cucumber"})
public class getStatesTest {

}
Select the getStatesTest.java and RunAs JUnit Test
You will see following in eclipse console:

#Sample Feature Definition Template
@sample
Feature: Sample Karate API Automation
15:47:16.572 [main] INFO  com.intuit.karate.ScriptBridge - karate.env system property was: null 
Apr 10, 2017 3:47:17 PM org.glassfish.jersey.logging.LoggingInterceptor log
SEVERE: 1 * Sending client request on thread main
1 > GET http://services.groupkt.com/state/get/IND/all

Apr 10, 2017 3:47:18 PM org.glassfish.jersey.logging.LoggingInterceptor log
SEVERE: 1 * Client response received on thread main
1 < 200
........
1 < Vary: Accept-Encoding
{
  "RestResponse" : {
    "messages" : [ "More webservices are available at http://www.groupkt.com/post/f2129b88/services.htm", "Total [36] records found." ],
    "result" : [ {
      "country" : "IND",
      "name" : "Andhra Pradesh",
      "abbr" : "AP",
      "area" : "160205SKM",
      "largest_city" : "Visakhapatnam",
      "capital" : "Hyderabad, India"
    }, {
      "country" : "IND",
      "name" : "Arunachal Pradesh",
      "abbr" : "AR",
      "area" : "83743SKM",
      "capital" : "Itanagar"
    }, {.....

15:47:18.056 [main] DEBUG com.intuit.karate.StepDefs - response time in milliseconds: 1034

  Scenario: Get all states of a country                       [90m# country/getStates.feature:23[0m
    [32mGiven [0m[32murl [0m[32m[1m'http://services.groupkt.com/state/get/IND/all'[0m [90m# StepDefs.url(String)[0m
    [32mWhen [0m[32mmethod [0m[32m[1mget[0m                                           [90m# StepDefs.method(String)[0m
    [32mThen [0m[32mstatus [0m[32m[1m200[0m                                           [90m# StepDefs.status(int)[0m

1 Scenarios ([32m1 passed[0m)
3 Steps ([32m3 passed[0m)
0m2.608s



Reports:
Refresh the karasmaple project and in taget>cucumber>index.html view the cucumber -BDD report.
index.html
#Sample Feature Definition Template
@sample Feature: Sample Karate API Automation

Scenario: Get all states of a country
  • Given url 'http://services.groupkt.com/state/get/IND/all'
  • When method get
  • Then status 200

Wondering wheres the Step definitions? Thats the beauty, you don't have to write step definition in Karate. All are inbuilt but you have to remember few keywords while writing the Feature file.

No comments:

Post a Comment

AutomationAutomated Blog