Alex Rivera | Logout

The best way to inspect HTTP response headers with Selenium

Asked 2009-03-24T20:57:39.577
14

I need the best way to inspect HTTP response headers with Selenium. I looked around the Selenium docs and didn't see any straightforward way to do it. Help is highly appreciated.

Edit
Report

2 Answers

3

I would not use Selenium for this type of test and suggest that you solve a variety of testing issues with different tools. what we do is:

  • Use unit tests to test code: methods and classes

  • Integration tests to test how application components hang together

  • A Simple functional test framework like Canoo WebTest (or some equivalent) to assert things like Http cache headers, basic page structure, simple redirection and cookie setting / values

  • Bespoke tests to ensure validity of pages to W3C standards

  • JSunit to test Javascript classes and methods we created

  • Selenium to test UI functionality/behaviour and the integration of Javascript into those pages

Its worth spending time breaking out the responsibility of testing different aspects of the system using these different tools since using only Selenium can cause issues:

  • The bigger the suite, the slower they run. Indeed Selenium is inherently slower compared to the other tools mentioned
  • It handles behaviour/functional testing well but nevertheless XPaths can be brittle and may require increasing amounts of time and effort to maintain
  • Usually requires you setup 'as-if-real-life' data with your app to step through user scenarios (which can be messy and take a lot of time)

There are also some techniques - which you may or may not have come across - which you can use to make your Selenium tests more resilient.

answered 2009-03-24T22:32:02.160
0

Read the session cookies from Selenium and then use a real HTTP library outside of Selenium to request the specific page.

Here is the Python code:

# get session cookies from Selenium
cookies = {}
for s_cookie in self.selenium.get_cookies():
    cookies[s_cookie["name"]]=s_cookie["value"]

# request the pdf using the cookies:
response = requests.get(self.full_url('/vms/business_unit/2002/operational_unit/200202/guest/40/bill/pdf/'), cookies = cookies)
self.assertEqual(response.headers["content-type"], "application/pdf")
answered 2013-05-02T10:24:34.443

Your Answer