Phantomjs selenium

Introduction Phantomjs selenium:

Contents

It becomes quite easier when using phantomjs selenium.It has fast and native support for various web standards such as DOM handling, CSS selector, JSON and etc. PhantomJS uses the WebDriver Wire Protocol implementation which is achieved by GhostDriver running at the back end of phantomjs.

PhantomJs is a webkit which runs headless with an inbuilt javascript API. It is quite fast as compared to running tests using selenium web driver.

phanthom- Automation Laboratories

 As the person can use his own implementation technique to automate test suites using phantomjs. For example I used Java to implement the test scenarios with phantomjs selenium as a browser to run the desired test cases.

What is Headless browsing?

Headless as the word suggest without being seen i.e when running automation tests users would not see any direct browser interaction but most of the functionalities would run from the back end. So basically a headless browser is a web browser without a graphical user interface. So google started using headless browsing from 2009 in order to index contents of websites using ajax.I started using phantomjs driver for headless automation because I have initially used CasperJS to carry out automation of few test cases. Now casperjs is just a wrapper over phantomjs to extend its capability. It was quite easy to implement and interesting to, seeing it run in a headless way. But as I am new to JavaScript it was a bit time consuming for me to learn the implementation of casperjs along with the implementation of JavaScript. That is when I started researching thinking about this fact that if there are some other ways of integrating phantomjs selenium. After quite a bit of research I found out that phantomjs driver is itself readily available with the Selenium library.

Pre-requisites:

  • You will be needing the binary executable file of phantomjs. Make sure the phantomjs version is above 2.
  • Java Development Kit installed in the machine.
  • Eclipse IDE (this is not mandatory), you can also run your test cases from your console.
  • Selenium jars.
  • Test-NGI used Test-NG to run the test cases. People can also use the normal java implementation to run the test cases.

 

Automation Steps:

Step-1: Open Eclipse IDE and create a Java project.

The following screen will appear

1

 

2

Step-2: Create the required project. Now when you click on you project you will see the following folder structure.

3

4

Step-3: By default there would be a set of system jars present. You would have to import the required selenium jars in order to get started. Follow the required screenshots

5

Now the secret ingredient that would help you all start running your automation using phantomjs.

Capabilities caps = new DesiredCapabilities();

((DesiredCapabilities) caps).setJavascriptEnabled(true);

((DesiredCapabilities) caps).setCapability(“takesScreenshot”, true);

((DesiredCapabilities) caps).setCapability(

PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,

“//Users//soumyajit//Documents//Zippers//phantomjs//bin//phantomjs”

);

driver = new PhantomJSDriver(caps);

return driver;

Now as you see I have used Capabilities class and its required methods to configure the driver instance as to what configurations the driver should possess in order begin execution on the browser side. Like in the above snippet I have set the desired capability to get the path of the binary phantomjs execution file. Also I have set phantomjs to take screenshots. You can also set the capability to ignore all SSL alerts appearing by using this line of code

((DesiredCapabilities) caps).setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGSnewString[] {“–web-security=no”, “–ignore-ssl-errors=yes”});

Although I have used absolute path to point to the phantomjs binary execution file. But best practice is to use relative path as it would be more generic to a system.

And we are good to run our automation test case.

This is the view of my console where the test cases are running.

6

So a few points with respect to the console where you see the test cases running

  • Your console or the CLI where the tests are being executed by phantomjs starts acting as a browser console.
  • This is the reason why users start seeing browser console related errors while the tests are running. I see this as an added advantage. As, because if there is a fatal console error that may lead to someone cracking the system, in that case the console errors can be reported earlier to ensure the protection of the application.
  • Besides this there is a log also known as phantomjsdriver log which separately guides the user with the browser behaviour, such as page settings for each session, what are the errors in case there is any failure in running the test cases etc.

Heres how the phantomjs driver log looks like

7

So there is the gist of Selenium headless automation using phantomjs. Please do provide your feedback and any doubt shoot me a mail. I will try to resolve your queries. For further, you can go through my github link to understand the implementation https://github.com/Corefinder89/Selenium-PhantomJSDriver







Soumyajit Basu

Soumyajit is 5+ years experienced Software professional with his prime focus on automation technologies based on quality development and takes interest in the CI/CD processes. He provides help in developing the QA process in an organization with his skills in automation for the web platform. His focus is on improving the delivery process for an ongoing project and connects the dot to help out with a successful deployment. He has experience in working on analytics, e-commerce, and the ad-tech domain.

Besides being a professional he takes an immense interest in learning new skills and technologies. He is a research guide author/writer at Dzone and Web Code Geeks. He also maintains a blog platform of his own where he likes to keep up his technology junks.

Leave a Reply

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