Saturday, March 15, 2025

automated testing – Unable to set proxy with BrowserMobProxy whereas modifying http header request in Selenium with Java – Software program High quality Assurance & Testing Stack Change


Browsermob-Proxy is a dependable resolution, However whereas working with the distant grid machine, Browsermob-proxy is not actually useful. Alternatively, I discovered this as a working resolution for my setup.

Hopefully, it is going to be helpful for somebody with the same setup.

  1. Add the ModHeader extension to the chrome browser

Find out how 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

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

.

// set the context on the extension so the localStorage may 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 can 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

PHP Code Snippets Powered By : XYZScripts.com