user12142134
user12142134

Reputation: 1

API Testing - RestAssured - Looping

I'm doing an assignment for school :

Requirement :

This is what I have:

//@Test
public void search_for_movies_on_string_and_validate_one_of_the_results() {
    Response response = given().
            param("apikey", apiKey).
            param("s", "Fantabulous").
        when().get(baseURI).
            then().extract().response();

        JsonPath jsonPath = response.jsonPath();

        List<String> idList = jsonPath.getList("Search.imdbID");
        Assert.assertTrue(idList.contains("tt7713068"));
}

How can I loop over a list to search for the movie with the correct ID?

apiKey = "7548cb76"

baseURI = "http://www.omdbapi.com/"

Upvotes: 0

Views: 1921

Answers (1)

Wilfred Clement
Wilfred Clement

Reputation: 2774

  • Count the size of the list that is returned
  • Loop over starting with 0 until the end of the size
  • Search for all ID's across the response that match your requirement "tt7713068", If it does then print the output

    RestAssured.baseURI = "http://www.omdbapi.com";
    Response response = given().param("apikey", "7548cb76").param("s", "Fantabulous").when().get(baseURI).then().extract().response();
    
    JsonPath jsonPath = response.jsonPath();
    
    int count = jsonPath.getInt("Search.size()");
    
    String id = "tt1634278";
    
    for(int i=0;i<count;i++)
    {
        String search = jsonPath.getString("Search["+i+"].imdbID");
        if(search.equalsIgnoreCase(id))
        {
            String output = jsonPath.getString("Search["+i+"].Title");
            System.out.println("The ID "+id+" is present in the list and the name of the movie is "+output+"");
        }
    }
    

Output :

The ID tt7713068 is present in the list and the name of the movie is Birds of Prey: And the Fantabulous Emancipation of One Harley Quinn

Upvotes: 1

Related Questions