Book Image

Selenium Design Patterns and Best Practices

By : Dima Kovalenko
Book Image

Selenium Design Patterns and Best Practices

By: Dima Kovalenko

Overview of this book

Table of Contents (18 chapters)
Selenium Design Patterns and Best Practices
Credits
Foreword
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Selenium Design Patterns and Best Practices will help you write better tests!

It does not matter whether you are writing a Selenium WebDriver test to test your website or shell scripts to test the HTTP API of the backend services of your multibillion dollar enterprise application. This book is not purely theoretical work, but comes from years of experience of the author and his colleagues. A lot of the practices and ideas written in this book did not appear as soon as we started to test the software. Instead, they came from years of mistakes, frustrations, and slow but continuous improvement. We do not believe that the examples and topics described in this book are definitive and static solutions to every single problem that you may encounter in your career. Instead, this book shows you some very generic solutions to very common problems that we, an ever-growing community of automated software testers, have encountered. We hope that this book will not only provide quick fixes to the problem(s) you may encounter, but will also empower you to solve more and more complex problems in your career by showing you some very simple improvement techniques.

What this book covers

Chapter 1, Writing the First Test, will guide us through the process of writing a simple Selenium test and converting it to a programming language.

Chapter 2, The Spaghetti Pattern, will help us write our second test that will completely depend on the test we wrote in the first chapter. We will understand why having tests that completely depend on each other is a bad practice.

Chapter 3, Refactoring Tests, will fix some of the pitfalls and common mistakes we encountered so far. This chapter will concentrate on the introduction of good computer programming practices such as code reuse.

Chapter 4, Data-driven Testing, will guide us through making some initial improvements to your test suite, and it will eventually prepare us to examine one of the most difficult problems in software testing: test data.

Chapter 5, Stabilizing the Tests, will help us understand that writing tests alone is not enough. We will dedicate this chapter to making our test bug free and resistant to random instabilities in the test environment.

Chapter 6, Testing the Behavior, will help you discover why testing the application in its current iteration becomes unmaintainable in the long run. Instead, we will start testing the desired behavior of the application, not the implementation.

Chapter 7, The Page Objects Pattern, covers one of the most undervalued and misunderstood topics when it comes to User Interface testing, that is, Page Objects. In this chapter, we will create a working Page Object testing framework and demonstrate how the tests can keep up with the new feature development cycle.

Chapter 8, Growing the Test Suite, will conclude this book with some basic tips on how to prioritize the growth of the test suite. Along the way, we will discuss how to keep our test stable and relevant to the whole team, no matter how often or big the changes are to the application being tested.

Appendix, Getting Started with Selenium, covers the initial setup of the user's computer. We will learn how to use the Command Line Interface terminal on Windows, Mac OSX, and Linux. We will install the Ruby programming language and Selenium WebDriver Ruby gem, followed by installation of the Firefox web browser. It concludes by explaining the test file and class nomenclature so that individuals new to the Ruby programming language can easily follow along with the tests.

What you need for this book

To get started with this book, you will need a basic understanding of what Selenium is, what it does, and basic programming skills. If you are able to create a simple click command in Selenium WebDriver and write a simple loop program in any programming language, you should be able to keep up with every example in this book. We will take the time to explain every line of code written in this book so that you are able to create the desired outcome in any situation you may find yourself in. There are some very small and simple software prerequisites that are needed. We will need to have access to the Command Line Interface terminal, Ruby runtime environment, and Firefox web browser. You can find the simple step-by-step setup instructions for all of these prerequisites in the Appendix, Getting Started with Selenium.

Who this book is for

This book is for anyone who wishes to write better automated tests. Whether you are writing your first Selenium test or have written hundreds of them, you will find this book useful to create a good test suite. However, this book is not only meant for writing better Selenium tests. A lot of the examples and techniques discussed in this book apply not only to Selenium WebDriver, but also to any automated tests written in any programming language.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "You can even open the search_test.html file in your web browser and see how it looks!"

A block of code is set as follows:

more_info_buttons = special_items.collect do |special_item|
  special_item.find_element(:class, "more-info")
end

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

require 'rubygems'
require 'selenium-webdriver'

selenium = Selenium::WebDriver.for(:firefox)
selenium.get("http://awful-valentine.com/")
selenium.find_element(:id, "searchinput").clear
selenium.find_element(:id, "searchinput").send_keys("cheese")
selenium.find_element(:id, "searchsubmit").click
selenium.quit

Any command-line input or output is written as follows:

ruby run_tests.rb

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on Install Now when it becomes clickable after several seconds."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

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 , 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 errata submission form 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 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 if you are having a problem with any aspect of the book, and we will do our best to address it.