Selenium Testing Tools Cookbook Second Edition Over 90 recipes to help you build and run automated tests for your web applications with Selenium WebDriver Unmesh Gundecha BIRMINGHAM - MUMBAI
Selenium Testing Tools Cookbook Second Edition Copyright 2015 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: October 2015 Production reference: 1261015 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78439-251-2 www.packtpub.com
Credits Author Unmesh Gundecha Project Coordinator Nidhi Joshi Reviewers Alexander Afanasyev Anuj Chaudhary Oliver Gondža Vatsala Dorairajan Acquisition Editor Tushar Gupta Content Development Editor Kirti Patil Technical Editor Rupali R. Shrawane Proofreader SaÞ s Editing Indexer Tejal Daruwale Soni Graphics Jason Monteiro Production Coordinator Manu Joseph Cover Work Manu Joseph Copy Editor Vatsal Surti
About the Author Unmesh Gundecha has a master's degree in software engineering and over 13 years of experience in software development and testing. He has architected functional test automation projects using industry-standard, in-house, and custom test automation frameworks, along with leading commercial and open source test automation tools. Presently, he is working as a test architect for a multinational company in Pune, India. Unmesh has also authored a book called Learning Selenium Testing Tools with Python, Packt Publishing.
About the Reviewers Alexander Afanasyev is currently a Python developer and architect and a QA automation lead at Avenues International Inc. This is a data and analytics company that provides consulting solutions. He has completed his master's of science degree in electronics engineering. He has several years of experience in development and testing at various companies across different sectors, both in Russia and the United States. Alexander has always had a passion for testing, code quality, and web technologies. In his free time, he is likely to be found contributing to StackOverß ow or GitHub, reading, or playing the guitar. I would like to thank Daniel Lyakovetsky for "opening doors," for guidance, and for the conþ dence that he has shown in me. A huge thank you to my wonderful wife, Anna, and my little daughter, Kate, who have all been a constant source of support.
Anuj Chaudhary is a software engineer who enjoys working on software testing and automation. He has vast experience in different testing methodologies such as manual, automated, performance, and security testing. He has worked as an individual contributor and a technical lead on various software projects dealing with all stages of the application development life cycle. Anuj has been awarded a Microsoft MVP two times in a row. He posts blogs at www.anujchaudhary.com. He has also reviewed the following books on Selenium: Selenium WebDriver Practical Guide (http://www.packtpub.com/seleniumwebdriver-practical-guide/book) Selenium Design Patterns and Best Practices (https://www.packtpub.com/ web-development/selenium-design-patterns-and-best-practices) Mastering Selenium WebDriver (https://www.packtpub.com/webdevelopment/mastering-selenium-webdriver) I would like to thank my wife Renu and son Arjun for always supporting me and letting me spend extra time on reviewing this book. Oliver Gondža is a Java enthusiast, extreme programmer, OSS contributor, and Red Hatter. Vatsala Dorairajan is a budding software technologist. She has mostly worked with "ideasmiths," transforming on-paper/in-concept ideas into working prototypes, which in turn have evolved into products. Her technical experience so far has been in Java, Flex, Python, PHP, and Ruby on Rails. She currently works with Jombay, an award-winning talent measurement and analytics company that helps companies hire, promote, and retain the right talent. Vatsala has also reviewed the book Selenium Testing Tools Starter, Packt Publishing.
www.packtpub.com Support Þ les, ebooks, discount offers, and more For support Þ les and downloads related to your book, please visit www.packtpub.com. Did you know that Packt offers ebook versions of every book published, with PDF and epub Þ les 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 service@packtpub.com 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. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read 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 a 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 9 entirely free books. Simply use your login credentials for immediate access.
Table of Contents Preface v Chapter 1: Getting Started 1 Introduction 1 ConÞ guring the Selenium WebDriver test development environment for Java with Eclipse and Maven 2 Using Ant for the Selenium WebDriver test execution 12 ConÞ guring Microsoft Visual Studio for Selenium WebDriver test development 15 ConÞ guring Selenium WebDriver for Python and Ruby 19 Setting up Internet Explorer Driver Server 22 Setting up ChromeDriver for Google Chrome 27 Setting up Microsoft WebDriver for Microsoft Edge 30 Chapter 2: Finding Elements 33 Introduction 33 Using browser tools for inspecting elements and page structure 34 Finding an element using the Þ ndelement method 41 Finding elements using the Þ ndelements method 46 Finding links 47 Finding elements by tag name 48 Finding elements using XPath 49 Finding elements using CSS selectors 58 Locating elements using text 63 Finding elements using advanced CSS selectors 65 Using jquery selectors 67 i
Table of Contents Chapter 3: Working with Elements 71 Introduction 71 Automating textboxes, text areas, and buttons 72 Checking an element's text 74 Checking an element's attribute and CSS values 76 Automating dropdowns and lists 77 Checking options in the Select element 81 Checking selected options in dropdowns and lists 83 Automating radio buttons and radio groups 86 Automating checkboxes 89 Working with WebTables 90 Chapter 4: Working with Selenium API 93 Introduction 93 Checking an element's presence 94 Checking an element's state 95 Using Advanced User Interactions API for mouse and keyboard events 96 Performing double-click on an element 98 Performing drag-and-drop operations 100 Working with context menus 101 Executing the JavaScript code 103 Capturing screenshots with Selenium WebDriver 105 Maximizing the browser window 107 Handling session cookies 108 Working with browser navigation 110 Working with WebDriver events 112 Chapter 5: Synchronizing Tests 117 Introduction 117 Synchronizing a test with an implicit wait 117 Synchronizing a test with an explicit wait 119 Synchronizing a test with custom-expected conditions 121 Synchronizing a test with FluentWait 123 Chapter 6: Working with Alerts, Frames, and Windows 127 Introduction 127 Handling a simple JavaScript alert box 128 Handling a conþ rm and prompt alert box 131 Identifying and handling frames 134 Working with IFRAME 139 Identifying and handling a child window 141 Identifying and handling a window by its title 144 Identifying and handling a pop-up window by its content 146 ii