Suppose you have an end point/API that requires some headers to communicate to server,  these headers we can get after login into the web application.

How to get this headers, if I am doing API automation, where I won’t open browser.

With the help of Chrome headless browser, you can fetch the headers by set the logging preferences.

ChromeDriver supports performance logging, from which you can get events of domains “Timeline”, “Network”.

How to open Chrome headless browser:
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeOptions options=options = new ChromeOptions();
options.addArguments("--headless", "--disable-infobars", "--disable-extensions","--no-sandbox","--disable-dev-shm-usage");
options.setAcceptInsecureCerts(true);
How to set logging preferences:
LoggingPreferences logPrefs = new LoggingPreferences();
options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

you can get performance log entries as below

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

Each entry is a JSON string of the following structure:

{
    "webview": <originating WebView ID>,
    "message": { "method": "...", "params": { ... }} // DevTools message.
}

Iterate each entries to fetch the required headers.

Below is the JSON content and code to fetch the “authorization” and “xsrfToken” by iterate all log entries

{"message":
   {
    "method":"Network.requestWillBeSent",
    "params":{"documentURL":"https://xxxxxxxxxx.com/?redirectTo=",
    "frameId":"xxxxxx",
    "hasUserGesture":false,
    "loaderId":"xxxxxx",
    "redirectResponse":  
      { 
         "headers":{"alt-svc":"clear",
         "authorization":"xxxxxxxx,
         "content-length":"0",
         "content-type":"application/octet-stream",
         "set-cookie":"xxxxxxxxxxx",
         "xsrf-token":"xxxxxxxxxxxx"
      }
    ..
  }
}

 

for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
   LogEntry entry = it.next();
    try {
	logObject = new JSONObject(entry.getMessage());
	logObject = logObject.getJSONObject("message");
	logObject = logObject.getJSONObject("params");
	logObject = logObject.getJSONObject("redirectResponse");
	logObject = logObject.getJSONObject("headers");
	authorization = (String) logObject.get("authorization");
	xsrfToken = (String) logObject.get("xsrf-token");
    } 
    catch(JSONException e){
        continue;
    }
}

Here is the complete code to fetch authorization headers like cookie, authorization & token.

public class Config {
  static ChromeOptions options;
  static WebDriver driver;
  static WebElement element;
  static Cookie cookie;
  static LogEntries logs;
  static JSONObject logObject;
  static String xsrfToken;
  static String authorization;
  
  public static void generateCookie() {
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
    options = new ChromeOptions();
    options.addArguments("--headless", "--disable-infobars", "--disable-extensions","--no-sandbox","--disable-dev-shm-usage");
    options.setAcceptInsecureCerts(true);
    options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs); 
    driver = new ChromeDriver(options);
    driver.get("https://xxxxxxxxxxxxxxxxx.com/");
    element = driver.findElement(By.name("UserName"));
    element.sendKeys("xxxxxxx");
    element = driver.findElement(By.name("Password"));
    element.sendKeys("xxxxxxx");
    driver.findElement(By.xpath("//*[@id=\"submitButton\"]")).click();
    
    cookie = driver.manage().getCookieNamed("auth-cookie");

    logs = driver.manage().logs().get("performance");
    for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
	LogEntry entry = it.next();
        try {
	     logObject = new JSONObject(entry.getMessage());
	     logObject = logObject.getJSONObject("message");
             logObject = logObject.getJSONObject("params");
	     logObject = logObject.getJSONObject("redirectResponse");
    	     logObject = logObject.getJSONObject("headers");
    	     authorization = (String) logObject.get("authorization");
	     xsrfToken = (String) logObject.get("xsrf-token");
	}
        catch(JSONException e){
              continue;
        }
    }

  }

  public static Map<String,String> getResponseHeaders() {
     Map<String, String> responseHeadersMap = new HashMap<>();
     responseHeadersMap.put("authorization", authorization);
     responseHeadersMap.put("xsrfToken", xsrfToken);
     responseHeadersMap.put("cookie", cookie.toString());
     return responseHeadersMap;
  }
}

In the above post, we discussed how to fetch the headers  through headless browser for API automation.

React JS Automation Frameworks Insights
0