Getting Started with PhantomJS - Aries beltran - E-Book

Getting Started with PhantomJS E-Book

Aries beltran

0,0
23,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 133

Veröffentlichungsjahr: 2013

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



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?
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 (<[email protected]>)

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 <[email protected]> for more details.

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 PacktCopy and paste, print and bookmark contentOn demand and accessible via web browser

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 6, Accessing Location-based Services, focuses on using existing web-based and location-based services. Using PhantomJS as our headless browser, we will be capturing location data and creating examples that are useful.

Chapter 7, Working with Files, shows you how to work with files and understand how these are supported by PhantomJS using its own FileSystem API.

Chapter 8, Cookies, discusses how cookies are supported by PhantomJS. We will learn to create cookies and interact with the pages that need them.

Chapter 9, External JavaScript, addresses the subject of working with external JavaScripts and shows how we can create PhantomJS modules that can be re-used in every script.

Chapter 10, Testing with PhantomJS, focuses on the use of PhantomJS for testing. We will learn how to use Jasmine, create test scripts, and make them work using PhantomJS.

Chapter 11, Maximizing PhantomJS, goes over the different products that are written using PhantomJS and shows how we can go beyond what we've learned from the books to make our own applications.

What you need for this book

You will need a Windows, Mac OS, or a Linux-based environment where you can develop and work with the examples given in this book. You will definitely need PhantomJS binaries, which will be discussed in Chapter 1, Getting Started, where we will learn how to get and install them.

Who this book is for

This book is intended for those who are interested in developing cool scripts and having fun at the same time, using JavaScript and PhantomJS. If you are a complete novice in JavaScript, this is not the book for you. You should also have working knowledge of HTML and CSS, but you don't need to be an advanced user.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Getting Started

PhantomJS is a new solution that provides headless testing of web applications. It is also a tool for dynamically capturing and rendering pages as images. It allows you to programmatically manipulate page content to control and change it to different forms. It can scrape websites and save important information to files. It will also provide you network-level information of your page and site resources. These are just a few of the functions that PhantomJS can do for us. It provides a fresh and a whole new way for web designers, testers, and developers to perform and create browser-based solutions.

PhantomJS uses QtWebKit as its core browser capability and uses the WebKit JavaScript engine for script interpretation and execution. Anything and everything that you can do in a WebKit-based browser (such as Chrome, Safari, and Opera browser) you can do with PhantomJS. It's more than just a browser because it supports web standards, such as CSS selector, DOM manipulation, JSON, HTML5 Canvas, and SVG; moreover, you can do some cool stuff such as performing file system I/O, accessing system environment variables, or even instantiating your own implementation of a web server daemon.

Downloading PhantomJS

Before we go through the features of PhantomJS, first we need to get our copy of the PhantomJS binaries. Typically, PhantomJS provides downloadable releases of binaries that are precompiled and packaged. You can choose from Linux, Mac OS X, and Windows precompiled packages. To download a copy, go to http://www.phantomjs.org/download.html.

Download your binaries based on your preference of operating system. After downloading, extract the binaries to any folder you desire. That's it! Your PhantomJS binary is ready to be used.

Tip

Add PhantomJS to PATH

Add the /bin folder of PhantomJS into your $PATH OS to make it easier when calling PhantomJS main binary. This allows us to call the binary anywhere without specifying the full path.