Visual Basic for Testers MARY ROMERO SWEENEY APress Media, LLC Visual Basic for Testers Copyright ©2001 by Mary Romero Sweeney Originally published by Apress in 2001 All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permis sion ofthe copyright owner and the publisher. ISBN 978-1-893115-53-8 ISBN 978-1-4302-1138-9 (eBook) DOI 10.1007/978-1-4302-1138-9 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Editorial Directors: Dan Appleman, Gary Cornell, Karen Watterson, Jason Gilmore Technical Reviewer: Harvin Queen Marketing Manager: Stephanie Rodriguez Managing Editor: Grace Wong Editor: Kiersten Burke Production Editor: Kari Brooks Page Composition: Diana Van Winkle, Van Winkle Design Group Artist: Tony Jonick Cover Designer: Karl Miyajima Indexer: Valerie Perry The information in this book is distributed on an "as is" hasis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. For my Mom, Dolores Romero Leder: I love you, I admire you; you are my hero. Contents at a Glance Forewords By Elfriede Dustin ............................................................ xvii By James Bach ...................................................................... xviii Acknowledgments ................................................................................................ xix Introduction ...................................................................................................... xxi Part One In the Beginning: Preparing for Automated Script Writing ............................................... l Chapter 1 Automated Software Testing Overview ........................ 3 Chapter 2 Getting Started with Visual Basic on an Automated Testing Project ............................................. 17 Part Two The Second Stage: Introduction to Automated Script Writing ........................................ 37 Chapter 3 Introduction to the Visual Basic Development Environment .................................................. 39 Chapter 4 Understanding Visual Basic Application Essentials ................................................................................ 81 Part Three Beyond the Basics: Now You're Testing! ...................................................................................... 135 Chapter 5 Creating Test Utilities ................................................ 137 Chapter 6 Testing with the Windows Registry ......................... 183 Chapter 7 Introduction to the Windows Application Programmer's Interface (API) .................................... 211 v Contents Part Four Advanced Topics in Automated Test Scripting ................................ 253 Chapter 8 Introduction to Database Testing ........................... 255 Chapter 9 Introduction to Testing COM Components .............. 293 Chapter 10 Testing the Web with Visual Basic ......................... 357 Chapter 11 VB.NET: Brave New World ................................................ 407 Chapter 12 From Tester to Tester: Advice to the Visual Basic Automator .................. 453 Appendixes ........................................................................................................... 463 Appendix A Resources and References ............................................ .465 Appendix B More Controls ...................................................................... 471 Appendix C File Access and Management ........................................ .479 Appendix D Required Reading: Automated Testing Essays .... 489 Index ...................................................................................................................... 521 vi Contents Forewords By Elfriede Dustin ............................................................ xvii By James Bach ...................................................................... xviii Acknowledgments ................................................................................................ xix Introduction ...................................................................................................... xxi Part One In the Beginning: Preparing for Automated Script Writing .............................. 1 Chapter Automated Software Testing Overview .............. 1 3 What Is Automated Software Testing? ........................................................ 3 When to Automate? ............................................................................................... 5 Project and Personnel Issues ......................................................................... 5 Product Issues ................................................................................................... 6 Additional Test Management Issues ............................................................... 7 Automated Software Testing in the Real World .................................... 8 Building a Team for Automated Testing .................................................... 9 What a Tester Needs to Know about Visual Basic Scripting ........ 10 Test Scripts Are Software ............................................................................ 11 Goals of Good Testing Software ................................................................... 12 Creating Successful Testing Software ....................................................... 13 Why Visual Basic? ............................................................................................. 14 Limitations of Visual Basic for Testing ............................................. 15 Chapter 2 Getting Started with Visual Basic on an Automated Testing Project .................................................................................... 17 Installing Visual Basic ................................................................................ 17 Visual Basic Editions .................................................................................... 17 Visual Basic Installation Notes ................................................................. 18 Visual Basic Templates and Wizards ........................................................ 18 Using the Data Form Wizard .......................................................................... 19 Using Form Templates ...................................................................................... 25 vii Contents Using the Visual Database Tools: White Box Testing Using SQL ................................................................. 26 The T-SOL Debugger: Inspecting and Testing Stored Procedures ........................................................................................ 28 Exploring COM Objects Using the Object Browser .............................. 30 The Enterprise Tools ....................................................................................... 35 Part Two The Second Stage: Introduction to Automated Script Writing ....................... 37 Chapter 3 Introduction to the Visual Basic Development Environment ........................... 39 Objectives ............................................................................................................. 39 Visual Basic History ....................................................................................... 40 Getting Started with Visual Basic ......................................................... .41 Starting Up Visual Basic .............................................................................. 41 Using Visual Basic's Five IDE Windows ..................................................... .43 Visual Basic Application Development ...................................................... .45 Working with the Properties Window .......................................................... .48 Writing Visual Basic Code .................. ,. ........................................................ 55 Saving Your Visual Basic Program ............................................................... 60 Visual Basic Help ........................................................................................... 62 Customizing the Development Environment .................................................. 67 Exercise 3-1. A Very Simple Program .............................................. 70 Activity One: Creating the Very Simple Program ..................................... 70 Activity Two: Using a Message Box ............................................................. 71 Activity Three: Adding a Control ............................................................... 73 Activity Four: Saving Files ........................................................................ 74 To View the Suggested Answer to This Exercise ...................................... 74 Exercise 3-2. Adding Controls and Setting the Tab Index ........ 75 Tester's Checklist ............................................................................................ 79 Chapter 3 Review ................................................................................................ 80 Chapter 4 Understanding Visual Basic Application Essentials .............................. 81 Objectives ............................................................................................................. 81 Understanding Objects, Properties, Events, and Methods ............. 82 Understanding Properties .............................................................................. 83 viii Contents Understanding Events ...................................................................................... 86 Understanding Methods .................................................................................... 87 Understanding the Difference between Events and Methods .................... 89 Procedural Programming versus Event-Driven Programming ..................... 90 The Debugger ........................................................................................................ 93 Exercise 4-1. Using the Debugger to Understand Events ........... 95 Coding Mechanics ................................................................................................ 96 Object Naming Conventions ............................................................................ 97 Coding Conventions ......................................................................................... 98 Declaring and Using Variables ................................................................... 100 Identifying Shortcuts and Old Code ......................................................... 107 Local-Level, Module-Level, and Application-Level Variables ............ 108 Understanding Scope and Lifetime Variables .......................................... 111 Understanding Option Explicit ................................................................... 113 Constants ........................................................................................................ 115 Using Branching Statements ........................................................................ 117 Programming with Loops ................................................................................ 119 Code Review Practice .................................................................................... 122 Using the Shell Function to Run One Program from Another ................ 124 Creating an Executable File from a Visual Basic Project .................. 126 Exercise 4-2. Information Please ................................................. 128 Tester's Checklist .......................................................................................... 132 Chapter 4 Review .............................................................................................. 133 Part Three Beyond the Basics: Now You're Testing! ........................... 135 Chapter 5 Creating Test Utilities ......................................... 137 Objectives ........................................................................................................... 137 Creating and Using Procedures ................................................................. 138 Planning Your Procedures ............................................................................ 138 Creating and Using Subroutine Procedures .............................................. 140 Using Arguments ............................................................................................. 143 Understanding Function Procedures ........................................................... 147 Procedure Scoping ......................................................................................... 150 Creating and Using Standard Modules .................................................... 151 Adding Existing Modules to Projects ....................................................... 153 Understanding Form Modules ........................................................................ 155 Displaying Forms ........................................................................................... 157 Exercise 5-1. Adding a New Form Module ...................................... 159 Creating a Logging Utility ........................................................................ 160 ix Contents File Handling .................................................................................................... 161 Using The Microsoft Scripting Library to Access Files ............. 162 Setting a Reference to the Scripting Library ...................................... 162 Simple GUI Testing Using SendKeys ........................................................ 169 Adding Timing to Your Tests ...................................................................... 171 Using the Timer Control .............................................................................. 172 Documenting Your Tests ................................................................................ 174 Exercise 5-2. Testing the Calculator ........................................ 176 Tester}s Checklist .......................................................................................... 182 Chapter 5 Review .............................................................................................. 182 Chapter 6 Testing with the Windows Registry ............... 183 Objectives ........................................................................................................... 183 Introduction to the Windows System Registry ................................... 184 Creating and Saving Application Settings ......................................... 187 Retrieving Visual Basic Application Entries ................................... 190 Retrieving Single Registry Settings ....................................................... 190 Retrieving Multiple Settings ..................................................................... 191 Arrays and the GetAllSettings Function .................................................. 195 Displaying Multiple Values Using List Boxes ........................................ 197 Deleting a Registry Setting ...................................................................... 199 Error Trapping .................................................................................................. 200 Using the ERR Object .................................................................................... 202 Accessing Other Registry Keys ................................................................. 204 Exercise 6-1. Creating a Registry Management Utility ........... 205 Follow These Steps to Write the Code (Or Try It on Your Own) ........ 206 Tester} s Checklist .......................................................................................... 209 Chapter 6 Review .............................................................................................. 21 o Chapter 7 Introduction to the Windows Application Programmer's Interface (API) ............................ 211 Objectives ........................................................................................................... 212 Windows API Overview ..................................................................................... 212 Understanding Static and Dynamic Libraries .......................................... 212 The Windows API Dynamic-Link Library Files .......................................... 214 Exploring the Windows API Routines ........................................................ .215 Useful APis for Testing .............................................................................. 216 Using API Routines ......................................................................................... 216 Steps to Call a Windows API Routine ....................................................... 216 Calling API Routines with User-Defined Type (UDT) Arguments .......... 219 X Contents Using Windows API Functions for the Registry ................................ 222 Exercise 7-1. Get Your Platform's Version ................................ 227 Working with the Windows API: Beyond the Basics .......................... 231 Understanding Windows API Arguments ....................................................... 231 Understanding the Any Keyword ................................................................... 234 Understanding Aliasing ................................................................................ 234 ANSI versus Unicode ...................................................................................... 235 API Return Values and the Meaning of True and False ......................... 235 Writing API Wrappers .................................................................................... 237 Windows API Examples for Testing ............................................................. 239 Calling Other DLL Procedures ..................................................................... 248 Exercise 7-2. Using Advanced API calls ...................................... .249 Additional Work ............................................................................................. 251 Tester's Checklist ......................................................................................... 252 Chapter 7 Review .............................................................................................. 252 Part Four Advanced Topics in Automated Test Scripting .............. 253 Chapter 8 Introduction to Database Testing .................. 255 Objectives ........................................................................................................... 256 Database Application Testing Using the Visual Database Tools ............................................................. 256 Using the Data View Window ........................................................................ 257 Using the Query Builder Window to Execute Database Queries ............ 261 The Data Environment Designer ................................................................... 264 Testing Databases Using ActiveX Data Objects (ADO) ................... 264 Understanding ADO Architecture Basics .................................................... 266 Using the Connection Object ...................................................................... 268 Using the Recordset Object ........................................................................ 270 Revisiting the ODBC Logon Form Template ................................................ 274 Testing SQL Server Databases Using COM .............................................. 278 The Many Ways to Test Databases Using Visual Basic ................... 282 Exercise 8-1. Querying a Database Using Visual Database Tools and ADO ................................................. 283 Exercise 8-2. Adding Test Data ..................................................... 289 Exercise 8-3. Testing Using SQL-DMO ........................................... 291 Tester's Checklist ......................................................................................... 292 Chapter 8 Review .............................................................................................. 292 xi