0

I am able to modify http request header using BrowserMobProxy, the same way explained

https://sqa.stackexchange.com/a/37318/9043

But, the problem I am having at the moment is, that I am executing my scenarios on dev/local/playpen environment (environment before System integration testing). And to open the website on this environment needs proxy to set.

When I set my proxy then it fails to modify header and apply the proxy. When I comment the proxy part then it easily modifies the header.

BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
proxy.addRequestFilter((request, contents, messageInfo)->{
 request.headers().add("some-header-attribute", "RandomeValue");
 System.out.println(request.headers().entries().toString());
 return null;
});
String _host = Utils.getConfigValue("proxy.host");
String _port = Utils.getConfigValue("proxy.port");
seleniumProxy.setProxyType(Proxy.ProxyType.MANUAL);
seleniumProxy.setHttpProxy(_host + ":" + _port);
seleniumProxy.setSslProxy(_host + ":" + _port);
seleniumProxy.setFtpProxy(_host + ":" + _port);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
String proxyOption = "--proxy-server=" + seleniumProxy.getHttpProxy();
options.addArguments(proxyOption);
asked Jul 4, 2019 at 22:36

1 Answer 1

0

Browsermob-Proxy is a reliable solution, But while working with the remote grid machine, Browsermob-proxy isn't really helpful. Alternatively, I found this as a working solution for my setup.

Hopefully, it will be useful for someone with a similar setup.

  1. Add the ModHeader extension to the chrome browser

How to download the Modheader? Link

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(C://Downloads//modheader//modheader.crx));
// Set the Desired capabilities 
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
// Instantiate the chrome driver with capabilities
WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), options);
  1. Go to the browser extensions and capture the Local Storage context ID of the ModHeader

Capture ID from ModHeader

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

.

// set the context on the extension so the localStorage can be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");
Where `idgpnmonknjnojddfkpgkljpfnnfcklj` is the value captured from the Step# 2
  1. Now add the headers to the request using 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: []
 }]));");

Where token-1, value-1, token-2, value-2 are the request headers and values that are to be added.

  1. Now navigate to the required web-application.

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

answered Jun 17, 2020 at 14:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.