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:
- REST APIs
- 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).
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):