Optimizing Selenium tests with HTTP requests

23 Aug

I found a Selenium talk by Santi from SauceLabs interesting:




and wanted to add a comment/elaborate on one of the topics.

For optimizing Selenium tests by bypassing GUI for nonessential actions (e.g. test data/state setup, etc.), you don’t necessarily need:

  • special APIs
  • hidden test pages to do the state/data setup
  • or go through the database & memcache config route

Since we’re talking about the web applications space, the first approach one should have in mind is what other methods can I test the web app with without using GUI test tools or involving a developer to provide access hooks? Or perhaps phrased differently, how else would you load/performance test the web app w/o using Selenium (Grid)?

For the latter question, ask load test pros, and they’ll probably tell you to use a tool like JMeter. Consider how to use JMeter and going back to the former question, the answer would be to use HTTP requests (normally GETs and POSTs).

And for these HTTP requests, you’re simulating a user and making the same requests but without a browser/Selenium. This also offers the benefit of test coverage closer to what Selenium gives you since all that you’re missing is rendering of the page and javascript/AJAX by a browser, but all the client-server interactions is replicated by the HTTP requests. Therefore you don’t need special (REST) APIs created or need to involve a developer (unless you lack expertise to work with HTTP requests in code). All the APIs needed are already part of the web app / website itself. You just need to find out the API (i.e. HTTP requests). How to do that?

Step 1, check if there’s existing documentation already, saves you time and serves as useful reference. If none available, goto step 2, reverse engineer the web app from the API/HTTP level. To do this, you would use tools like the network analysis feature of browser developer tools (for Chrome/Safari) like Firebug, or tools like Fiddler, Wireshark, or a web proxy that logs HTTP requests. For IE, in addition to the IE developer tools, there’s also a handy tool called ieHttpHeaders.

With these tools you spy on the network traffic that occurs whenever you perform actions like click a link/button, submit a form, login, etc. You’ll see the actual HTTP (GET or POST) requests that were made, the data that was sent along with it, any associated HTTP headers and cookies (cookies are where the session state are stored), and the response from the server.

Once you make note of the requests you need to make, the type of data to pass along, the cookies you need to create/send or extract from response, you can replicate them back using code with the HTTP libraries offered for your programming language.

In addition, this works both ways in that you could first use HTTP code to set up session cookie and make configurations then extract cookie and pass to Selenium, or you could have Selenium start some work, then extract Selenium cookie(s) and pass to HTTP code to do some other work where it’s optimal over Selenium.

It’s some amount of work to provide good code/test examples, but you can find some basic examples at least on how to extract cookie from Selenium to then pass to HTTP code (say to do a file download):

Java example for WebDriver


PHP example for Selenium RC


and last, this article I wrote a while back is also a good example and code demo of how to spy for HTTP requests from browser session then convert the traces from the manual exploration into code that makes same HTTP requests, which can be adapted for Selenium and for other languages (original is in Perl):


