PhantomJS - Executing Selenium Automation scripts on a headless browser using PhantomJS

What is Headless Browser ?

Headless browser is a web browser without Graphical User Interface.

What are the different browsers which have Graphical User Interface ?

Firefox, Chrome, Internet Explorer, Safari and Opera browser have Graphical User Interface.

In Selenium, while executing the automation scripts on any of the above browser, we can visually see what actually is happening inside the browser, as these browsers have Graphical User Interface.

Unlike the above browser, headless browsers wont have any Graphical User Interface.

What is PhantomJS ?

PhantomJS is one of the Selenium Drivers, which will enable to run our automation scripts on a headless browser. While the automation scripts are running on the headless browser, we cannot see what is happening inside the headless browser, as they dont have Graphical User Interface and hence are not completely visible to our eyes.

In order to run the automation scripts on Chrome browser, we need to use chromedriver.exe.
In order to run the automation scripts on Internet Explorer browser, we need to use iedriverserver.exe
In order to run Selenium 3 scripts on Firefox browser, we need to use geckodriver.exe

Similarly, in order to run the automation scripts on headless browser having no GUI, we can use any of these below drivers for Selenium:

  • HtmlUnitDriver
  • Ghost Driver
  • PhantomJS Driver
  • ZombieJS Driver
  • Watir-WebDriver

Hence PhantomJS is one of the drivers designed for running Selenium scripts on a headless browser having no GUI.

Practically executing the Selenium code using PhantomJS invisible browser:

1. Create a new Java Project in Eclipse IDE as shown below:

2. Create 'libraries' folder inside the project as shown below:

3. Download Selenium 3.4.0  Jar files from here as shown below:

4. Extract the downloaded Zip file and copy all the JAR files from the extracted folder into the 'libraries' folder in Eclipse IDE as shown below:

5. Check whether PhantomJS driver jar file is available in the above extracted jar files and pasted jar files as shown below:

Note: No need to download PhantomJS driver jar file separately , as it is part of Selenium jar files.

6. Configure the Jar files from the Project Properties > Libraries tab > Add Jars > Select jars under 'libraries' folder of project > Click 'OK' button followed by 'Apply' button as shown below:

7. Observe that all the Jar files in the 'libraries' folder got configured as shown below:

8. Create a new Java class in the Project as shown below:

9. Search 'PhantomJS download' in Google search and click on the below shown link from the search results:

10. In the resultant page, click to download the windows version of PhantomJS as shown below:

11. Extract the downloaded Zip file and copy the extracted folder to any location on your machine as shown below:

12. Go inside the above folder and find the path of the phantomjs.exe file as shown below:

13. Now write the Selenium code to set system property for the above phantomjs.exe file as shown below:

14. Create driver object for the PhantomJSDriver class as shown below:

15. Hover the mouse over the errors and resolve them as shown below:

16. Observe that the errors got resolved and write the remaining Selenium code as shown below:

17. Execute the above code and observe that the code will be executed on an invisible browser by PhantomJS. Also observe that output in red will be displayed in Eclipse IDE console, even though the code got executed on an invisible browser:

18. As there is no way of seeing the above executed on a browser, lets add some SOP statements between the code as shown below, so that we can understand how the code got executed:

19. Execute the above code having SOP statements and observe that they got printed on the Eclipse IDE console as shown below:


Hence PhantomJS driver is one of the Selenium Drivers, which is used to executed the Selenium Automation code in an invisible way on a headless browser.


Aswathy said...

Very detailed article. It is always helpful when you have screenshots of every single step. Appreciate your documentation. Thanks

Arun Motoori said...

Thank you Aswathy :)