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

Wednesday, April 12, 2017

API Automated Testing + KARATE 2

API Automated Testing + KARATE  2


We have have seen basic setup and a simple sample in previous post simple-api-automated-testing-BDD-Karate . 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.

Now lets try some complex stuff with multiple examples. 

REST API test design with Karate and Cucumber : Example 2
Create a package country under src/test/java 
Inside the country package create a file  getStates.feature  and getStatesTest.java

We are going to try the API for different countries. [IND and USA]

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

http://services.groupkt.com/state/get/{countryCode}/all
getStates.feature
#Sample Karate Feature Definition Template
@karatesample

Feature: Sample Karate API Automation

Scenario Outline: Get states for different countries

Given url 'http://services.groupkt.com/state/get/<country>/all'
When method get
Then status 200

Examples:
    | country |
    | IND |
    | USA |

getStatesTest.java
Use the same as in previous post

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

Scenario Outline: Get states for different countries

  • Given url 'http://services.groupkt.com/state/get/<country>/all'
  • When method get
  • Then status 200
Examples:

country
IND
USA
@karatesample Scenario Outline: Get states for different countries

  • Given url 'http://services.groupkt.com/state/get/IND/all'
  • When method get
  • Then status 200
@karatesample Scenario Outline: Get states for different countries

  • Given url 'http://services.groupkt.com/state/get/USA/all'
  • When method get
  • Then status 200

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.

About

About this Blogspot 


Am into Software Test Automation and R&D.

I begun my first coding on my Xth holidays, it was mid 90's, a Computer center was started next door in my small town and my parents immediately joined me there and I was excited to be in a place full of computers !! I was introduced to Basic Programming - using Basic language, being maths lover I immediately fell in love with programming. Next 6 years I spent my studies with Computer.

Why I started Blogging?

Gaining knowledge / learning is an innate desire. Like a child I am a very curious person by nature, I question a lot either its a book am reading, movie or an application I test :) .

I am a passionate software tester and Automation enthusiast.  I keep myself update on things happening in TESTERs world.

Theres 2 main things / ideology / advice I got on my early days

  1. I have an Apple and if I give you an Apple, then I dont have any Apple. But I have knowledge and I give you knowledge, then we both have knowledge and even it has possibility of gaining more knowledge ! This I got when I was doing my intern as college student. I never forget and always never hesitated on sharing knowledge. 
  2.  When I was a fresher I was encouraged to ask questions by my client who where product SMEs with 30+ years of experience - ' The most dumb question is the one you never asked',  it's so true which I follow till now. 


I got guidance from great Leads on Automation in my initial stage.
Forums and Blogs from all over the world helped me a lot. Whenever I stuck 98% of time I was able to get answers from fellow bloggers and active forum members.

I joined a wonderful tribe TheTestTribe which get together like minded people in Testing.

On one fine day I thought I got so much from the community, I should give back some and I started blogging, started presenting in meetups and seminars. This is my small way of giving back.



I'm grateful to bloggers and forums through which I learned a lot.

- Mathangi.G



- Mathangi.G