Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Getting Started with PhantomJS
Getting Started with PhantomJS

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

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Getting Started with PhantomJS

Chapter 2. Manipulating Page Content

PhantomJS is a browser, and the basic function of a browser is to access web pages. In this chapter, we will learn various techniques of loading web pages in PhantomJS, and we will explore beyond using it simply as a headless browser.

Opening a web page

In a normal browser, opening a web page means typing a URL and letting the browser render the document fetched. It works almost the same way in PhantomJS, except that we don't actually wait for the page to be rendered before our eyes. Everything is done in a non-visual way. We don't see text, high-resolution images, or even animation on the page. We don't see anything that will show up on the screen.

We also do not type the URL in the address bar as we do in a normal browser. We create scripts to load the page. We learned in the previous chapter that to access a URL or a page in PhantomJS, we need to use the WebPage API.

var page = require('webpage').create();
page.open("http://www.packtpub.com", function(status) {
  if ( status === "success" ) {
    console.log("Page is loaded.");
    phantom.exit(0);
  }
});

And that's how we open a page in PhantomJS. So what now? We don't use PhantomJS just to browse a...

Playing with DOM elements

There are a lot of things we can do with the page we are accessing beyond getting the title of the document, and this can be done with a little help from the Document Object Model API. We are not going to discuss each object and function of the DOM API, but we will touch on some that are very useful. If you want to learn more about DOM API, the best place to start is the Mozilla Development Network: https://developer.mozilla.org/en-US/docs/DOM.

Selecting elements

Everything starts with the document object, and it contains nested elements. To select an element, we either traverse the entire document or use the DOM selectors. There are different methods to reference a document element, which can be done by element ID, class, name, tag, or XPath.

getElementById

This retrieves the element using a unique ID

getElementByClassName

This selects the element using the element class name

getElementByName

This provides a reference using the element name

getElementByTagName...

Simulating mouse clicks

Web pages are built with interlocking links, and getting to another page can be done by following that link. In a normal browser, this is done by clicking with your mouse button. In PhantomJS there are two ways to do this: one is by using PhantomJS sendEvent functions (PhantomJS event triggering); the other is by using DOM event triggering.

PhantomJS event triggering

The WebPage API supports sending events to the page. This can be mouse events or keyboard events. We will discuss more events as we progress, but for now we will tackle how to simulate clicking on page elements and links using PhantomJS event triggering.

PhantomJS Mouse Event Triggering API

sendEvent( eventType, Point X, Point Y, button='left' )

eventType

  • mouseup
  • mousedown
  • mousemove
  • click
  • doubleclick

Point X

This is the X coordinate where we trigger the event

Point Y

This is the Y coordinate where we trigger the event

button

This states which mouse button to trigger; by default...

Working with form fields

If we are dealing with web pages, more often than not, we will come across form fields, which are input boxes, selection lists, text areas, and buttons. PhantomJS can also be used to automate the input of data and changing field values.

One of the best examples of these scenarios is a login page. We will create a script that will automate the login process to Instagram's web profile. Again, the username and password will be passed as command-line arguments. The first argument is username, followed by password.

var system = require('system');
var username = system.args[1];
var password = system.args[2];

Now that we have our credentials, we then open Instagram's login page at https://www.instagram.com/accounts/login.

