0

All "long_name" value for all index of "address_Components" is not display. When using the path: path("results[0].address_components[].long_name") then test case is failed. I want to display all "long_name" value given in response. Please suggest me.

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Chicago",
               "short_name" : "Chicago",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Cook County",
               "short_name" : "Cook County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Illinois",
               "short_name" : "IL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Chicago, IL, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 42.023131,
                  "lng" : -87.52366099999999
               },
               "southwest" : {
                  "lat" : 41.6443349,
                  "lng" : -87.9402669
               }
            },
            "location" : {
               "lat" : 41.8781136,
               "lng" : -87.6297982
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 42.023131,
                  "lng" : -87.52404399999999
               },
               "southwest" : {
                  "lat" : 41.6443349,
                  "lng" : -87.9402669
               }
            }
         },
         "place_id" : "ChIJ7cv00DwsDogRAMDACa2m4K8",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

I am using selenium (Maven TestNG):

 @Test
 public void test_08(){
      String[] arrayList = {"chicago"};
      for(int i=0; i<arrayList.length; i++){
     Response resp = given().
              parameter("address", arrayList[i]).
              when().
              get("http://maps.googleapis.com/maps/api/geocode/json");
     Assert.assertEquals(resp.getStatusCode(), 200);
     String respReport = resp.
              then().
              contentType(ContentType.JSON).
              extract().
              path("results[0].address_components[0].long_name");
     System.out.println("Logn Name: "+respReport+);
      }
 }

OUT PUT:

Logn Name: Chicago

PASSED: test_08

S. jaiswal
  • 33
  • 6

1 Answers1

0

It is because you don't give an index to components as well which is also a json array! So this one passes because you are giving an index to components:

path("results[0].address_components[0].long_name");

Whilst in your bold text at the top of your description you don't.

Now for iterating through the various elements of address_componenents array all you need to do is find the length of the address_components array and then increase the index from zero until < length.of the json array. Something like this:

String jsonObjectAsString = jsonObjectFromYourResponse.toString();  

public void retrieveAddressComponenets(String jsonObjectAsString){      

    JsonElement jelement = new JsonParser().parse(jsonObjectAsString);      

    JsonObject  jobject = jelement.getAsJsonObject();       
    JsonArray jArray = jobject.getAsJsonArray("address_components");

            int i=0;
            while(i<jArray.length()){ 
            jelement = new JsonParser().parse(jarray.get(i).toString());                 
            System.out.println("Logn Name: "+respReport);   
            i++;
            }           
}

Here is how your json object looks like (currently it has 4 entries you want to retrieve) enter image description here

Hope this helps to resolve! Best of luck!

Xwris Stoixeia
  • 1,831
  • 21
  • 22