GithubHelp home page GithubHelp logo

vamsidarbhamulla / cucumber-testng-rest-assured Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 7.0 72 KB

License: GNU General Public License v3.0

Gherkin 24.50% Java 75.50%
java testng rest-api rest-assured cucumber-jvm cucumber bdd gherkin gradle maven

cucumber-testng-rest-assured's Introduction

Project

This is demo project to implement rest api testing using the below libraries.

Tools/Framework/Libraries

  • JDK 13
  • Maven/Gradle - build tool
  • TestNG - test runner
  • Cucumber - BDD/Gherkin style feature files
  • Rest assured - Rest api verification library

Run the tests using maven

mvn clean verify -Dcucumber.options="--tags @user"

Run the tests using gradle

./graldew clean testExec -Dcucumber.options="--tags @user"

BDD (Feature file / Step definition)

BDD requires a feature file to invoke the step definitions:

  • Create the scenarios in feature file as per the requirements, so each step in feature file has to match a step definition in class file;
  • Following the BDD practices for coding;
  • Using the special annotation like "@Before" which is the first method to run for each scenario. Moreover, this is the right place to set up the URI (endpoint) which will be called by HTTP request;

REST API

  • This project is aimed at calling Jsonplaceholder' API to validate the Users. This is written in a feature file using Cucumber.
  • Each line of the scenario is tied to backend code that actually executes the line (step).

Verify JSON GET Request

Testing a simple response containing some JSON data.

{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "[email protected]",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
    "name": "Romaguera-Crona",
    "catchPhrase": "Multi-layered client-server neural-net",
    "bs": "harness real-time e-markets"
  }
}
  • Status Code: 200 OK

Request not found

{
  
}
  • Status Code: 404 Not Found

JSON schema validation

  • Th main goal is to ensure that the JSON format is correct as well as all data inside him. Therefore, it was created a json file (schema) and stored it in "resource" file in the package project. In the source-code is validated by the statement 'body(matchesJsonSchemaInClasspath("user-schema.json"))';

How it is done (source-code)

Passing a userId by parameter in feature file and calling GET request

  • when().get("/" + userId)

Print in console the response

  • then().log().all();

Check the status code, so the expected status is passed by parameter in feature file

  • then().statusCode(Integer.parseInt(statusCode));

Check body response - In this case, the required format is JSON

  • then().assertThat().contentType(ContentType.JSON).and().body(matchesJsonSchemaInClasspath("schema-json.json"));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.