When comparing PhantomJS vs Selenium, the Slant community recommends PhantomJS for most people. In the question“What are the best headless browsers for testing?” PhantomJS is ranked 1st while Selenium is ranked 6th.
Ranked in these QuestionsQuestion Ranking
Pros
Pro Supports screen capture
Pro Used in many open source projects
Pro Supports many browser standards
PhantomJS has full DOM and CSS parsing, JSON, canvas, and SVG support.
Pro Built on WebKit
WebKit is becoming the gold standard for browser compatibility, making it a good starting point for native headless browser testing.
Pro Actively supported
The open source community behind Selenium has a very large & engaged developer base. This ensures a stable support channel for the tool. In addition, various companies also provide support for Selenium. This active and multi-channel support provides a much more lucrative option for developers looking to implement Selenium in their workflow.
Pro Cross-browser
Supports many browsers and a solid platform for adding future browsers to selenium (WebDriver)
Pro Cross-platform
Selenium is based on Java and can be run on Windows, Mac, Linux, Unix, etc.
Pro Ability to imitate a real user using the application
Testing is analogous to a human being using the web application which is not possible with a solution which runs headless and claims to run faster than Selenium.
Pro Integrates with any CI tool
Selenium, with its ability to interact with different testing frameworks, like NUnit or xUnit, makes it easy to get Selenium working with practically any Continuous Integration services.
Pro Uses an approved WC3 standard - WebDriver
The WebDriver protocol is standardized by the World Wide Web Consortium which makes it easy for third-parties to get involved and contribute to Selenium.
WebDriver has been the foundation of many additional automation projects besides core selenium and has become the de-facto standard for UI automation.
Pro You can use the same IDE you use for coding to debug
Pro Integrates well with existing testing frameworks
Pro Support for many languages
Selenium supports a variety of languages including Java, Python, PHP, C#, Ruby and JavaScript ensuring that the tool is easily accessible to a wide variety of developers.
Pro Use the very minimum amount of code required to run
Selenium tries to inject into the page only the bare minimum of code needed to run the tests.
Pro Easy to set preconditions for the tests
Cons
Con Deprecated by Puppeteer
PhantomJS is no longer actively maintained by the original authors. Puppeteer is said to be a replacement supported and backed by the Google Chrome team, now.
Con Heavy setup
You'll often end up having PhantomJS binaries connected via WebDriver to your testing framework, possibly using client/server especially if you want your test running with something else than Java. This means an overhead in terms of maintenance and performance, but still usually lighter than running a full browser (like Chrome, Firefox, IE).
Con Browser closes unexpectedly
It often happens when running on more then 5 (my measurement) JVM instances that the browser gets stuck and quits unexpectedly. This can be partially solved by running the instances one by one instead of parallel (this is a problem when testing Jenkins and Bamboo agents) but I don't believe this qualifies as a solution. The error is called UnreachableBrowserException
.
Con Elements are sometimes not visible
This is an error which occurs with almost no reason, PhantomJS sometimes decides that it cannot click the element even though the element is intractable or enabled.
This happens if you have to scroll to see the element (and these are not pages that load elements with JavaScript) which is strange because PhantomJS should catch the whole page if it is not loaded explicitly with JavaScript. This problem partially goes away with re-sizing the browser, but that does not really qualify as a solution.
The error it raises is: ElementNotVisibleException
.
Con Requires installation of drivers
Con The IDE for recording isn't too powerful
Con Issues with locators
Locators that support common attributes like: name
, id
, XPATH
, javascriptDOM
, etc. have to be found through firebugs.