Can we validate API response with app result?

I have one requirement. I am automating one android app where i wanted to compare the APP result with API response. How can i achieve this scenario? Please help me on this.

Add OkHttp jar or anything similar which will do GET, POST etc.
Sample is given here http://square.github.io/okhttp/.
Make a generic code for that when you are running appium code import that code to the main code pass API URL and headers and get response same like your app, store it in JSON array use JsonPath(https://github.com/json-path/JsonPath) get the data what you need and compare both.

Thank you @Prajith_KP for your valuable reply. I will check this. Do you have any document or any site for reference to achieve this for reference?

1 Like

Don’t have any reference links if you want you can refer below sample which i roughly did
Below code is for GET method

function will accept URL and headers and return response

String[] get(String url, Map<String,String> headers) throws IOException {
client = new OkHttpClient.Builder().connectTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS)
.followRedirects(true).followSslRedirects(true).
build();
response_received= new String[3];
Request.Builder builder = new Request.Builder()
.url(url).header(“User-Agent”, “Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19”);
headers.forEach((key,value)->builder.addHeader(key, value));
Request request = builder.build();
System.out.println(ExecutionLogGenerator.Logger(classname)+"–> HTTP GET “+ url);
try (Response response = client.newCall(request).execute()){
System.out.println(ExecutionLogGenerator.Logger(classname)+”<-- HTTP “+response.code()+” “+url+” [Success : “+ response.isSuccessful()+”]"+" [Message :"+ response.message()+"]");
response_received[0]= String.valueOf(response.code());
response_received[1]= response.message();
response_received[2]= response.body().string();
return response_received;
}
}

but comparison its based on requirement you can go through the samples given here
https://github.com/json-path/JsonPath you will find something suitable for your requirement. :slightly_smiling_face:

Thank you very much. :slight_smile:

1 Like

you’re welcome :grimacing:

Hi @Prajith_KP Does OkHttp works for IOS as well? Or Do you know anything similar for IOS?