ebook img

Everyday Scripting with Ruby: For Teams, Testers, and You PDF

297 Pages·2007·2.016 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Everyday Scripting with Ruby: For Teams, Testers, and You

What readers are saying about Everyday Scripting with Ruby What a wondrous collection of recipes, guidelines, warnings, compre- hensive examples, metaphors, exercises, and questions! It’s a terrific value to softwaretesting practitioners who want to get the most from their test automation effort. Grigori Melnik Lecturer, University of Calgary A fantastictype-along-with-me introduction to a powerful scripting language that starts in the shallows and then moves into the depths turning the reader into an accomplished Ruby scripter, almost with- out them noticing it! Erik Petersen Emprove Finally a hands-on book that is filled with gems of wisdom for the test- ing community. By following the book’s easy-to-read chapters, real-life code samples, and superb coverage of complex topics like test-driven design and inheritance, a tester will not only take her testing career to the next level but also contribute immensely to the software develop- ment at her organization. Gunjan Doshi VP of Product Development and Process Excellence, Community Connect, Inc Marick explains the Ruby language using a series of short, practical examples. Watir users and other testers who want to learn Ruby will find it very accessible. Bret Pettichord Lead Developer, Watir When you’ve read this book, you will be able to automate software tests, which will give you an edge on most of your QA workmates. You will be able to program in Ruby, which is a joy in itself. You will have created several very useful utilities and will know how to adapt them to meet your particular needs. All of the above will have been achieved briskly and pleasantly. You will become a more effective tester and, most likely, will have a fine time in the process. George Hawthorne Consultant, Oblomov Consulting The book is an excellent read, is very informative, and covers a lot of ground in a relatively slim book. I think this is always a good idea. I have a lot of 800+ page tech books that I’ve read about the first half or two thirdsof, because they are padded toward the end with very esoteric information. This book held my interest throughout—I have a full-time job and a ten-month-old son and still managed to get through these examples in around a week! Brian’s personality comes through (e.g., the Kennel containing Barkers) in a good way that helps rather than hinders in understanding the material. Paddy Healey Enterprise Systems Engineer, Aventail Corporation The chapters, examples, and exercises on regular expressions are worth the cost of the book alone! Everythingelse is more than just gravy—it’s every kind of dessert you didn’t know you could have. Whether you are just beginning to script or have been scripting for several years, this book will be an invaluable resource. The examples and exercises, Ruby facts, step-by-step approach, and explanations will help you kick up your automation efforts to a whole new level! Paul Carvalho Consultant, SoftwareTesting and Quality Services Everyday Scripting with Ruby For Teams, Testers, and You Brian Marick The Pragmatic Bookshelf Raleigh,NorthCarolina Dallas,Texas Many of the designations used by manufacturers and sellers to distinguish their prod- uctsareclaimedastrademarks.Wherethosedesignationsappearinthisbook,andThe Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The PragmaticProgrammer,PragmaticProgramming,PragmaticBookshelfandthelinkingg devicearetrademarksofThePragmaticProgrammers,LLC. Every precaution was taken in the preparation of this book. However, the publisher assumesnoresponsibility for errorsor omissions,or for damagesthatmay result from theuseofinformation(includingprogramlistings)containedherein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatictitles,pleasevisitusat http://www.pragmaticprogrammer.com Copyright©2006BrianMarick. Allrightsreserved. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmit- ted, in any form, or by any means, electronic,mechanical,photocopying, recording, or otherwise,withoutthepriorconsentofthepublisher. PrintedintheUnitedStatesofAmerica. ISBN-10:0-9776166-1-4 ISBN-13:978-0-9776166-1-9 Printedonacid-freepaperwith85%recycled,30%post-consumercontent. Firstprinting,December2006 Version:2007-1-25 ToDawn,myBestBeloved,bestfriend,androlemodel Andtoshoemakers’childreneverywhere Contents 1 Introduction 13 1.1 How the Book Works . . . . . . . . . . . . . . . . . . . . . 15 1.2 An Outline of the Book . . . . . . . . . . . . . . . . . . . 16 1.3 Service After the Sale . . . . . . . . . . . . . . . . . . . . 17 1.4 Supplements . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.5 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . 18 2 Getting Started 19 2.1 Download the Practice Files. . . . . . . . . . . . . . . . . 19 2.2 In the Beginning Was the Command Line. . . . . . . . . 20 2.3 Do You Need to Install Ruby? . . . . . . . . . . . . . . . . 22 2.4 Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . 22 2.5 Your Two Basic Tools . . . . . . . . . . . . . . . . . . . . 23 2.6 Prompts, Command Lines, Prompts, and irb . . . . . . . 24 2.7 It’s Time to Make Mistakes . . . . . . . . . . . . . . . . . 26 I The Basics 29 3 A First Script: Comparing File Inventories 30 3.1 A Script in Action. . . . . . . . . . . . . . . . . . . . . . . 30 3.2 The Ruby Universe . . . . . . . . . . . . . . . . . . . . . . 31 3.3 Objects Send and Receive Messages . . . . . . . . . . . . 31 3.4 Variables Name Objects . . . . . . . . . . . . . . . . . . . 33 3.5 Comparing Arrays . . . . . . . . . . . . . . . . . . . . . . 34 3.6 Printing to the Screen . . . . . . . . . . . . . . . . . . . . 35 3.7 Making a Script. . . . . . . . . . . . . . . . . . . . . . . . 36 3.8 Where Do We Stand? . . . . . . . . . . . . . . . . . . . . 38 3.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 CONTENTS 9 4 Ruby Facts: Arrays 39 5 Three Improvements and a Bug Fix 43 5.1 Command-line Arguments . . . . . . . . . . . . . . . . . 43 5.2 Ignoring Case . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.4 Dissecting Strings . . . . . . . . . . . . . . . . . . . . . . 51 5.5 Fixing a Bug . . . . . . . . . . . . . . . . . . . . . . . . . 53 5.6 Where Do We Stand? . . . . . . . . . . . . . . . . . . . . 55 5.7 Prelude to the Exercises . . . . . . . . . . . . . . . . . . . 56 5.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 6 Ruby Facts: If, Equality Testing, and Unless 61 6.1 if ...elsif ...else . . . . . . . . . . . . . . . . . . . . . . . 61 6.2 When Are Objects Equal? . . . . . . . . . . . . . . . . . . 63 6.3 A Shorthand Version of if . . . . . . . . . . . . . . . . . . 63 6.4 unless . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 6.5 The Question Mark Operator . . . . . . . . . . . . . . . . 64 II Growing a Script 66 7 The Churn Project: Writing Scripts without Fuss 67 7.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . 67 7.2 Building a Solution . . . . . . . . . . . . . . . . . . . . . 69 7.3 Where Do We Stand? . . . . . . . . . . . . . . . . . . . . 91 7.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 8 Ruby Facts: Booleans 94 8.1 Other Boolean Operators . . . . . . . . . . . . . . . . . . 94 8.2 Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . 94 8.3 Every Object Is a Truth Value . . . . . . . . . . . . . . . 96 8.4 Boolean Expressions Can Select Objects . . . . . . . . . 96 9 Our Friend, the Regular Expression 98 9.1 Regular Expressions Match Strings . . . . . . . . . . . . 99 9.2 Dissecting Stringswith Regular Expressions . . . . . . . 101 9.3 Reordering an Array . . . . . . . . . . . . . . . . . . . . . 102 9.4 Where Do We Stand? . . . . . . . . . . . . . . . . . . . . 104 9.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 CONTENTS 10 10 Ruby Facts: Regular Expressions 106 10.1 Special Characters . . . . . . . . . . . . . . . . . . . . . . 106 10.2 Grouping and Alternatives . . . . . . . . . . . . . . . . . 108 10.3 Taking StringsApart . . . . . . . . . . . . . . . . . . . . . 108 10.4 Variables Behind the Scenes . . . . . . . . . . . . . . . . 109 10.5 Regular Expression Options . . . . . . . . . . . . . . . . 109 10.6 Wait, There’s More... . . . . . . . . . . . . . . . . . . . . 110 10.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 11 Classes Bundle Data and Methods 112 11.1 Classes Define Methods . . . . . . . . . . . . . . . . . . . 115 11.2 Objects Contain Data . . . . . . . . . . . . . . . . . . . . 116 11.3 Where Do We Stand? . . . . . . . . . . . . . . . . . . . . 120 11.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 12 Ruby Facts: Classes (with a Side Order of Symbols) 126 12.1 Defining Accessors . . . . . . . . . . . . . . . . . . . . . . 126 12.2 Self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 12.3 Class Methods . . . . . . . . . . . . . . . . . . . . . . . . 133 12.4 Class Variables and Globals . . . . . . . . . . . . . . . . 136 12.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 III Working in a World Full of People 138 13 Scraping Web Pages with Regular Expressions 139 13.1 Treating Web Pages Like Files . . . . . . . . . . . . . . . 140 13.2 Restricting Attention to Part of the Page . . . . . . . . . 142 13.3 Plucking Out the Title and Authors . . . . . . . . . . . . 144 13.4 Hashes StoreNamed Data . . . . . . . . . . . . . . . . . 146 13.5 Taking the Trip . . . . . . . . . . . . . . . . . . . . . . . . 147 13.6 Exercise Yourself . . . . . . . . . . . . . . . . . . . . . . . 149 14 Other Ways of Working with Web Applications 152 14.1 Handling XHTML . . . . . . . . . . . . . . . . . . . . . . . 152 14.2 Driving the Browser . . . . . . . . . . . . . . . . . . . . . 154 14.3 Direct Access to Underlying Protocols . . . . . . . . . . . 155

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.