Tải bản đầy đủ

Instant selenium testing tools starter

www.it-ebooks.info


Instant Selenium Testing Tools
Starter

A short, fast, and focused guide to Selenium Testing tools
that delivers immediate results

Unmesh Gundecha

BIRMINGHAM - MUMBAI

www.it-ebooks.info


Instant Selenium Testing Tools Starter
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: April 2013

Production Reference: 1170413

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-514-9
www.packtpub.com

www.it-ebooks.info


Credits
Author

Project Coordinator

Unmesh Gundecha

Joel Goveya

Reviewers

Proofreader

Vatsala Dorairajan

Aaron Nash



Murat Gocmen
Graphics
Acquisition Editor

Valentina D'silva

Usha Iyer
Production Coordinator
Commissioning Editor

Melwyn D’sa

Neha Nagwekar
Cover Work
Technical Editor

Melwyn D’sa

Jalasha D’costa
Cover Image
Conidon Miranda

www.it-ebooks.info


About the Author
Unmesh Gundecha has a Master’s Degree in Software Engineering and around 10 years
of experience in software development and testing. Unmesh has architected the functional
test automation projects using industry standards, 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 with a multinational company in Pune, India.
He is also the author of Selenium Testing Tools Cookbook, published by Packt Publishing in
November 2012.
I would like to thank my family who are always supportive in everything I do
and especially my two lovely kids, Ira and Arav. I would also like to thank the
Packt team and reviewers for giving the perfect shape to this book.
Finally, big thanks to Selenium Development and User Community for
building this wonderful tool.

www.it-ebooks.info


About the Reviewers
Vatsala Dorairajan is a budding software technologist. In her 4 years of work experience

spread across three startup companies, she has worked with ‘Idea’smiths, building ‘on paper’/’inconcept’ ideas into working prototypes evolving into products. Her technical experience so far
has been in Java, Flex, Python, and PHP. She is passionate about making classroom education an
absolutely fun experience and hopes to be an educationist one day.

Murat Gocmen is a QA Automation Engineer at IBM and has an overall experience of
5 years in the software industry. He has completed his Master’s in Computer Engineering
from Air Force Institute of Technology (AFIT) and has studied Bachelor of Science in Computer
Science and Engineering. He has worked in various sectors such as web commerce, retail,
and apartment-rental.

www.it-ebooks.info


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 Publishing 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
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 Publishing
books and eBooks.

www.it-ebooks.info


www.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 Packt
ÊÊ Copy and paste, print, and bookmark content
ÊÊ On demand and accessible via web browser

Free Access for Packt Publishing account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib
today and view nine entirely free books. Simply use your login credentials for immediate access.

www.it-ebooks.info


www.it-ebooks.info


Table of Contents
Instant Selenium Testing Tools Starter

1

So, what is Selenium?
3
The basic features of Selenium
3
What kind of things can you do with it?
5
How can you use this technology within your existing project?
5
Installation6
Step 1 – What do I need?
6
Step 2 – Downloading Selenium IDE
6
Step 3 – Installing Selenium IDE
7
And that's it!
9
Quick start
10
Step 1 – Recording and adding commands in a test
10
Step 2 – Saving the recorded test
12
Step 3 – Saving the test suite
13
Step 4 – Running the recorded test
13
Step 5 – Exporting a recorded test to Selenium WebDriver
14
Top 5 features you'll want to know about
22
1 – Running tests on various browsers
22
2 – Locating elements
26
3 – Working with HTML elements
29
4 – Synchronizing steps
30
Selenium IDE
Selenium WebDriver

30
31

5 – The Page Object pattern

33

www.it-ebooks.info


Table of Contents

People and places you should get to know
36
36
Official sites
Articles and tutorials
36
Community36
Blogs37
Twitter37

[ ii ]

www.it-ebooks.info


