Getting Started with PhantomJS
()
About this ebook
Related to Getting Started with PhantomJS
Related ebooks
Implementing AppFog Rating: 0 out of 5 stars0 ratingsDjango Project Blueprints Rating: 0 out of 5 stars0 ratingsBuilding Web Applications with Flask Rating: 0 out of 5 stars0 ratingsJavaScript Projects for Kids Rating: 0 out of 5 stars0 ratingsInstant Hands-on Testing with PHPUnit How-to Rating: 0 out of 5 stars0 ratingsGetting Started with React Native Rating: 4 out of 5 stars4/5Instant Jsoup How-to Rating: 0 out of 5 stars0 ratingsLearning Website Development with Django Rating: 0 out of 5 stars0 ratingsWeb App Testing Using Knockout.JS Rating: 0 out of 5 stars0 ratingsWeb Development with Jade Rating: 0 out of 5 stars0 ratingsMobile Web Performance Optimization Rating: 0 out of 5 stars0 ratingsFlask Blueprints Rating: 0 out of 5 stars0 ratingsGetting Started with Flurry Analytics Rating: 0 out of 5 stars0 ratingsPython Web Scraping - Second Edition Rating: 5 out of 5 stars5/5Practical Django 2 and Channels 2: Building Projects and Applications with Real-Time Capabilities Rating: 0 out of 5 stars0 ratingsMastering Flask Rating: 0 out of 5 stars0 ratingsWildFly: New Features Rating: 0 out of 5 stars0 ratingsLearning Splunk Web Framework Rating: 0 out of 5 stars0 ratingsOpa Application Development Rating: 0 out of 5 stars0 ratingsAdvanced Express Web Application Development Rating: 0 out of 5 stars0 ratingsJasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratingsGetting Started with Grunt: The JavaScript Task Runner Rating: 3 out of 5 stars3/5Mastering PyCharm Rating: 5 out of 5 stars5/5Django 1.0 Web Site Development Rating: 4 out of 5 stars4/5Learning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsInstant Apache Stanbol Rating: 0 out of 5 stars0 ratingsInstant RESS Implementation: How To Rating: 0 out of 5 stars0 ratingsJavaScript Unlocked Rating: 5 out of 5 stars5/5Getting Started with Gulp – Second Edition Rating: 0 out of 5 stars0 ratings
Internet & Web For You
No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsSocial Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Coding For Dummies Rating: 5 out of 5 stars5/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5The Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Get Rich or Lie Trying: Ambition and Deceit in the New Influencer Economy Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5200+ Ways to Protect Your Privacy: Simple Ways to Prevent Hacks and Protect Your Privacy--On and Offline Rating: 0 out of 5 stars0 ratingsPodcasting For Dummies Rating: 4 out of 5 stars4/5Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Remote/WebCam Notarization : Basic Understanding Rating: 3 out of 5 stars3/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsMore Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5The Cyber Attack Survival Manual: Tools for Surviving Everything from Identity Theft to the Digital Apocalypse Rating: 0 out of 5 stars0 ratingsThe Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5The Internet Is Not What You Think It Is: A History, a Philosophy, a Warning Rating: 4 out of 5 stars4/5How To Start A Podcast Rating: 4 out of 5 stars4/5How to Destroy Surveillance Capitalism Rating: 4 out of 5 stars4/5Introduction to Internet Scams and Fraud: Credit Card Theft, Work-At-Home Scams and Lottery Scams Rating: 4 out of 5 stars4/5
Reviews for Getting Started with PhantomJS
0 ratings0 reviews
Book preview
Getting Started with PhantomJS - Aries Beltran
Table of Contents
Getting Started with PhantomJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started
Downloading PhantomJS
Building PhantomJS from source
Working with PhantomJS
PhantomJS JavaScript API
The Module API
The WebPage API
The System API
The FileSystem API
The WebServer API
The phantom object
The command-line arguments
The script argument
The debug option
The cookie-file option
Writing PhantomJS scripts
Summary
2. Manipulating Page Content
Opening a web page
Playing with DOM elements
Selecting elements
Simulating mouse clicks
PhantomJS event triggering
DOM event triggering
Working with form fields
Summary
3. Handling Events and Callbacks
Listening to page events
Page load events
When a resource is requested
Receiving a page resource
Two stages of the resource received event
Knowing when the URL changes
Capturing alert messages
Answering prompt messages
Responding to confirm pop ups - OK or Cancel
Performing user events
Keyboard events
Mouse events
Summary
4. Capturing Errors
Handling PhantomJS errors
Capturing page script errors
Anticipating the page loading error
Exiting with error codes
Summary
5. Grabbing Pages
Undocumented supported image format
Screenshot dimensions
Saving web pages as PDFs
Summary
6. Accessing Location-based Services
Checking a location based on IP address
Getting driving directions
Looking up for pizza delivery establishments
Summary
7. Working with Files
PhantomJS FileSystem API
Reading files
Checking for file existence
Opening the file
Closing the opened files
Writing files
Character encoding
Summary
8. Cookies
Reading cookies
Baking some cookies
Deleting cookies
Keeping cookies
Summary
9. External JavaScript
Modules
Using the object-oriented modules
Using third-party JavaScript libraries
Summary
10. Testing with PhantomJS
What is unit testing?
Unit testing with Jasmine
Downloading Jasmine
Test specs with Jasmine
The PhantomJS Jasmine runner
Summary
11. Maximizing PhantomJS
CasperJS
GhostDriver
Screenshots, web metrics, and more
Summary
Index
Getting Started with PhantomJS
Getting Started with PhantomJS
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2013
Production Reference: 1191113
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-422-7
www.packtpub.com
Cover Image by Abhishek Pandey (<abhishek.pandey1210@gmail.com>)
Credits
Author
Aries Beltran
Reviewers
Phil Sales
Stéphane Wirtel
Acquisition Editors
Owen Roberts
Martin Bell
Commissioning Editor
Sruthi Kutty
Technical Editors
Menza Mathew
Amit Shetty
Copy Editors
Sarang Chari
Tanvi Gaitonde
Insiya Morbiwala
Alfida Paiva
Lavina Pereira
Project Coordinator
Joel Goveya
Proofreader
Lauren Harkins
Indexer
Tejal Soni
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
About the Author
Aries Beltran is a software developer located in Manila, Philippines. He works as an architect and R&D developer for financial businesses using web and enterprise technologies. He is currently developing new tools to provide real-time insights. He is interested in playing around with cutting-edge HTML5 development and mobile visualization. When he isn't coding, he likes to take photos of his daughter, Tara, who is his favorite model.
I would like to thank my family, Cecille and Tara, for inspiring me always and giving me courage to aim higher. I would also like to thank Phil Sales for helping me in every aspect of this book. Lastly, I would like to thank all of the people that I work with at Packt Publishing, who are very supportive and understanding.
About the Reviewers
Phil Sales is a software development manager and has worked in this role for more than 10 years. He started and managed development and testing teams for various companies, mostly in the banking sector. Most of his projects have been web application oriented, with a Java/J2EE flavor. His latest endeavor involved starting up a Manila office for a UK-based software vendor, with development, testing, and support teams. Based in Manila, he has also worked on projects in the US, the UK, and Thailand.
Stéphane Wirtel has been one of the main developers of the OpenERP project for the past six years. He is also a consultant for the high availability of OpenERP and for the SaaS architecture of OpenERP. He has been an enthusiastic developer and user of Linux for the past 15 years and has been working on Python for 10 years. He likes to discover new technologies (LLVM, Erlang, Golang, and so on).
He is also a member of the Python Software Foundation and a former member of the Association for Computing Machinery and the GNOME Foundation.
I would like to thank my wife, Anne and my daughter, Margaux.
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Preface
PhantomJS is a fully scriptable headless browser. When I started using it two years ago, I thought it was just another environment that can perform and evaluate JavaScript, but, as I explored its features, to my surprise, I found it to be an awesome technology. Most of the features that are discussed in this book come from the bits and pieces of the application that I have been using in my work to create a web monitoring and user-simulation type of service. PhantomJS is one of those technologies that can be integrated into any existing platform and can solve web development puzzles, ranging from page manipulation to user event simulation.
This book is a guide to help you not only ease your way into developing scripts in PhantomJS but it will also show you the cool features of this technology. In addition, it will also encourage you to be more creative and play with it as each chapter unfolds new capabilities. This book will help you get started.
What this book covers
Chapter 1, Getting Started, starts by introducing what PhantomJS is, how to get and install it, and then goes directly into creating your very first script.
Chapter 2, Manipulating Page Content, shows you how to directly interact with pages that we open in our headless browser.
Chapter 3, Handling Events and Callbacks, explores how to capture events that web pages generate for the browser and reacts accordingly. We will also simulate browser events that involve simulating the users' mouse and keyboard events.
Chapter 4, Capturing Errors, focuses on how to handle errors within PhantomJS and those that are generated by the pages.
Chapter 5, Grabbing Pages, plays with one cool feature of PhantomJS, that is, capturing screenshots programmatically.
Chapter