top of page

Rest API / Json

דוגמה שלי

https://1drv.ms/w/s!ArDkuBl5lvzTgyWvKckfle82mDKy?e=eiPaOb

http://restservicestesting.blogspot.com/

https://artoftesting.com/automationTesting/restAPIAutomationGetRequest.html

Mevan pom xml

https://mvnrepository.com/artifact/io.rest-assured/rest-assured/3.3.0

package REST_assured.REST_assured;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.http.Method;
import io.restassured.parsing.Parser;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.given;

public class AppTest 
{
//    @BeforeClass
//    public void setup() {
//        RestAssured.baseURI = "https://api.github.com";
//        RestAssured.port = 443;
//    }
//    
//    @Test
//    public void whenMeasureResponseTime_thenOK() {
//        Response response = RestAssured.get("/users/eugenp");
//        long timeInMS = response.time();
//        long timeInS = response.timeIn(TimeUnit.SECONDS);
//         
//        assertEquals(timeInS, timeInMS/1000);
//    }
//    
    
    @Test
     public void GetWeatherDetails()
     {   
        try {
     // Specify the base URL to the RESTful web service
     RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
     
     // Get the RequestSpecification of the request that you want to sent
     // to the server. The server is specified by the BaseURI that we have
     // specified in the above step.
     RequestSpecification httpRequest = RestAssured.given();
     
     // Make a request to the server by specifying the method Type and the method URL.
     // This will return the Response from the server. Store the response in a variable.
     Response response = httpRequest.request(Method.GET, "/Hyderabad");
    
     // Now let us print the body of the message to see what response
     // we have recieved from the server
     String responseBody = response.getBody().asString();
     System.out.println("Response Body is =>  " + responseBody);
     
     
    // Get the status code from the Response. In case of 
     // a successfull interaction with the web service, we
     // should get a status code of 200.
     int statusCode = response.getStatusCode();
     System.out.println("statusCode=>  " + statusCode);
     
     
     // Get all the headers. Return value is of type Headers.
     // Headers class implements Iterable interface, hence we
     // can apply an advance for loop to go through all Headers
     // as shown in the code below
     Headers allHeaders = response.headers();
     
     // Iterate over all the Headers
     for(Header header : allHeaders)
     {
     System.out.println("Key: " + header.getName() + " Value: " + header.getValue());
     }
     
     
     
     
     
     String serverType =  response.getHeader("Server");
    // Assert.assertEquals(serverType /* actual value */, "nginx/1.12.1" /* expected value */);
     System.out.println("Server Value=>  " + serverType);
     
     
     
    // First get the JsonPath object instance from the Response interface
     JsonPath jsonPathEvaluator = response.jsonPath();
     
     
    // Let us print the city variable to see what we got
     System.out.println("City received from Response " + jsonPathEvaluator.get("City"));
     
     // Print the temperature node
     System.out.println("Temperature received from Response " + jsonPathEvaluator.get("Temperature"));
     
     // Print the humidity node
     System.out.println("Humidity received from Response " + jsonPathEvaluator.get("Humidity"));
     
     // Print weather description
     System.out.println("Weather description received from Response " + jsonPathEvaluator.get("Weather"));
     
     // Print Wind Speed
     System.out.println("City received from Response " + jsonPathEvaluator.get("WindSpeed"));
     

     
     // Print Wind Direction Degree
     System.out.println("City received from Response " + jsonPathEvaluator.get("WindDirectionDegree"));
     
    
     System.out.println("????????????????????????????????????????????????????????????????????????/n/n/n/n");
     System.out.println("     Parsing JSON Arrays and Lists   ");
     
     response = doGetRequest("https://jsonplaceholder.typicode.com/users"); 
     
     List<String> jsonResponse = response.jsonPath().getList("$");

     System.out.println(jsonResponse.size());
    
     String usernames = response.jsonPath().getString("username");
     System.out.println(usernames);
     usernames = response.jsonPath().getString("username["+0+"]");
     System.out.println(usernames);
     
     List<String> jsonResponse4 = response.jsonPath().getList("username");
     System.out.println(jsonResponse4.get(0));
     
     /////  see cpsolation
//     "id": 3,
//     "name": "Clementine Bauch",
//     "username": "Samantha",
//     "email": "Nathan@yesenia.net",
//     "
address": {
//       "street": "Douglas Extension",
//       "suite": "Suite 847",
//       "city": "McKenziehaven",
//       "zipcode": "59590-4157",
//       "
geo": {
//         "
lat": "-68.6102",
//         "lng": "-47.0653"
//       }
     
     
     
     
     jsonResponse4 = response.jsonPath().getList("
address.geo.lat");
     
     
     
     
     
     System.out.println("????????????????????????????????????????????????????????????????????????/n/n/n/n");
     System.out.println("     Parsing JSON ArrayList and HashMap   ");
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
     response = doGetRequest("https://jsonplaceholder.typicode.com/users/1");

     
     
     Map<String, String> company = response.jsonPath().getMap("company");
     System.out.println(company.get("name"));
     
     
     response = doGetRequest("https://jsonplaceholder.typicode.com/users/");

     company = response.jsonPath().getMap("company[0]");
     System.out.println(company.get("name"));
     
     response = doGetRequest("https://jsonplaceholder.typicode.com/users/");

     List<Map<String, String>> companies = response.jsonPath().getList("company");
     System.out.println(companies.get(0).get("name"));
        }
        catch(Exception e)
        {
             System.out.println(e);    
        }
     }
    
    //  Online REST API
    ///http://dummy.restapiexample.com/
    
    // https://www.toolsqa.com/rest-assured/put-request-using-rest-assured/
    
    
    public static Response doGetRequest(String endpoint) {
        try {
        RestAssured.defaultParser = Parser.JSON;

        return
            given().headers("Content-Type", ContentType.JSON, "Accept", ContentType.JSON).
                when().get(endpoint).
                then().contentType(ContentType.JSON).extract().response();
        }
        catch(Exception e)
        {
             System.out.println(e);
             return null;
        }
    }
}

 

<dependency>

        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>3.3.0</version>
        <scope>test</scope>

        </dependency>


        
    }

How to Parse JSON Response with REST-assured

Selenium - izselenium aoutomation tool

054-8059589

  • White Facebook Icon
  • White Twitter Icon
  • White Google+ Icon
  • White YouTube Icon

© 2017 by izselenium

bottom of page