Instant Selenium Testing
Tools Starter
Welcome to Instant Selenium Testing Tools Starter. This book has been especially
created to provide you with all the information that you need to get up to speed
with Selenium IDE and Selenium WebDriver. You will learn the basics of Selenium,
get started with installing Selenium, creating a test suite and tests cases, and then
running these tests on your web application, using WebDriver, and some tips and
tricks for using Selenium.
This book contains the following sections:
So what is Selenium? helps you find out what Selenium actually is, what you can do
with it, and why it's so great.
Installation teaches you how to download and install Selenium with the minimum
fuss and then set it up so that you can use it as soon as possible.
Quick start teaches you how to record a test, save a test case, enhance a test by
adding commands, and run a test with Selenium IDE. This section will also get you
started on programming with Selenium WebDriver. Here you will learn how to
perform some core tasks in Selenium WebDriver such as creating an instance of
a desired browser. It explains interacting with page elements using WebElement,
adding verification points, and writing user defined methods.
Top 5 features you'll want to know about will introduce you to various features that
Selenium provides in order to automate your web applications. This section will
introduce you to some of the key features of Selenium such as running tests on
various browsers, locating elements, working with HTML elements, synchronizing
steps, and the Page Object pattern.
People and places you should get to know provides you with many useful links to
the project page and forums, as well as a number of helpful articles, tutorials,
blogs, and the Twitter feeds of Selenium super-contributors, as every open source
project is centered around a community.

www.it-ebooks.info


www.it-ebooks.info


Instant Selenium Testing Tools Starter

So, what is Selenium?
In this section, you will get to know a bit about Selenium; its basic features, what you can do
with it, and how you can put it to work with automating tasks in a browser window, or build
automated tests to validate your web application.

The basic features of Selenium
Selenium is a browser automation framework. It provides a number of tools and APIs for
automating user interaction on pure HTML and JavaScript applications in browsers such as IE,
Firefox, Google Chrome, Safari, and many more. However, Selenium does not support Rich
Internet Application (RIA) technologies such as Silverlight, Flex/Flash, and JavaFx out of the box.
Selenium offers the following set of tools for automating interaction with browsers:

ÊÊ Selenium IDE: This is a Firefox add-on for recording and playing back Selenium scripts
with Firefox. It provides a GUI for recording user actions using Firefox. It's a great tool
to start learning and using Selenium, but it can only be used with Firefox while other
browsers are not supported.
However, you can convert the recorded scripts into various programming languages
supported by Selenium WebDriver and run these scripts on browsers other than Firefox.

3

www.it-ebooks.info


Instant Selenium Testing Tools Starter
ÊÊ Selenium WebDriver: This is a programming interface for developing more advanced
Selenium scripts using different programming languages. You can also run tests on
multiple browsers supported by Selenium. The following figure provides a high-level
architecture of Selenium WebDriver:

