8.3 C
New York
Tuesday, March 25, 2025

Methods to modify HTTP request header in Selenium WebDriver with Java? – Software program High quality Assurance & Testing Stack Alternate


With the options already mentioned above essentially the most dependable one is utilizing Browsermob-Proxy

However whereas working with the distant grid machine, Browsermob-proxy is not actually useful.

That is how I fastened the issue in my case. Hopefully, could be useful for anybody with an analogous setup.

  1. Add the ModHeader extension to the chrome browser

Methods to obtain the Modheader? Hyperlink

ChromeOptions choices = new ChromeOptions();
choices.addExtensions(new File(C://Downloads//modheader//modheader.crx));

// Set the Desired capabilities 
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, choices);

// Instantiate the chrome driver with capabilities
WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), choices);
  1. Go to the browser extensions and seize the Native Storage context ID of the ModHeader

Capture ID from ModHeader

  1. Navigate to the URL of the ModHeader to set the Native Storage Context

.

// set the context on the extension so the localStorage could be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");

The place `idgpnmonknjnojddfkpgkljpfnnfcklj` is the worth captured from the Step# 2
  1. Now add the headers to the request utilizing Javascript

.

   ((Javascript)driver).executeScript(
         "localStorage.setItem('profiles', JSON.stringify([{  title: 'Selenium', hideComment: true, appendMode: '', 
             headers: [                        
               {enabled: true, name: 'token-1', value: 'value-1', comment: ''},
               {enabled: true, name: 'token-2', value: 'value-2', comment: ''}
             ],                          
             respHeaders: [],
             filters: []
          }]));");

The place token-1, value-1, token-2, value-2 are the request headers and values which might be to be added.

  1. Now navigate to the required web-application.

    driver.get("your-desired-website");

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles