Archive | PHP RSS feed for this section

Selenium file download by code and request for more platform options

23 Aug

I see in the Selenium discussion groups, the following has been a frequent topic:

How to download file with Selenium/WebDriver?

particularly a cross-browser, cross-OS solution, or when FF/Chrome profile doesn’t work for the auto download file to some location (w/o prompting) and AutoIt, etc. not perfect.

The typical suggestion is to do it via external code via HTTP libraries for your language platform. But so far, there’s only suggestion of Java:

Being an idealist, I think it would be nice if there were more options besides Java. And furthermore, could provide more library code for integrated testing to do more than just download files. I’m thinking along the lines of sharing session cookies to make HTTP POSTs and other REST API calls outside of Selenium but that require a session that is started from and available with Selenium. Think functional GUI + API testing together.

On that note at least, here’s a start for similar example to the Java one in PHP:

Now who’s with me in trying to offer more to the community than just Java (and PHP)? We should have equivalent examples in .NET, Python, Ruby, etc.

Update 4/6/2015:

Python version can be from this:

and an alternate simple gist for Java:

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):

Sue Connelly's KIT List Blog

Job Search and Career Management Tips and Strategies

The Networker

Bassem Aly



xlcwu's Blog

Lifestyle, Computer, Ubuntu, Linux, Apple, Mac, iPhone, Game, Programming, Java, C, ObjC and More ...

Multiple States Knowledge Base

Multiple States is a digital design, development and content creation studio, specialising in beautiful, clean and functional websites with powerful content.

journey of quality

make it perfect!

Marc Guillemot's blog

Java, Groovy, HtmlUnit, WebTest, NekoHTML and more. Contact me for rates.

North American Sea Glass Association official website

NASGA is a non-profit organization positively supporting sea glass collectors and the sea glass community with festivals, information, educational opportunities, commercial membership and more. The primary goal of NASGA is to establish a community of informed collectors and sellers of sea glass that are educated on the characteristics and significance of genuine sea glass.

Abiral Shakya's Blog

Learning, Improving and Sharing

Tea in Eighteenth-Century Britain

History of Tea Project at Queen Mary University of London

Phimosis - A Simple Cure (That is working for me)

No nonsense, no adverts, no sales pitch, just honest information.

Glass Onion Blog

Cheat sheets, post-its and random notes from the desk of a programmer

Abode QA

A Hub For Testing Minds...

The Test Therapist

Performance & Security Testing Blog


a programmer's hub

Let's Not Crash and Burn

it makes your brain tingle


“Incinerate Ignorance”

Anastasia Writes

politics, engineering, parenting, relevant things over coffee.

One Software Tester

Trying To Make Sense Of The World, One Test At A Time