0

I am using selenium and java with my automation tests. I am forced to send authorization token in my tests so I used BrowserMobProxy library. Here is my setUp in @BeforeClass:

BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.addHeader("Authorization",tokenIDdev2);
proxy.start();

Next, this that I need to do is opening my browser by special proxy address, I've found this kind of setting up proxy address:

Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
DesiredCapabilities capabilities = new  DesiredCapabilities();
seleniumProxy.setHttpProxy(PROXY_ADDRESS);
seleniumProxy.setSslProxy(PROXY_ADDRESS);
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

// Setting up Proxy for chrome
ChromeOptions opts = new ChromeOptions();
opts.merge(capabilities);
driver = new ChromeDriver(opts);

Problem is that when I am connecting by proxy which was set up in the method above, requests are not sending with the header that I specified in the first part.

Do you have any ideas that I am doing wrong, or maybe there is another way to handle this situation?

Thanks!

Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31

1 Answers1

0

For some reason, I think I never managed to make .addHeader(); method work either.

But I did it in another way:

    String authorizationKey = "Authorization";
    browserMobProxyServer.addRequestFilter((request, content, messageInfo)->{
        if (request.headers().contains(authorizationKey)) {
            request.headers().remove(authorizationKey);
        }
        request.headers().add(authorizationKey, tokenIDdev2);
        return null;
    });

You may remove the if part if you are 100% sure that the "Authorization" header is never present.

Batou
  • 98
  • 1
  • 8