Selenium WebDriver supports browsers including Mozilla Firefox, Google Chrome,
Microsoft Internet Explorer, Safari, and Opera.
It supports writing scripts with various programming languages including Java, .NET
Languages (C#, VB.NET), Python, Ruby, PHP, and JavaScript.
ÊÊ Selenium Standalone Server: This allows remote and distributed execution of Selenium
scripts. You can also use the Grid feature of a standalone server to run tests in parallel
and run tests on mobile platforms such as Android or Apple iOS for iPhone and iPad.

4

www.it-ebooks.info


Instant Selenium Testing Tools Starter

What kind of things can you do with it?
Selenium is widely used for automated testing of web applications; however, its usage is not
limited to testing. Selenium mimics user actions such as entering text into a text field, clicking
on buttons or links, selecting an option from a drop-down list, and many more, in a browser
window as if a human user is interacting with the application. Selenium is also used for screen
scraping and automating repetitive tasks in web applications.

How can you use this technology within your existing project
You can use Selenium for functional/acceptance testing of your web applications. You can create
automated regression tests using Selenium and run them whenever you need to test a new build
of your application.
You can use Selenium to automate repetitive tasks such as data entry, filling out forms, check
status, or perform complex navigation steps while manual testing.

5

www.it-ebooks.info


Instant Selenium Testing Tools Starter

Installation
In three easy steps, you can install Selenium IDE and get it set up on your system.

Step 1 – What do I need?
Before you install Selenium IDE, you will need to check that you have all of the required
elements, as listed:
ÊÊ Disk space: 500 MB free (minimum).
ÊÊ Memory: 1 GB (minimum).
ÊÊ Selenium IDE requires Mozilla Firefox web browser to be installed on the system.
ÊÊ For developing tests with Selenium WebDriver, you will need Eclipse IDE installed on
the system. You can download and install Eclipse from http://www.eclipse.org/
downloads/packages/eclipse-ide-java-developers/junosr2.

Step 2 – Downloading Selenium IDE
The easiest way to download Selenium IDE as a Firefox add-on is from
http://seleniumhq.org.
1. Start the Firefox browser and navigate to the Selenium home page at
http://seleniumhq.org and click on the Download tab as shown in the
following screenshot:

6

www.it-ebooks.info


Instant Selenium Testing Tools Starter
2. On the Download page, go to the Selenium IDE section and click on the version link
as shown in the following screenshot. We suggest that you download the most current
stable build and as of writing this book the most stable build is 1.10.0:

Step 3 – Installing Selenium IDE
Installing Selenium IDE is quick and easy, similar to installing any other add-on in Firefox.
Selenium IDE Version 1.10.1 is supported on Firefox Version 19:
1. After downloading, Firefox will request your permission to install the add-on. Click on
the Allow button as shown in the following screenshot:

7

www.it-ebooks.info


Instant Selenium Testing Tools Starter
2. Firefox will download the add-on and display the Software Installation dialog box as
shown in the following screenshot. Click on the Install Now button:

3. Firefox will install the Selenium IDE. Firefox will restart after the add-on is installed.
4. To launch the Selenium IDE, click on Firefox | Web Developer | Selenium IDE as shown
in the following screenshot. You might see a Firefox menu in some cases (on operating
systems such as Linux, Ubuntu, and Mac OS X), where you can open the Selenium IDE
by clicking on Tools | Selenium IDE from the Firefox main menu:

8

www.it-ebooks.info


Instant Selenium Testing Tools Starter
5. Selenium IDE will be launched on top of the Firefox window as shown in the
following screenshot:

And that's it!
By this point, you should have a working installation of Selenium IDE, and you should be free
to play around and discover more about it. Installing Selenium IDE is as simple as installing any
other add-on or extension for Firefox. In the next section, you will see the power Selenium IDE
has and how useful it is for automating and testing web applications.

9

www.it-ebooks.info


Instant Selenium Testing Tools Starter

Quick start
In this section we will show you how to record a test using Selenium IDE. During the recording,
we will add some additional commands to the test and run the recorded test. At the end of this
section, we will show you how to export the recorded test to Selenium WebDriver.
A test is a basic building block in Selenium IDE. It contains commands for navigation, test steps,
and checks for expected versus the actual state of the application. In this section we will show
you how to create your first test and execute this test with Selenium IDE.

Step 1 – Recording and adding commands in a test
In this section we will show you how to record a test on a demo e-commerce application. We will
test the product search feature of the application using the following steps:
1. Launch the Firefox browser.
2. Open the website for testing in the Firefox browser. For this example we will use
http://demo.magentocommerce.com/.
3. Open Selenium IDE from the Tools menu.
4. Selenium IDE by default sets the recording mode on. If it's not pressed, you can start
(record) button in the top-right corner.
recording by pressing the
5. Now switch back to the Firefox browser window and type Nokia in the search textbox
and click on the Search button as shown:

6. Check if the link Nokia 2610 Phone is present in the search results. We can do that by
selecting the link and opening the context menu (right-click) and selecting Show All
Available Commands | assertElementPresent link=Nokia 2610 Phone.

10

www.it-ebooks.info


Instant Selenium Testing Tools Starter

7. Next, we will click on the Nokia 2610 Phone link to open the product page and check
if the Nokia 2610 Phone text is displayed on the product page. To do this, select the
Nokia 2610 Phone text and open the context menu (right-click) and select Show All
Available Commands | assertTextPresent link=Nokia 2610 Phone:

11

www.it-ebooks.info


Instant Selenium Testing Tools Starter
8. Go back to Selenium IDE. All the previous steps are recorded by Selenium IDE in the
Command-Target-Value format as shown in the following screenshot. Stop the
recording session by clicking on the Recording button:

Step 2 – Saving the recorded test
Before we play back the recorded test, let's save it in Selenium IDE:
1. Select File | Save Test Case from the Selenium IDE main menu:

2. In the Save As dialog box, enter the test case name as SearchTest.html and click on
the Save button. The test will be saved with the name SearchTest.

12

www.it-ebooks.info


Instant Selenium Testing Tools Starter

Step 3 – Saving the test suite
In Selenium IDE, we can group multiple tests in a test suite. Let's create a test suite and
Selenium IDE will automatically add SearchTest to this suite:
1. Select File | Save Test Suite from the Selenium IDE main menu.
2. In the Save As dialog box, enter the test case name as SearchFeatureTests.html
and click on the Save button.
3. You can create and record more than one test case in a test suite.

Step 4 – Running the recorded test
Selenium IDE provides multiple ways to execute the tests:
ÊÊ Option 1 – running a single test case
1. Select the test which you want to execute from the test suite pane and click on
(play current test case) button .
the
2. Selenium IDE will start the playback of the test and you can see the steps
that we recorded earlier are being played automatically in the browser
window. At end of execution, Selenium IDE will display results as per the
following screenshot:

13

www.it-ebooks.info


Instant Selenium Testing Tools Starter
ÊÊ Option 2 – running all tests from a test suite
If you have multiple tests in a test suite, you can use the
button to play all the test cases.

(play the entire test suite)

After the test is executed in Selenium IDE, you can see the results in Log tab. All the
steps which are successfully completed will be heighted in green and checks in dark
green. If there are any failures in the test, those will be highlighted in red. This is how
Selenium IDE helps you testing your web application.

Step 5 – Exporting a recorded test to Selenium WebDriver
Selenium IDE is great to start, however, you will need more to build a test automation framework.
Complex tests, with conditional statements and logic, parameterization cannot be automated very
well using Selenium IDE. In addition to this you might need features such as detailed logging and
reporting, error handling particularly unexpected errors, database testing, and integration with
other testing tools and frameworks that are not supported by Selenium IDE.
For building a flexible, maintainable, and robust framework, you will need to use Selenium
WebDriver.
Selenium WebDriver is a simple API that helps you with browser automation. However, when
using it for testing and building a test framework, there is much more needed. You will need to
integrate Selenium WebDriver API with different libraries, tools, and so on, for test development.
You will need an integrated development environment (IDE) to build your test project and inject
other dependencies in to the framework.
We will show how to use Eclipse for developing tests with Selenium WebDriver. Eclipse is a
very popular IDE in the Java world. Eclipse provides a feature-rich environment for Selenium
WebDriver test development in Java. Along with Eclipse, Apache Maven also provides support
for managing the entire lifecycle of a test project. Maven is used to define project structure,
dependencies, build, and test management.
You can use Eclipse and Maven for building your Selenium WebDriver test framework from a
single window. Another important benefit of using Maven is that you can get all the Selenium
library files and their dependencies by configuring the pom.xml file. Maven automatically
finds and downloads the dependencies to the project from the Maven central repository while
building the project. In this section we will show you how to configure a new Maven project in
Eclipse with the following steps:
1. Launch the Eclipse IDE.
2. Create a new project by selecting File | New | Other from the Eclipse
main menu.

14

www.it-ebooks.info


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×