Set User-Agent

Set User-Agent #

setUserAgent(String userAgent);

Changes User-Agent header for outgoing requests and navigator.userAgent.

To reset User-Agent to its default value, pass null as the new value.
Setting User-Agent to an empty string will actually update the User-Agent to empty string.
public void testUIWithLoadero() {
  // Example of setting custom user-agent

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

  // Set custom User-Agent value
  setUserAgent("Custom User Agent");

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

  // Wait for page to load and create screenshot with changed User-Agent
  E(byCssSelector("body"))
    .waitFor(10).untilIsVisible()
    .saveScreenshot("UAChanged.png");

  // Set User-Agent header to empty string value
  setUserAgent("");

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

  // Wait for page to load and create screenshot with empty User-Agent
  E(byCssSelector("body"))
    .waitFor(10).untilIsVisible()
    .saveScreenshot("UAEmpty.png");

  // Reset User-Agent header to original value
  setUserAgent(null);

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

  // Wait for page to load and create screenshot with reset User-Agent
  E(byCssSelector("body"))
    .waitFor(10).untilIsVisible()
    .saveScreenshot("UAReset.png");
}
User-Agent will revert to original value in browser built-in pages and WebRTC dump.