How to add headers or parameters to an HTTP reques

2019-04-28 18:05发布

问题:

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:

  • Add headers to an HTTP request passed by the driver.
  • Add parameters to such a request, as if the driver got his target URL after submitting a form.

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.

回答1:

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.