var page = require('webpage').create();
page.open('https://instagram.com/accounts/login/',
function(status) {

With simple browser page inspection, we can check the element ID of the username field, password field, and...

Summary

PhantomJS's capability of manipulating web pages is very flexible. We can do a lot more than just accessing the page itself. We can modify the look, change content on the fly, or even dynamically interact with its behavior as it was coded. In this chapter, we have tried to capture relevant data from an existing social media site and use it for any other purpose. This is just the beginning of what PhantomJS can do as far as interacting with web pages. We also touched a bit upon handling the status of pages and did a simple pausing. Using JavaScript's setTimeout is just a simple workaround to let the background process complete. Proper handling of page events is also supported in PhantomJS, and we will be discussing them in the next chapter.

Left arrow icon Right arrow icon

Key benefits

  • Writing scripts that can interact directly with web services and pages
  • Interacting with social media websites using PhantomJS scripts
  • Creating web-based test scripts and running them in a headless browser

Description

PhantomJS is a headless WebKit browser with JavaScript API that allows you to create new ways to automate web testing. PhantomJS is currently being used by a large number of users to help them integrate headless web testing into their development processes. It also gives you developers a new framework to create web-based applications, from simple web manipulation to performance measurement and monitoring.A step step-by by-step guide that will help you develop new tools for solving web and testing problems in an effective and quick way. The book will teach you how to use and maximize PhantomJS to develop new tools for web scrapping, web performance measurement and monitoring, and headless web testing. This book will help you understand PhantomJS’ scripting API capabilities and strengths.This book starts by looking at PhantomJS’ JavaScript API, features, and basic execution of scripts. Throughout the book, you will learn details to help you write scripts to manipulate web documents and fully create a web scrapping tool.Through its practical approach, this book strives to teach you by example, where each chapter focuses on the common and practical usage of PhantomJS, and how to extract meaningful information from the web and other services.By the end of the book, you will have acquired the skills to enable you to use PhantomJS for web testing, as well as learning the basics of Jasmine, and how it can be used with PhantomJS.

Who is this book for?

If you are a JavaScript developers who is interested in developing applications that interact with various web services, using a headless browser, then this book is ideal for you. This book will also be good for you if you are planning to create a headless browser testing for your web application. Basic understanding of JavaScript is assumed.

What you will learn

  • Explore the possibilities of PhantomJS development and make use of a headless browser
  • Understand PhantomJS’ core features and API
  • Manipulate the DOM properties of web pages using scripts
  • Interact with web pages using PhantomJS scripts
  • Learn how to emulate keyboards and mouse actions
  • Work with geolocation services with PhantomJS
  • Build scripts that will create web sites snapshots
  • Create and implement PhantomJS custom modules
  • Use PhantomJS to perform headless testing with Jasmine

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 25, 2013
Length: 140 pages
Edition : 1st
Language : English
ISBN-13 : 9781782164234
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 25, 2013
Length: 140 pages
Edition : 1st
Language : English
ISBN-13 : 9781782164234
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 96.97
Data Visualization with D3.js Cookbook
€36.99
PhantomJS Cookbook
€34.99
Getting Started with PhantomJS
€24.99
Total 96.97 Stars icon

Table of Contents

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

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(5 Ratings)
5 star 80%
4 star 20%
3 star 0%
2 star 0%
1 star 0%
SetITGood Feb 09, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Being in IT for almost two decades now I am perhaps lucky to have witnessed the growth of this industry. More so, I am glad to see the "rebirth" of Java Scripting for front-end development in the recent years. But ask any front-end developer and one thing they dreaded most was testing these web pages and Java Scripts. PhantomJS - however is a game changer!PhantomJS provides "headless" testing of web applications. Wait, what did I mean with headless? When you are typing a URL from the browser, it essentially creates a request and the response is reflected on the page. PhantomJS actually does the same thing, EXCEPT we don't need to wait for it to be rendered before our eyes! Probably this is how it got its name :)PhantomJS also can dynamically capture/render pages as images, allow manipulation of page content/event, gain access to network-level information, and ability to save important infos into files for later processing.I find this book easy to read with only 121 pages, you can actually finish it in one sitting (but would be good to do it in front of your laptop trying out the sample codes yourself). However, it is advised you already have concrete knowledge of Java Scripts, HMTL, and CSS since the book heavily used later technologies such as DOM, JSON, and HTML5.An improvement I guess would be since the goal of the book was to jumpstart IT practitioners into PhantomJS, it would have been better to have laid out the different syntaxes of PhantomJS itself. In Chapter 2, I was surprised to see "===" as the conditional operator. Having used to "==" in Java, I had to check this if it was a typo.The book has more to give than what I expected. All throughout, the book used actual web pages (e.g. Pinterest, Instagram) as examples for us to run our tests, which challenges our creativity and encourages us to further try it out with other pages. Aside from PhantomJS, I liked how in a subtle way, the author introduced other technologies such as Yahoo LocalSearch and Google Directions API (which made me realized how easy it was to use them!). In the last chapter, the book introduced CasperJS - a spin-off extension and further simplified PhantomJS. I find this really exciting!The book is concise and straightforward, I highly encourage everyone doing front-end development to read this and for Technical Architects to consider PhantomJS at production work.On a side note, I noticed that the author used his family member's names all throughout the book, so I guess this was in some way a personal book. Anyway, I am excited for PhantomJS and is looking forward to further playing with it!
Amazon Verified review Amazon
Bill Mar 30, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book. I had so much pleasure reading it and harness the power of phantomjs. Having a limited time for a project, i couldnt find much info about it even on their main site. This book provided an easy to follow guide. I recommend it!
Amazon Verified review Amazon
Geomar Esmilla Feb 10, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Getting Started with PhantomJS is a book about well, getting started. My work for the past 5 years requires me to review application architecture and data protection solutions, needless to say I have not coded in years. And this book just got me started writing codes again. You see, it really gets you started.There are very few books on web development tools these days that could capture my interest and maintain my interest. This book? I like this book. I like the way it's written. You should buy a copy and read it. Why? Because it starts and ends the way I like it. From introducing you to the command line interface (really for geek in all of us who loves that legacy look) and it ends with a shortlist of cool tools that will keep your momentum going.Aries Beltran writes that way that makes me reminisce those moments reading Borland books back in the 80s and 90s. Ahh.. Nostalgia. Simple, and effective in catching the reader's interest as one flips from page to page. This is a 140-page book. It's not long like those 5 inches thick book. But I'm impatient. I thought I'm going to speed read my way through it because I just don't have the patience, but I was wrong. It got me engaged enough to read, really read it from cover to cover. Every 11 chapter provides a newbie to PhantomJS like me a "Eureka" moment. I found myself immerse in Aries Beltran's world. I coded while reading. I coded while I ate (I especially loved Chapter 8: Cookies). And I coded in my sleep. And discovered that I like PhantomJS.The book is packed with cool recommendations:* Check out Confess.JS for metrics (a topic close to my heart)* CasperJS seems like a natural progression from PhantomJSThis book is basically good stuff. It's a book you will revisit again and again. It's a good addition to any developer's and web aficionado's library.Check them out. [...]
Amazon Verified review Amazon
Robert Y. De Cruz Feb 10, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
PhantomJS makes website UI testing easy. It can be used as an automation tool for repeatedly going through each page, providing a means for testing that a code commit didn't break the website. Full-time testers can use it to automate their work by setting up the test scripts once, then letting PhantomJS do the tedious work of re-running the script every time the same functionality needs to be tested. This is the way UI testing should be done and I wish we had a similar tool/framework for non-Webkit browsers and desktop apps.This book is an excellent tutorial to PhantomJS. After going through the book, I'm able to do simple tasks such as opening web pages, triggering UI elements, etc. Then it shows me how to do the more complex stuff such as taking a screenshot of a web page when it was accessed, reading cookies, and doing unit testing. Software Testers will find this book valuable to their job as it goes through all the functionality you'll need to test most websites. This book will also be useful for Software Devs aiming to add some UI-level unit testing to make sure markup and JS changes don't break any of the existing UI functionality. This is a must-have book for any web development project.I also found the book to be a good reference manual as it's designed to be easy to look up how to do things. The example scripts are simple to follow and can be readily incorporated in your test scripts. It's a valuable tool in the tester's arsenal. Highly recommended for website testers and web developers looking for an easier way to test and automate their web scripts.[...]
Amazon Verified review Amazon
Andrew Artajos Feb 08, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
At 140 pages and 11 chapters, this book is a quick read. To be honest it looks more like a booklet than a real book. Despite its appearance, it delivers the meat on the chosen subject--Getting started with PhantomJS.It starts you off on basic tasks like installing PhantomJS, working with the commandline, then moves toward the more complex tasks. The author shows you how to download a simple webpage then slowly builds upon this foundation in the succeeding chapters, until you get to learn how to use PhantomJS with different API's, JQuery, Jasmine--a unittesting framework, and CasperJS. There were some parts of the book I rather skip like Capturing Errors, Working with Files, and Cookies. These topics are boring but necessary nonetheless.The book is clear, concise, and uses simple English. This helps the reader go through the book quickly without having to put more effort in understanding the subject.The book in paperback format, although more expensive, is easier to read than the ebook format. When working with the examples, I think it would be more practical to buy the ebook instead. It will be easier to refer to when typing the actual code on the computer.However, this book makes you want more. I want to see in the future a more in depth take on PhantomJS. Most likely a book that discusses the best practices and some programming gems on the topic.Overall, this book is quite informative. It gets your feet wet on PhantomJS. I'm surprised that PhantomJS could open up to a lot of possible avenues to explore. PhantomJS when combined with modules, API's, and other open source software, offers a rich set of building tools for creating the next kick ass browser based solution.as seen on [...]
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.