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("https://manytools.org/http-html-text/http-request-headers/");
    E(byCssSelector(".middlecol tbody"))
        .waitFor(10).untilIsVisible().scrollTo().view("");
    E(byCssSelector("body")).saveScreenshot("default_headers.png");
    // Set custom request header, reload the page and create screenshot
    setRequestHeader("Accept-Language", "lv-LV");
    getSelenideDriver().navigate().refresh();
    E(byCssSelector(".middlecol tbody"))
        .waitFor(10).untilIsVisible().scrollTo().view("");
    E(byCssSelector("body")).saveScreenshot("custom_headers.png");
    // Reset request header, reload the page and create screenshot
    setRequestHeader("Accept-Language", null);
    getSelenideDriver().navigate().refresh();
    E(byCssSelector(".middlecol tbody"))
        .waitFor(10).untilIsVisible().scrollTo().view("");
    E(byCssSelector("body")).saveScreenshot("reset_headers.png");
}