Silver Quettier
Silver Quettier

Reputation: 2050

How to add headers or parameters to an HTTP request handled with Selenium Webdriver?

I'm using Selenium Webdriver for unit testing of a web application. It's used in JUnit tests Despite reading the available documentation extensively and searching around, I could not find a way to:

It would be possible to create a test web page with an appropriate form and have Webdriver bounce off it to get those parameters automatically, but this is quite an ugly hack. I would like to avoid it, especially for the sake of test atomicity. (This is unit testing.)

Before Wendriver, I was using Spring's MockHttpServletRequest and MockHttpServletResponse to do this, which worked like a charm, but I would like to use the power of Webdriver to assert the target page's contents.

Upvotes: 8

Views: 14009

Answers (2)

Praveen
Praveen

Reputation: 1485

Might be useful for others who are looking for a solution

This is how I fixed the problem in my case. Hopefully, might be helpful for anyone 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");

Upvotes: 1

niharika_neo
niharika_neo

Reputation: 8531

You can try evaluating browsermob-proxy. It helps in manipulating the headers. https://github.com/webmetrics/browsermob-proxy. Integrating with webdriver is simple. You just need to start the driver with the proxy values set.

Upvotes: 5

Related Questions