Set Request Header

Set Request Header #

setRequestHeader(headerName, headerValue);

This function allows updating the header values for any requests that browser sends out. The parameter headerName is the name of the request header to be changed. The parameter headerValue is used to set a new value for the specified header.

To reset request header to its default value, passnullas the new header value.
Setting headerValue to empty string will actually update request header value as an empty string.
public void testUIWithLoadero() {
  // Example of setting custom request header value

  // Open page and create screenshot with default request header
  open("http://www.xhaus.com/headers")
    .setElement(byCssSelector("body"))
    .waitFor(10).untilIsVisible()
    .saveScreenshot("default.png");

  // Set custom request header
  setRequestHeader("Keep-Alive", "timeout=5, max=1000");

  // Refresh page
  getSelenideDriver().navigate().refresh();

  // Wait for page to load and create screenshot
  E(byCssSelector("body"))
    .waitFor(10).untilIsVisible()
    .saveScreenshot("custom.png");

  // Reset request header
  setRequestHeader("Keep-Alive", null);

  // Refresh page
  getSelenideDriver().navigate().refresh();

  // Wait for page to load and create screenshot
  E(byCssSelector("body"))
    .waitFor(10).untilIsVisible()
    .saveScreenshot("reset.png");
}