API Testing using Karate

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:

  1. Create a maven specific project in your Eclipse
  2. 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 = ‘http://localhost&#8217;;

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;

Karate_1

Karate_3

I’ve used mockoon to mock the REST API using the below details;

https://mockoon.com

Karate_2

Thank you!!!

One thought on “API Testing using Karate

  1. Hi Gopi,

    {Sorry for long post}

    if possible share your comment on my below query.
    I am executing the Karate Suite through Jenkins job which is maven based. While execution the job ends it with comment like
    [INFO] — maven-surefire-plugin:2.12.4:test (default-test) @ Karate_Digital —
    [INFO] Skipping execution of surefire because it has already been run for this configuration
    [INFO] ————————————————————————
    [INFO] BUILD SUCCESS

    I am clueless as not able to execute my suite through Maven in my IDE too with the similar message.

    My POM looks like below

    4.0.0

    Karate_Digital
    Karate_Digital
    0.0.1-SNAPSHOT
    jar

    UTF-8
    1.8
    3.6.0
    0.9.1

    com.intuit.karate
    karate-jersey
    ${karate.version}
    test

    com.intuit.karate
    karate-junit4
    ${karate.version}
    test

    net.masterthought
    cucumber-reporting
    4.5.1

    <!– src/test/java
    **/*.java
    –>

    org.apache.maven.plugins
    maven-compiler-plugin
    ${maven.compiler.version}

    Problem Statement
    true

    report-only

    UTF-8
    ${java.version}
    ${java.version}
    -Werror
    digital_statement_automation_team/src/test/java/com/barclaycard/qe/runner

    **/*Runner_API.java

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s