3

In Selenium using Java, with Chromedriver i am trying to get the response body of one particular HTTPS Request. I am using LogEntries, but i get all information from the logs, except the Response Body. Is there any way to get the Response Body?

        System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\webdrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(ChromeOptions.CAPABILITY, options);

        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
        options.setCapability("goog:loggingPrefs", logPrefs);
        ChromeDriver driver = new ChromeDriver(options);
        System.out.println("Navigate to " + url);
        driver.navigate().to(url);

        LogEntries logs = driver.manage().logs().get("performance");

        for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
            LogEntry entry = it.next();
            try {
                JSONObject json = new JSONObject(entry.getMessage());
                JSONObject message = json.getJSONObject("message");
                String method = message.getString("method");
                if (method != null && "Network.responseReceived".equals(method)) {
                    method = message.getString("method");
                    JSONObject params = message.getJSONObject("params");
                    JSONObject response = params.getJSONObject("response");
                    String messageUrl = response.getString("url");
                    if (messageUrl.equals("https://pay.sandbox.realexpayments.com/api/auth"))
                        System.out.println(json.toString());
                }
         }

And i get this JSON Response, which has no Response Body

{ 
   "webview":"221612BC1DF99CA155A0301EEA4FBDAA",
   "message":{ 
      "method":"Network.responseReceived",
      "params":{ 
         "frameId":"221612BC1DF99CA155A0301EEA4FBDAA",
         "requestId":"46320.53",
         "response":{ 
            "headers":{ 
               "Keep-Alive":"timeout=20",
               "Transfer-Encoding":"chunked",
               "Server":"Apache Tomcat",
               "Connection":"keep-alive",
               "X-CONTENT-TYPE-OPTIONS":"nosniff",
               "X-Application-Context":"HPPAzure:Azure:8080",
               "Date":"Fri, 31 Jan 2020 12:27:30 GMT",
               "Content-Type":"application/json;charset=UTF-8",
               "Request-Context":"appId=cid-v1:d078d25d-db86-42cf-88e7-0d8f9ef12ee7"
            },
            "securityDetails":{ 
               "cipher":"AES_256_GCM",
               "signedCertificateTimestampList":[ 
                  { 
                     "signatureData":"3044022056068FEDDA9F229659822AC94F5D35E8BEA1B99C1E4AA4681ED61604DF1AC033022064CFED8EA8FB473AEF690DF63F79FE693D4DF1ED612F09FA4C7A715A6952279E",
                     "logDescription":"Google 'Pilot' log",
                     "origin":"Embedded in certificate",
                     "logId":"A4B90990B418581487BB13A2CC67700A3C359804F91BDFB8E377CD0EC80DDC10",
                     "hashAlgorithm":"SHA-256",
                     "signatureAlgorithm":"ECDSA",
                     "status":"Verified",
                     "timestamp":1.537278661211E12
                  },
                  { 
                     "signatureData":"3046022100A23157B9BF476E1602EA6654620173A78BC47F445E632FDEA433C6B8853D31E1022100F0FFD6115606BAE5FFF0EDDA34DC28BA392DCDC84AC5B1AF8B3D9620BE2850E5",
                     "logDescription":"DigiCert Log Server",
                     "origin":"Embedded in certificate",
                     "logId":"5614069A2FD7C2ECD3F5E1BD44B23EC74676B9BC99115CC0EF949855D689D0DD",
                     "hashAlgorithm":"SHA-256",
                     "signatureAlgorithm":"ECDSA",
                     "status":"Verified",
                     "timestamp":1.537278661319E12
                  },
                  { 
                     "signatureData":"3044022060A1856FF22896D4388E3643132224C9371362F0E5CC0E92F9E9092ABFA8B61A02201433B88051A0E0B1D6814AA1D8CE5E1B45EDB404AE32546749C31947652AB686",
                     "logDescription":"Google 'Skydiver' log",
                     "origin":"Embedded in certificate",
                     "logId":"BBD9DFBC1F8A71B593942397AA927B473857950AAB52E81A909664368E1ED185",
                     "hashAlgorithm":"SHA-256",
                     "signatureAlgorithm":"ECDSA",
                     "status":"Verified",
                     "timestamp":1.53727866125E12
                  }
               ],
               "protocol":"TLS 1.2",
               "certificateId":0,
               "certificateTransparencyCompliance":"compliant",
               "sanList":[ 
                  "pay.sandbox.realexpayments.com"
               ],
               "validFrom":1537228800,
               "issuer":"Thawte EV RSA CA 2018",
               "keyExchange":"ECDHE_RSA",
               "keyExchangeGroup":"P-256",
               "subjectName":"pay.sandbox.realexpayments.com",
               "validTo":1606132800
            },
            "connectionReused":true,
            "fromPrefetchCache":false,
            "timing":{  },
            "encodedDataLength":344,
            "remotePort":443,
            "mimeType":"application/json",
            "securityState":"secure",
            "url":"https://pay.sandbox.realexpayments.com/api/auth",
            "protocol":"http/1.1",
            "fromDiskCache":false,
            "fromServiceWorker":false,
            "remoteIPAddress":"52.155.173.65",
            "statusText":"",
            "connectionId":70,
            "status":200
         },
         "loaderId":"0CB9CB8372A2D2D60E34F31ADDA813D5",
         "type":"XHR",
         "timestamp":3810237.716388
      }
   }
}

Am i missing something, or maybe there is some other way to implement this? Thanks in advance

2 Answers2

0

Try something like below import io.restassured.RestAssured;

public class HttpResponseCode {

public int httpResponseCodeViaGet(String url) {

        return RestAssured.get(url).statusCode();

}

public int httpResponseCodeViaPost(String url) {

    return RestAssured.post(url).statusCode();

}

public static void main(String args[]) {

    new HttpResponseCode().httpResponseCodeViaGet("http://www.google.com");

}

}

Chris
  • 236
  • 4
  • 14
  • Actually, i just wanna record the call which is not made by me, but from the application itself. With restAssured i would be able to get the response if i make that call, but unfortunately that is not my case. – Nenad Mirkoski Feb 03 '20 at 08:53
0

It was left out intentionally, so you will not be able to get it.

Ref: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2267&q=performance%20response%20body&can=1

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
melofelo
  • 49
  • 6