0

I'm doing an assignment for school :

Requirement :

  • Create a search ("s) for the items containing "Fantabulous"
  • Verify that the movie with id "tt7713068" is in the list
  • Use a json path to generate a list of movie ID's and loop over it to search for the movie with the correct ID.

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/"

Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29

1 Answers1

1
  • 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

Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29