Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with PhantomJS

You're reading from   Getting Started with PhantomJS Harness the strength and capabilities of PhantomJS to interact with the web and perform website testing with a headless browser based on WebKit

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781782164227
Length 140 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Aries beltran Aries beltran
Author Profile Icon Aries beltran
Aries beltran
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started 2. Manipulating Page Content FREE CHAPTER 3. Handling Events and Callbacks 4. Capturing Errors 5. Grabbing Pages 6. Accessing Location-based Services 7. Working with Files 8. Cookies 9. External JavaScript 10. Testing with PhantomJS 11. Maximizing PhantomJS Index

PhantomJS JavaScript API

PhantomJS runs JavaScript and comes with a JavaScript API to make your life easy. It extends the standard JavaScript API and adds richer layers of capabilities, such as allowing us access to the underlying file system, ease of access and manipulation of DOM objects, system and environment variable gathering. It even gives us the ability to inject custom scripts into the web page.

But, be warned. PhantomJS is a very active community, and every now and then, changes are being introduced. New APIs and objects are being added, but of course, there are few items that are being changed, and ultimately some of them become deprecated or are totally removed. The PhantomJS website has a full list of all the functions and syntax, and has proper tagging for deprecated functions. We should visit it regularly to check for upcoming updates so that we can adjust appropriately in our codes.

The Module API

While writing custom objects and API sets, you may want to create custom modules that will make your life easier. You can do that in PhantomJS using the Module API. It allows you to create your own modules and import it anywhere in your implementation. The built-in modules are webpage, system, fs (File System), and webserver.

The WebPage API

PhantomJS is a headless browser. Accessing and manipulating web documents are its core functionalities, and that's what the WebPage API is used for. The WebPage API allows us to access, control, and manipulate web documents. It provides a rich interface to easily reference and extract page details including document content. It enables capturing of events, such as page loading, when an error occurs within the page, or when navigating to another page is requested, and so on. It is also capable of capturing pages and saving them as images. And more importantly, it allows you to manipulate documents on the fly and traverse DOM as you do with any web page. This is enormously valuable for writing automated user interface tests—for example, you can force click events or post forms, and capture the results—as well as standard web scraping of public URLs.

The System API

The System module provides system-level functionalities ranging from OS information, environment variables, command-line arguments, and process-related properties. The System module is very useful as you engage more in developing applications with PhantomJS.

The FileSystem API

Accessing files, writing to text files, or just reading a custom configuration file—these are tasks that can be done with PhantomJS. FileSystem provides a standard API to perform file I/O. You can read, write, and delete files; you can even list folder files.

FileSystem has 31 functions to manage and manipulate files within PhantomJS. We have been using this set extensively, and it helps solve several problems without fail. Writing JSON data to a file is very basic, but you will find it much easier using the FileSystem API.

The WebServer API

Perhaps you have a grand idea, but it requires you to process a web request and execute a PhantomJS script on it. PhantomJS can do that; you can embed your own web server implementation using the WebServer API within your PhantomJS application. This feature is marked as experimental, but it does work, and with roughly five lines of code you can have your own web server running.

This module is based on the open source Mongoose web server library that supports multiple platforms, authorization, web sockets, URL rewrite, and even "resumeable" downloads. For more information about Mongoose, visit http://code.google.com/p/mongoose/.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime