Set Request Header

Set Request Header #

def set_request_header(
  driver: TestUIDriver,
  header: str,
  value: str or None = None
) -> None

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

To reset request header to its default value, pass None as the new header value.
Setting value to empty string will actually update request header value as an empty string.
def test(driver):
    # Example of setting custom request header value

    # Open page and create screenshot with default request header
    driver.navigate_to("http://www.xhaus.com/headers")
    e(driver, "css", "body").wait_until_visible(seconds=10)
    driver.save_screenshot("default.png")

    set_request_header(driver, "Keep-Alive", "timeout=5, max=1000")

    # Wait for page to load and create screenshot
    driver.get_driver().refresh()
    e(driver, "css", "body").wait_until_visible(seconds=10)

    driver.save_screenshot("custom.png")

    # Reset request header
    set_request_header(driver, "Keep-Alive", None)

    # Wait for page to load and create screenshot
    driver.get_driver().refresh()
    e(driver, "css", "body").wait_until_visible(seconds=10)

    driver.save_screenshot("reset.png")