Phantomjs Interview Questions And Answers

phantomjs interview questions and answers   Top 6 Phantomjs Interview Questions And Answers

1) What is PhantomJS?

PhantomJS is a lightweight headless browser built on WebKit. It is called headless because the execution does not happen on the browser, but on the terminal.

Using PhantomJS, we can write to a file, read the contents of the file or upload a file, take an screen capture, convert the webpage into a pdf and lots more. With headless browsers, you get all the browser behavior i.e. Cookies, Http Request Methods i.e. GET /POST, Clearing Cookies, Deleting Cookies, etc., Reloading of Page, Navigating to a Different Page.

PhantomJS uses WebKit that has a similar browsing environment like the famous browsers - Google Chrome, Mozilla Firefox, Safari, etc. It also provides a lot of JavaScript API, which helps in taking screenshots, uploading of file, writing to file, reading a file, interacting with the web pages, etc.

PhantomJS does not support Flash or Video, as it requires plugins and it is difficult to support the same on the command line.


2) Why PhantomJS is known as headless browser?

It is said to be a headless browser as there is no involvement of browser while executing the JavaScript code. The execution will not be seen in the browser display, but on the command line prompt.


3) What are the features of PhantomJS?

Following are the key features of PhantomJS:

Page Automation: PhantomJS allows to read the page contents with the help of its API. It can help to manipulate the DOM, use external libraries to carry out the task required.

Headless Testing: PhantomJS helps testing of UI at the command line. While, with the help of a screenshot, it can easily help to find errors in the UI. PhantomJS sometimes cannot help with testing alone. However, it can be wrapped along with other testing libraries like Mocha, Yoeman, etc. You can take the help of PhantomJS to upload a file and submit the form.

PhantomJS can be used to test logins across the sites and make sure the security is not compromised. PhantomJS can be used along with other tools like CasperJS, Mocha, Qunit to make the testing more powerful.

Screen Capture: PhantomJS helps in taking a screen capture of a page specified and save the same as an image in various formats like PNG, JPEG, PDF, and GIF. With the help of the screen capture, it can easily help to make sure the web content is fine.

PhantomJS offers properties and methods with the help of which it allows developers to adjust the size of the screenshots and specify the area they want to capture.

Network Monitoring: One of the important features of PhantomJS is its usage to monitor the network connection using the API available. PhantomJS permits the inspection of network traffic; it is suitable to build various analysis on the network behavior and performance.

PhantomJS can be used to collect the data about the performance of the webpage in a live environment. PhantomJS can be used with tools like Yslow to gather performance metrics of any websites.


4) What are the PhantomJS objects?

Following are the different PhantomJS objects:

● CookiesEnabled
● Cookies
● LibraryPath
● Version

Lets get to know these objects in details:

cookiesEnabled: It tells whether the cookies are enabled or not. It will return true, if yes; otherwise false.
Below is the syntax:

phantom.cookiesEnabled


Cookies: It helps to add and set cookies to a domain. It returns an object with all the cookies available for the domain.
Below is the syntax:

phantom.cookies;


LibraryPath: PhantomJS libraryPath stores the script path to be used by the injectJS method.
Below is the syntax:

phantom.libraryPath


Version: It gives the version of the PhantomJS that is running and returns the details in an object. For example: {"major":2,"minor":1,"patch":1}.
Below is the syntax:

phantom.version


5) What are the methods in PhantomJS?

PhantomJS is a platform to help execute JavaScript without a browser. To do that, the following methods are used, which help in Adding the Cookie, Deleting, Clearing, Exiting the Script, Injecting JS, etc.


6) What are the different methods that help to execute JavaScript without the browser in PhantomJS?

Following are the list of methods:

● addCookie
● clearCookie
● deleteCookie
● Exit
● InjectJS

Methods details:

addCookie: The addcookie method is used to add cookies and store in the data. It is similar to how the browser stores it. It takes a single argument that is an object with all the properties of cookies.
Here is the syntax:

phantom.addCookie ({ 
   "name" : "cookie_name",  
   "value" : "cookie_value", 
   "domain" : "localhost" 
});


clearCookies: This method is use to delete all the cookies.
Here is the syntax:

phantom.clearCookies();


deleteCookie: This method is use to delete any cookie in the CookieJar with a 'name' property matching cookieName. It will return true, if successfully deleted; otherwise false..
Here is the syntax:

phantom.deleteCookie(cookiename);


Exit: The phantom.exit method will exit the script which it had started. It exits the program with return value mentioned. It gives '0', if there is no value passed.
Here is the syntax:

phantom.exit(value);


injectJs: InjectJs is used to add addtionaljs files in phantom. If the file is not found in the current directory librarypath, then the phantom property (phantom.libraryPath) is used as an additional place to track the path. It returns true if the file addition is successful otherwise false for failure, incase if it is not able to locate the file.
Here is the syntax:

phantom.injectJs(filename);