There is a new testing framework out there called Karate that is build on top of the popular Cucumber framework.
Karate makes it easy to script interactions with out web-services under test and verify the results.
Karate is an open-source web-API test-automation framework that can script calls to HTTP end-points and assert that the JSON or XML responses are as expected.
Dependencies
It's easy than BDD Framework for API testing. In karate no need to write step definition.
userinfo host port
┌───────┴───────┐ ┌────┴────────┐ ┌┴┐
http://john.doe:password@www.example.com:123/forum/questions/?tag=networking&order=newest#top
└─┬─┘ └───────────┬────────────────────────┘└─┬─────────────┘└────────┬──────────────────┘└┬─┘
scheme authority path query fragment
Each resource in REST architecture is identified by its URI (Uniform Resource Identifier). A URI is of the following format −
<protocol>://<service-name>/<ResourceType>/<ResourceID>
Karate sits on top of cucumber, it inherits all the functionalities of cucumber, so you can write your API tests in simple Given When Then format and utilize all the cucumber keywords such as Feature, Scenario Outline, Scenario, Examples, Feature tagging.
What is a Web Service?
Applications which are accessed via HTTP APIs are often called Web Services. In other words, a web service is a function that can be accessed by other programs over the web (HTTP).
What is JSON?
JSON stands for JavaScript Object Notation and is a text representation that is also valid JavaScript code.
{
"employers":{ // object {} // object is a employeers
"employee":[ //Array []
{
"id":1,
"name":"Dan Brown",// "" // key = values.. id is a key and its value is 1
"position":"Director",
"status":"active",
}
]
}
}
JSON can be thought of as a hierarchical set of key/value pairs where the value can be:
- Object - delimited by { and }
- Array - delimited by [ and ]
- String - delimited by ” and ”
- Integer
An array is a list of objects or key/value pairs. The keys are String values e.g. “employee”, “id”, “name”, etc.
Method of creating project:
Create a Maven project- Add archetype--( Achetype Group id: com.intuit.karate Archetype Artifact Id: karate-archetype Archetype Version: 0.2.7 .
( It finds the karate archetype and give a project name. All the dependency related karate is download automatically on pom.xml.)
Eg:
1. create a feature file eg: mirror.feature
2. creae a test file
Mirror.feature
Feature: Mirror webapp
Scenario: fetch mirror url
Given url 'https://mirror.attinadsoftware.com'
When method get
Then status 400
MirrorTest
import org.junit.runner.RunWith;
import com.intuit.karate.junit4.Karate;
@RunWith(Karate.class)
public class MirrorTest {
...................................................................................
Karate is a relatively new open-source framework for testing web-services
It is similar like cucumber. It does not need a step defination of cucumber.
Create sprint boot project ..add dev tool and spring web.
add maven dependency. core karate, j4unit, apache karate, karate reporting...
A new capability in Karate is to be able to start an HTTP server and script
responses to an incoming HTTP request
add a parameter also...eg name john and status inactive
Given path ''
And param first_name = 'john'
And param status = 'inactive'
When method get
# Way of Writing Headers
Background:
* url baseUrl
* configure headers = {Content-Type: 'application/json', x-callingSystemUser: 'Adhikam', x-requestUUID: '23232D', x-callingSystemId: 'sys123', DP-MISCINFO: 'Okta.LCC.CSR.Dev.users', DP-OWNER: 'chuck' }
# OR
* def someData = {Content-Type: 'application/json', x-callingSystemUser: 'Adhikam', x-requestUUID: '23232D', x-callingSystemId: 'sys123', DP-MISCINFO: 'Okta.LCC.CSR.Dev.users', DP-OWNER: 'chuck' }
* headers someData
# OR
* headers req_headers
* header Content-Type = 'application/json'
* header x-callingSystemUser = 'Adhikam'
* header x-requestUUID = '23232D'
* header x-callingSystemId = 'sys123'
* header DP-MISCINFO = 'Okta.LCC.CSR.Dev.users'
* header DP-OWNER = 'chuck'
# OR
Scenario: Post method when branchVerified is false
Given path 'testcodes/statcheck'
Then header Content-Type = 'application/json'
And header x-callingSystemUser = 'Adhikam'
And header x-requestUUID = '23232D'
And header x-callingSystemId = 'sys123'
And header DP-MISCINFO = 'Okta.LCC.CSR.Dev.users'
And header DP-OWNER = 'chuck'
****************Assertion***************
[
{
"id": 1,
"name": "FirstUser",
"password": "User1Pass"
},
{
"id": 2,
"name": "SecondUser",
"password": "User2Pass"
}
]
.............................................
Feature: Test User API
Scenario: Fetch all users
Given url 'https://some-api.com/api/users'
When method GET
Then status 200
And assert response.length == 2
And match response[0].name == 'FirstUser'
#assertion size
# Assert numbe of array object
* def size = function(o){ return o.size() }
* def length = size(response)
* assert length == 2
* assert length != 1
.............................................
...................................................
{
"data": {
"id": 2,
"name": "fuchsia rose",
"year": 2001,
"color": "#C74375",
"pantone_value": "17-2031"
}
}
......................
* match $.data == '#present'
* match $.data.id == 2
* match $.data.name == 'fuchsia rose'
# assert array object length
And assert response.length == 1
................................
karate-config.js
function fn() {
var env = karate.env; // get java system property 'karate.env'
karate.log('karate.env selected enviroment was:', env);
Karate.configure("ssl", true)
if (!env) {
env = 'dev'; // a custom 'intelligent' default
} ////env can be anything: dev, qa, staging
var config = { // base config JSON
env: env,
appId: 'manoj',
appSecret: 'adhi',
baseUrl: 'https://labcorp' + env + ' org'/',
};
if (env == 'dev') {
config.appId: 'devmanoj'
config.appSecret: 'mypass'
// over-ride only those that need to be
config.baseUrlDev = 'https://wsa.labcorp.com/dev';
} else if (env == 'qa') {
config.appId: 'qamanoj'
config.appSecret: 'mypass'
config.baseUrlQA = 'https://wsa.labcorp.com/qa';
}
karate.log("My host: "+ config.baseUrl);
//or
//karate.log("Prod URL being used: "+ config.baseUrl);
//karate.log("Dev URL being used: "+ config.baseUrlQA);
//karate.log("QA URL being used: "+ config.baseUrlDev);
// don't waste time waiting for a connection or if servers don't respond within 5 seconds
karate.configure('connectTimeout', 5000);
karate.configure('readTimeout', 5000);
return config;
}
command:
karate Command Line: working
mvn test -Dtest=KarateRunner
mvn test -Dkarate.options="classpath:com/lca/phoenix/lcc/testinfo/karate/statcheckPost.feature"
Run karate from different enviroment:
mvn test -DargLine="-Dkarate.env=AWS_Dev"
mvn test -Dkarate.options="classpath:demo/userFeatureScenarioOutline.feature"
Jenkins : regular test
cd "C:\Dev\modular-framework-texas-practice-saurab"
mvn clean install
check karate saurab...
cd "C:\Dev\karateframework"
mvn clean install
cd "C:\Dev\karateframework"
mvn test -Dtest=KarateRunner
cd "C:\Dev\karateframework"
mvn test -Dkarate.options="classpath:demo/userFeatureScenarioOutline.feature"
mvn test -Dkarate.options="--tags ~@ignore classpath:demo/cats/cats.feature" -Dtest=DemoTestParallel
mvn test -Dtest=KarateRunner
mvn test -Dkarate.options="--tags ~@ignore classpath:demo/userFeatureScenarioOutline.feature" -Dtest=DemoTestParallel
You should know following index properly.
No comments:
Post a Comment