15 Responses to “Optimizing Selenium tests with HTTP requests”

  1. Rob August 23, 2012 at 6:05 pm #

    That’s a very interesting idea. My only concern would be how to handle the brittleness of coding so close to the metal for what the parameters are and what the valid values are.

    Also, I’d be concerned about the time needed to discover these changes and debug when there are problems. Maybe there’s a way to use a PageObject-like model to define what you want to do with the page, and then automatically run the scenario and capture the underlying HTTP traffic?

    • autumnator August 24, 2012 at 5:33 am #

      Adapting PageObject model to this would be interesting. If what you’re referring to is to have a recorder capability to capture the needed HTTP traffic and perhaps generate the needed HTTP requests automatically, there’s already some ways to do it with or without PageObjects. You could run the unoptimized Selenium test with some Firebug tools enabled to capture network traffic as HAR files, or run the test through a proxy to capture as HAR file (or log the traffic in some format). You’d then only need to parse the traffic, analyzing it for what you want to convert to HTTP requests in code (as not all the traffic is needed like image requests and javascript, CSS file downloads from browser), but perhaps the proxy could have filtered those out for you as well. And one could write a parser script that will generate the HTTP requests as code for you. I posted link in the article showing how to do so with a parser in Perl for Perl.

      Alternatively, if you don’t have unoptimized Selenium script to run to generate the traffic, you could just work with the manual test case (that is planned for automation), and run that manually through a proxy or Firebug like tools to capture the traffic.

  2. Sachin October 12, 2012 at 5:27 am #

    Thanks a lot .. its was very informative …
    I have a problem ..
    I have a web app which has dynamic urls …
    I need to automate this web app and the aim to generate high workloads on server
    As of now i did it with selenium with multiple parallel browsers but the load generated on server is very very low … i think using a GUI browser is consuming more time ….
    Can i do it with JAVA – servlets .. if so any information on how to do that …

    Any help will be much appreciated ..

    Thank you

    • autumnator October 13, 2012 at 3:22 am #

      If your intent is to load test the web app & not really functional test it, then your best option is to use a tool like JMeter. You simply hit the URLs you need to hit (as HTTP GET like a browser, or HTTP POST like form submission), setting any needed cookies, HTTP headers, etc. Very good tool that I recommend. You won’t get the desired load using Selenium with multiple browsers and/or Grid. You can also choose to do it with Java code or any other code to load the web app, but JMeter is the simple option to go with.

      • Sachin October 16, 2012 at 8:20 am #

        Thanks for the info …. I want to generate load .. But the web app has Javascript which Jmeter couldn’t handle … Can i do with Java servlets .. Any info on how to do with JAVA …
        Thanks …

      • autumnator October 17, 2012 at 1:34 am #

        If you’re load testing browser rendering of Javascript, then I understand, but you only need to do that with one instance of Selenium while your site is under load with other tools. If you want to load test server, then you can ignore javascript completely. Javascript is client side. If the javascript makes HTTP requests to server via AJAX, you just need to capture those and make same request with JMeter to simulate the load. Capture and analyze with a proxy, Firebug, Fiddler, or Wireshark, etc.

      • Sachin October 18, 2012 at 5:05 am #

        Thanks … ill try capturing requests … But the app has dynamic urls … these urls has parameters which are calculated upon execution javascript .. so can this also be done with jmeter ….

        Thanks …

  3. Sachin October 18, 2012 at 5:59 am #

    I tried capturing with jmeter proxy .. but page has auto redirects and these pages have parameters which are calculated dynamically … so how to go about this .. how abt using HTMLunit … …
    Thanks …

    • autumnator October 18, 2012 at 6:21 am #

      Yes HTMLUnit or PhantomJSDriver are some options you can try, those will scale better than with a real browser. As for the JMeter route, I believe it is still possible, but you have to know how to work with redirects, dynamic URLs, data extraction, and JMeter advanced usage/programming. I don’t recommend using JMeter proxy to build your test. Better to observe the HTTP requests & responses made using Firebug (net tab) or tools like HttpFox or liveHttpHeaders, which also show you the chain of redirects. JMeter can auto follow redirects, so that will help. If you can figure out how the dynamic URLs are calculated, you can build that into the test. Or say if the URLs are calculated within the redirects where one redirect response provides the parameters or even the full URL itself, you can parse that out and use as next request for the dynamic URL. Might be kinda hard to explain in detail if you don’t have much knowledge in this area (works better in a hands on tutorial).

    • Sachin October 18, 2012 at 6:36 am #

      I tried using jmter proxy .. my problem i can do the first request .. but further it has a java script which calculates a parameter name loopid and this is a parameter for redirected url .. Further it also has a parameter control state which i think is server is sending .. but response headers does indicate this … and these parameters are dynamic …
      Thanks …

      • Sachin October 18, 2012 at 1:06 pm #

        Thanks for the suggestions …….

      • autumnator October 18, 2012 at 6:31 pm #

        It would help if you have access to the javascript or access to the developer who works on the app. You could either replicate the logic of how the loopid and other parameters are calculated within JMeter, or load the javascript code into JMeter to execute (via BSF preprocessor) and use the output in your request. As for response headers containing parameters, I think you should be able to extract those out via a regular expression extractor, etc. and pass as input in your next request. I’ve never parsed out response headers though, have only done with response body.

  4. Sachin October 19, 2012 at 3:34 am #

    I tried with jmeter .. i was able to solve the javascript issue … Bu the problem is there is other paratmeter adf_contol_state .. which is dynamically calculated … I think its Ajax related … i think its not present in the response header either ..
    Thanks …..

    • Sachin October 19, 2012 at 2:01 pm #

      Thanks for all these info .. one last question .. can i do the same tasks using HttpUrl or other classes in java … because i am told if possible do with pure java without using Htunit or jmeter ..

      • autumnator October 19, 2012 at 9:26 pm #

        Yes, that would be possible to use HttpUrl or other Java library/classes. It may be easier to do with JMeter, etc. since you have the functionality built into the tool to do things like regular expressions, branching/looping logic, setting headers, etc. w/o coding needed. If you use the Java classes method, you may have to write all the needed functionality from scratch unless you have most of the framework set up already.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Midnight Musings

Thoughts on making art

Automation Guide

The More You Learn The More You Play...!

The Performance Engineer



Thoughts related to software development

Yi Wang's Tech Notes

A blog ported from http://cxwangyi.blogspot.com

Appium Tutorial

Technical…..Practical…..Theoretically Interesting


I swear! Meerkats can do Linux


Requeuing the packets dropped in my memory.

Two cents of software value

Writing. Training. Consulting.

@akumar overflow

wisdom exceeding 140 chars.

Lazy Programmer's Shortcut

Java, J2EE, Spring, OOAD, DDD & LIFE! .......all in one :)

Testing Mobile Apps



education and inspiration for visual storytellers

No, Seriously...

Freeing up some mind cache!

Mike Taulty

I do some developer stuff for Microsoft UK

Dhananjay Kumar

Developer Evangelist @Infragistics | MVP @Microsoft |

Bespoke Blog

Science! Culture! Computational Engines!

%d bloggers like this: