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: TestUIDriver):
    # Example of setting custom request header value

    def move_to_element(driver, element):
        # Moves element in view.
        # param: driver: TestUIDriver
        # param: element: TestUIElement
        actions = driver.actions()
        actions.move_to_element(element.get_element()).perform()

    # Open page and create screenshot with default request header
    driver.navigate_to(
        "https://manytools.org/http-html-text/http-request-headers/"
    )
    e(driver, "css", "body").wait_until_visible()
    move_to_element(driver, e(
        driver, "xpath", "//td[text() = 'Accept']"
    ))
    driver.save_screenshot("default_headers.png")

    # Set custom request header, reload the page and create screenshot
    set_request_header(driver, "Accept-Language", "lv-LV")
    driver.get_driver().refresh()
    e(driver, "css", "body").wait_until_visible()
    move_to_element(driver, e(
        driver, "xpath", "//td[text() = 'Accept']"
    ))
    driver.save_screenshot("custom_headers.png")

    # Reset request header, reload the page and create screenshot
    set_request_header(driver, "Accept-Language", None)
    driver.get_driver().refresh()
    e(driver, "css", "body").wait_until_visible()
    move_to_element(driver, e(
        driver, "xpath", "//td[text() = 'Accept']"
    ))
    driver.save_screenshot("reset_headers.png")