5 Reasons I Chose Selenium over PhantomJS

Screen Shot 2013-08-21 at 2.43.59 AM

Selenium vs PhantomJS, CasperJS, or ZombieJS do almost the same thing, automate browsers, but why should you use one over the other? It turns out that there are some use cases when using Selenium might give you advantages over a headless toolkit.

I’m not saying that I always use Selenium. I prefer to use PhantomJS because it would have less overhead and performs better. However, because of specific edge cases I needed to use a real browser where the headless toolkit just didn’t cut it.

(This is a follow up post to Running Headless Selenium with Chrome.)

#1. Easier development with Inspect Element

While writing automation against other pages, I didn’t always know what HTML element I needed to write code for. So I would write code by using a UI, right clicking on some part of the page, then selecting the HTML element I wanted.

#2. When you need a specific JavaScript engine

PhantomJS uses QtWebKit. That means PhantomJS is not the same JavaScript engine that Chrome uses. (source) For the most part it won’t matter. However, if you need to be 100% sure it looks and acts like a browser then you need a real browser.

#3. When the website uses strange browser detection

An exmaple use case is scraping websites. I’ve found websites that give you slightly different HTML than they present a browser even after you change the user-agent string. My guess is they also might attempt to detect HTML5 features or a Flash plugin.

From an SEO perspective the change in HTML it’s nothing that I would consider a negative or a cloak but the interesting bits I wanted weren’t there to scrape or interact with. A real browser was the only thing that worked for me.

#4. I actually need Flash

One of our features uses ZeroClipboard. It works partly by using an invisible Flash movie. So by default, I will need a real browser to make this test work.

#5. When you need a browser specific features

I have not used Selenium this way yet but I will need it soon. Some features I work on include steps that uploads files, print from the webpage, opens separate windows, switches tabs, and uses browser plugins.

Some features I work on right now include these steps. While we have those features covered in unit tests, I’m also testing them manually for important features. I anticipate this is a good way to automate those tests.

Again, I’m not saying that I always use Selenium. I only use it for specific use cases because it’s the only thing that will work.

10 thoughts on “5 Reasons I Chose Selenium over PhantomJS

  1. Selenium is a fantastic tool for crawling webpage content. Its really welll supported within a significant amount of programming languages (PHP, Perl, Python, Java) and the project itself very well maintained. Great article, Chris!

  2. Robert Fletcher

    Another use case that is an issue for a project I’m working on is that you need drag and drop. Headless drivers have some basic functionality, but if you need to be able to set precise coordinates you’re stuck with Selenium.

  3. Small typo : An exmaple.

    Otherwise, very interesting article.

  4. JSON wire protocol is juste too much of an abstraction on top of what browsers actually do, strong point of headless browsers is that it’s really possible to fully control what’s going on

  5. You have really helped me to clarify issues with the #2 & #3 that you pointed out. My problems is that the Java part of it doesn’t cut it for me. How do I go about learning this Selenium, I intend to work from Python with it. Please help, thanks.

    And thanks for this post.

  6. I have also found Selenium to be very useful for me like you mentioned in #3.
    Disqus.com will not PhantomJS to pass through

  7. The reason I gave up on PhantomJS is that it cannot click on SPANs or DIVs without a lot of work (I never got it to work). And the website I am testing uses drop downs made of DIVs and Spans (the devs say they have to do it that way to have stylized drop downs).

  8. @Chris Le – Great thoughts! In regards to #3, you can actually set a custom user-agent within PhantomJS (for instance, to mimic Safari or Chrome). Overall though, the ecosystem available through Selenium is a huge asset — and if you need Flash, it’s basically your only option.

    @Jeff W – I’m curious about your claim. From my experience, it’s easy to match any element in PhantomJS using CSS paths (regardless of whether it’s a div, span or whatever). You might consider checking out CasperJS. It makes PhantomJS a bit easier to deal with.

    @All – I’m pretty entrenched in the PhantomJS/CasperJS/Selenium space right now. I’ve built my own automated browser testing system in the cloud called Ghost Inspector. Happy to help anyone with questions!

  9. Jeff W: Simply load jQuery at the top of you PhantomJS script, so that jQuery gets loaded for the page you want to work on. Then just use a jQuery selector.

  10. […] Selenium is a web automation framework. Selenium provides a unified API for manipulating browsers. It is built with with a Firefox plugin. You use it to replay actions in an interactive and code based manner. It’s most popular use is automated testing for web developers and enables remote execution of automated tests.  PhantomJS is a very similar technology, running exclusively with JavaScript in Webkit, but we will be talking about Selenium here as it is a broader technology. […]

Leave a Reply

Your email address will not be published. Required fields are marked *