Quick PhantomJS setup with PyCharm and Selenium Webdriver – beware of slashes on Windows

PhantomJS was suggested tom me time and time again for web testing. The term being something like “dude, it’s a headless browser”. It is actually a cool concept. Good introduction here.. The official web page defines it as : PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

It speeds up the test execution as it does not render the content visually on screen. What this means is that your usual browser (like Firefox) is not initiated and thus not only is it much faster but also might prevent compatibility issues with browsers.

self.driver = webdriver.PhantomJS(executable_path="C:/tmp/phantomjs.exe")

Beware of Windows path slashes. I originally just copy/pasted the location as

C:\tmp\phantomjs.exe

but it did not work in my setup. It did work when the slashes were reversed as

C:/tmp/phantomjs.exe

Windows 7 64bit
PhantomJS 1.9
Selenium WebDriver 2.41
Python 2.7.5
PyCharm Community 3.1.1