Skip to content
Advertisement

How To Use Response value In Another Method

I am working on restassured and here is my 2 methods. I want to use the albumId returned from the AlbumList method in the other method

public void AlbumList() {
  
    Response response1 = given().spec(url).queryParam("page", 0).queryParam("size", 100)
            .queryParam("sortBy", "createdDate").queryParam("contentType", "album/photo")
            .queryParam("sortOrder", "ASC")
            .header("Content-type", "application/json")
            .header("Accept", "application/json")
            .header("X-Auth-Token", payload.userAuth())
            .when().get("/album")
            .then().assertThat().statusCode(200).extract().response();


    Assert.assertEquals(response1.jsonPath().get("[4].label"), "TLOTR");

    JsonPath js = new JsonPath(response1.asString());

    int count = js.getInt("size()");

    // Response check part

    for (int i = 0; i < count; i++) {

        assertEqual(js, i, "createdDate", AlbumAttributes.actual_createdDate());
        assertEqual(js, i, "lastModifiedDate", AlbumAttributes.actual_modifiedDate());
        assertEqual(js, i, "uuid", AlbumAttributes.actual_uuid());


        if (js.get("[" + i + "].coverPhoto") != null) {
            String d = response1.jsonPath().get("[" + i + "].coverPhoto.tempDownloadURL").toString();
            Assert.assertTrue(d.matches(AlbumAttributes.actual_temp_url()));
            System.out.println(js.get("[" + i + "].coverPhoto.tempDownloadURL").toString() + " is equalent to : " + AlbumAttributes.actual_temp_url());
        }
        if (js.get("[" + i + "].coverPhoto.metadata['Thumbnail-Large']") != null) {
            String e = response1.jsonPath().get("[" + i + "].coverPhoto.metadata['Thumbnail-Large']").toString();
            Assert.assertTrue(e.matches(AlbumAttributes.actual_metaData_url()));
            System.out.println(js.get("[" + i + "].coverPhoto.metadata['Thumbnail-Large']").toString() + " is equalent to : " + AlbumAttributes.actual_metaData_url());
        }

    }

    String albumId = response1.jsonPath().get("[0].uuid").toString();
    String albumId2 = response1.jsonPath().get("[1].uuid").toString();

    
}

I know these are void and doesnt return anything but idk how to use it. Bye the these methods are in the same class. Thanks in advance

public void AlbumDetails() {
Response response = given().queryParam("page", 0).queryParam("size", 100)
        .queryParam("sortBy", "createdDate").queryParam("sortOrder", "DESC")
        .header("Content-type", "application/json")
        .header("Accept", "application/json")
        .header("X-Auth-Token", payload.userAuth())
        .when().get("/album/" + albumId)
        .then().assertThat().statusCode(200).extract().response();

// Response check part

Assert.assertEquals("[]", response.jsonPath().get("fileList").toString());
Assert.assertEquals("album/photo", response.jsonPath().get("contentType").toString());
Assert.assertEquals("false", response.jsonPath().get("readOnly").toString());

Assert.assertTrue(response.jsonPath().get("createdDate").toString().matches(AlbumAttributes.actual_createdDate()));
Assert.assertTrue(response.jsonPath().get("lastModifiedDate").toString().matches(AlbumAttributes.actual_modifiedDate()));
Assert.assertTrue(response.jsonPath().get("uuid").toString().matches(AlbumAttributes.actual_uuid()));

System.out.println("Album Details Response Test PASS");
long albumDetails_time = response.getTime();
System.out.println("tAlbum Detail's Api response time is : " + albumDetails_time);

}

Advertisement

Answer

The easiest way is to change the return type of the AlbumList() method from void to String:

public String AlbumList() {

And in AlbumDetails() method we should change:

.when().get("/album/" + AlbumList())

Another option is to create instance variable albumId, not locally in AlbumList() method, and use it:

public class SomeClass {

    public String albumId;

    public void AlbumList() {
        ...
        albumId = response1.jsonPath().get("[0].uuid").toString();
    }

    public void AlbumDetails() {
        ...
        .when().get("/album/" + albumId)
        ...
    }
}

P.S. Here are some more tips:

  1. due to clean code more correct name describes what these methods do, e.g. getAlbumList() or storeAlbumList() and smth similar for another method;
  2. for extracting jsonPath from response we can use: JsonPath js = given()....extract().jsonPath();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement