Learning Selenium Testing Tools - Third Edition - Sample Chapter
Learning Selenium Testing Tools - Third Edition - Sample Chapter
Third Edition
Web technologies are becoming increasingly complex
and there is often a need to test your web applications
against a vast number of browsers and platforms, so
you need to build highly reliable and maintainable test
automation cases. Selenium is one of the most easy to
implement and efficient solutions for this.
This book will guide you through designing and
implementing the automation framework on Selenium
to build advanced automated test cases. You will start
with getting acquainted with the Selenium IDE, working with
AJAX, and using different methods to locate elements in
a web page. You will then move on to using PageObjects,
making tests maintainable, using WebDriver with different
browsers, and setting up Selenium Grid. You will also
be introduced to advanced topics such as working with
proxy servers, network intrusions, and more. By the end
of the book, you will have the skills to efficiently test your
web applications using Selenium. A well-detailed section
has been dedicated to teaching you the object-oriented
concepts with basics of core Java, which are the
pre-requisites for Selenium automation.
$ 44.99 US
27.99 UK
P U B L I S H I N G
Sa
m
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Raghavendra Prasad MG
Third Edition
Learning Selenium
Testing Tools
ee
Raghavendra Prasad MG
Raghu won an award for being the best performer in the keyword test automation
framework design and implementation at GTNexus. He also won an Individual Summit
award for Data Driven Test Automation Framework design and implementation at
MphasiS, an HP Company for GM (General Motors, a supply chain project).
Raghu won an Excellence in Execution award at Telelogic, an IBM Company. He is
passionate about test automation. You can reach him at his LinkedIn account,
Chapter 8, Mobile Devices, explains how Selenium WebDriver works on mobile devices
to test mobile websites or sites built with responsive web design.
Chapter 9, Getting Started with the Selenium Grid, shows us how we can set up our
Selenium Grid. We will also take a look at running tests in parallel to try bringing down
the time it takes to run tests.
Chapter 10, Advanced User Interactions, explains how to build chains of actions together
to help when you need to drag and drop or have key combinations working. We will also
look at how we can press a mouse button and hold it down while we move the mouse.
Chapter 11, Working with HTML5, explains working with some of the HTML5
technologies that are becoming available to browsers. The Selenium WebDriver APIs are
very similar to the JavaScript APIs in the browser in order to make use of them easier.
Chapter 12, Advanced Topics, explains how to capture network traffic between the
browser and the web server. We finish off by capturing screenshots.
Chapter 13, Migrating from Remote Control to WebDriver, introduces how the
interaction with the browser has changed and how we can convert our Selenium 1 tests
to Selenium 2 to take advantage of the changes in Selenium WebDriver.
Appendix A, Automation Prerequisites for Selenium Automation, deals with the basics of
Java and prerequisites related to programming, which is mandatorily required for
automation testing. We are giving an automation prerequisites-related program of
examples with this book.
Appendix B, Answers for Self-test Questions, has the answers to all the self-test questions
in the previous chapters.
Debugging tests
[8]
Chapter 1
4. You will then be asked if you would like to install Selenium IDE and
the exporter add-ons. These have been made pluggable to the IDE by the
work that Adam Goucher did. You will see a screenshot similar to the
following one:
5. Click on Install button. This will now install Selenium IDE and formatters as
Firefox add-ons.
[9]
6. Once the installation process is complete, it will ask you to restart Firefox.
Click the Restart Now button. Firefox will close and then reopen. If you
have anything open in another browser, it might be worth saving your
work as Firefox will try to go back to its original state. However, this
cannot be guaranteed.
7. Once the installation is complete, the add-ons window will show the
Selenium IDE and its current version:
You have successfully installed Selenium IDE and we can start thinking about
writing our first test.
[ 10 ]
Chapter 1
Starting from the top, I will explain what each of the items are:
Base URL: This is the URL that the test will start at. All open commands will
be relative to Base URL unless a full path is inserted in the open command.
Speed Slider: This is the slider under the Fast and Slow labels in
the screenshot.
: This play entire test suite icon runs all the tests in the IDE.
: This play current test case icon runs a single test in the IDE.
: This step icon steps through the test once it has paused.
: This is the record button. This will be engaged when the test is recording.
[ 11 ]
The Command drop-down list has a list of all the commands that are needed
to create a test. You can type into it to use the autocomplete functionality or
use it as a dropdown.
The Target textbox allows you to input the location of the element that you
want to work against.
The Find button, once the target box is populated, can be clicked to highlight
the element on the page.
The Value textbox is where you place the value that needs to change. For
example, if you want your test to type in an input box on the web page, you
will put what you want it to type in the value box.
The Table tab will keep track of all your commands, targets, and values. It
has been structured this way because the original version of Selenium was
styled on FIT tests. FIT (Framework for Integrated Testing) was created by
Ward Cunningham. The tests were originally designed to be run from HTML
files and the IDE keeps this idea for its tests.
If you click the Source tab, you will be able to see the HTML that will store
the test. Each of the rows will look like this:
<tr>
<td>open</td>
<td>/chapter1</td>
<td></td>
</tr>
The area below the Value textbox will show the Selenium log while the
tests are running. If an item fails, then it will have an [error] entry. This
area will also show help on Selenium commands when you are working in
the Command drop-down list. This can be extremely useful when typing
commands into Selenium IDE instead of using the record feature.
The Log tab will show a log of what is happening during the test. The
Reference tab gives you documentation on the command that you have
highlighted, and is also useful if you forgot some command; users can just
start writing command in the Command field, then select some like searched
and read the reference.
[ 12 ]
Chapter 1
Tests should always have a known starting point. In the context of Selenium,
this can mean opening a certain page to start a workflow.
Tests should not have to rely on any other tests to run. If a test is going
to add something, do not have a separate test to delete it. This is to ensure
that if something goes wrong in one test, it will not mean you have a lot
of unnecessary failures to check.
These rules, like most rules, can be broken. However, breaking them can mean that
you may run into issues later on, and when you have hundreds, or even thousands
of tests, these small issues can mean that large parts of a test suite are failing.
With these rules in mind, let's create our first Selenium IDE test.
3. Your test has now been recorded and should look like the preceding
screenshot.
4. Click the play button that looks like this icon:
[ 14 ]
Chapter 1
5. Once your test has completed, it will look like the following screenshot:
We have successfully recorded our first test and played it back. As we can see,
Selenium IDE tried to apply the first rule of test automation by specifying the open
command. It set the starting point of the test, in this case, /chapter1, and then it
began stepping through the workflow that we want to record.
Once the actions have all been completed, you will see that all of the actions have a
green background. This shows that they have completed successfully. On the left,
you will see that it has completed one successful test, or run, within Selenium IDE.
If you were to write a test that failed, the Failure label will have a 1 next to it.
There are two mechanisms for validating elements available on the application under
test. The first is assert: this allows the test to check whether the element is on the
page. If it is not available, then the test will stop on the step that failed. The second
is verify: this allows the test to check if the element is on the page, but if it isn't, then
the test will carry on execution. To add the assert or verify commands to the tests, we
need to use the context menu that Selenium IDE adds to Firefox. All that one needs
to do is right-click on the element if on Windows or Linux. If you have a Mac, then
you will need to do the two-finger click to show the context menu.
When the context menu appears, it will look roughly like the following screenshot
with the normal Firefox functions above it:
We will record the test and learn how to use/verify some commands as follows:
1. Open the IDE so that we can start recording.
2. Navigate to http://book.theautomatedtester.co.uk/chapter1.
3. Select Selenium Grid from the drop-down box.
4. Change the selection to Selenium Grid.
5. Verify that the Assert that this text is on the page text is mentioned on the
right-hand side of the drop-down box, by right-clicking on the text and
selecting verifyText id=diveontheleft Assert that this text is on the page.
You can see the command in the previous screenshot.
6. Verify that the button is on the page. You will need to add a new command
for verifyElementPresent with the verifybutton target in Selenium IDE.
[ 16 ]
Chapter 1
7. Now that you have completed the previous steps, your Selenium IDE should
look like the following screenshot:
If you now run the test, you will see it has verified that what you are expecting to
see on the page has appeared. Notice that the verify commands have a darker green
color. This is to show that they are more important to the test than moving through
the steps. The test has now checked that the text we required is on the page and that
the button is there too.
[ 17 ]
What will happen if the verify command did not find what it was expecting? The IDE
would have thrown an error stating what was expected was not there, but then carried
on with the rest of the test. We can see an example of this in the following screenshot:
The test would not have carried on if it was using assert as the mechanism for
validating that the elements and text were loaded with the page.
We have just seen that we can add asserts or verification to the page. Selenium
IDE does not do this when recording, so it will always be a manual step. The assert
command will stop the running tests, unlike the verify command in which the tests
will continue running even after failure. In both the cases, Selenium IDE will log an
error. Each of these have their own merits.
Some of the verify and assert methods are as follows:
Selenium Command
Command Description
verifyElementPresent
assertElementPresent
verifyElementNotPresent
[ 18 ]
Chapter 1
Selenium Command
Command Description
assertElementNotPresent
verifyText
assertText
verifyAttribute
assertAttribute
verifyChecked
assertChecked
verifyAlert
assertAlert
verifyTitle
assertTitle
[ 19 ]
[ 20 ]
Chapter 1
We just had a look at how to create comments. Comments will always appear
as purple text in the IDE. This, like in most IDEs, is to help you spot comments
quicker when looking through your test cases. Now that we know how to keep
our tests maintainable with comments, let's carry on working with Selenium IDE
to record/tweak/replay our scripts.
Multiplying windows
Web applications, unfortunately, do not live in one window of your browser. An
example of this can be a site that shows reports. Most reports will have their own
window so that people can easily move between them.
Unfortunately, in testing terms, this can be quite difficult to do, but in this section,
we will have a look at creating a test that can move between windows.
[ 21 ]
4. Add a verify command for an element on the page. Your test should now
look like the following screenshot:
[ 22 ]
Chapter 1
Once the test has finished with the pop-up window, it will need to return to the
parent window from where it started. To do this, we need to specify null as the
window. This will force the selectWindow command to move the context of the
test back to its parent window.
[ 23 ]
9. Run your test and watch how it moves between the windows. When
complete, it should look like the following screenshot:
We just had a look at creating a test that can move between multiple windows. We
saw how we can move between the child windows and its parent window as though
we were a user.
[ 24 ]
Chapter 1
[ 25 ]
5. Run the test that you have created. When it has finished running, it should
look like the following screenshot:
Have a look at the page that you are working against. Can you see the text that the
test is expecting? You should see it, so why has this test failed? The test has failed
because when the test reached that point, the element containing the text was not
loaded into the DOM. This is because it was being requested and rendered from
the web server into the browser.
To remedy this issue, we will need to add a new command to our test so that our
tests pass in the future:
1. Right-click on the step that failed so that the Selenium IDE context
menu appears.
2. Click on Insert New Command.
[ 26 ]
Chapter 1
Selenium does not implicitly wait for the item that it needs to interact with, so it is
seen as good practice to wait for the item you need to work with and then interact
with it. The waitFor commands will timeout after 30 seconds by default, but if you
need it to wait longer, you can specify the tests by using the setTimeout command.
This will set the timeout value that the tests will use in future commands.
If need be, you can change the default wait if you go to Options | Options and
then on the General tab, and under Default timeout value of recorded command
in milliseconds (30s = 30,000ms) change it to what you want. Remember, 1,000
milliseconds = 1 second.
[ 27 ]
In the previous examples, we waited for an element to appear on the page; there
are a number of different commands that we can use to wait. Also, remember that
we can take advantage of waiting for something not to be on the page, for example,
waitForElementNotPresent. This can be just as effective as waiting for it to
be there.
[ 28 ]
Chapter 1
The following commands make up the waitFor set of commands, but this is not an
exhaustive list:
Selenium Command
waitForAlertNotPresent
Command Description
waitForAlertPresent
waitForElementPresent
waitForElementNotPresent
waitForTextPresent
waitForTextNotPresent
waitForPageToLoad
waitForFrameToLoad
A number of these commands are run implicitly when other commands are being
run. An example of this is the clickAndWait command. This will fire off a click
command and then fire off a waitForPageToLoad command. Another example is
the open command, which only completes when the page has fully loaded.
If you are feeling confident, then it's a good time to try different waitFor command
techniques.
[ 29 ]
Once the element has been stored, you will be able to use it again by requesting it
from a JavaScript dictionary that Selenium keeps track of. To use the variable, it
will take one of the following two formats: it can look like ${variableName} or
storedVars['variableName']. I prefer the storedVars format as it follows the
same format as within Selenium internals. To see how this works, let's work
through the following example:
1. Open up Selenium IDE and switch off the record button.
2. Navigate to http://book.theautomatedtester.co.uk/chapter1.
3. Right-click on the text Assert that this text is on the page and go to the
storeText command in the context menu and click on it.
4. A dialog will appear as shown in the following screenshot. Enter the name
of a variable that you want to use. I have used textOnThePage as the name
of my variable.
[ 30 ]
Chapter 1
Once your test has completed running, you will see that it has placed Assert that this
text is on the page into the textbox.
Debugging tests
We have successfully created a number of tests and have seen how we can work
against AJAX applications, but unfortunately, creating tests that run perfectly the
first time can be difficult. Sometimes, as a test automator, you will need to debug
your tests to see what is wrong.
To work through this part of the chapter, you will need to have a test open in
Selenium IDE.
[ 31 ]
These two steps are quite useful when your tests are not running and you want
to execute a specific command. They are:
1. Highlight a command.
2. Press the X key. This will make the command execute in Selenium IDE.
When a test is running, you can press the pause button to pause the test after the
step that is currently being run. Once the test has been paused, the step button is no
longer disabled and you can press it to step through the test as if you were stepping
through an application.
If you are having issues with elements on the page, you can type in their location
and then click on the Find button. This will surround the element that you are
looking for with a green border that flashes for a few seconds. It should look like
the following screenshot:
The echo command is also a good way to write something from your test to the
log. This is equivalent to the Console.log code in JavaScript, for example, echo |
${textOnThePage}, as shown in the following screenshot:
[ 32 ]
Chapter 1
Also, remember that if you are trying to debug a test script that you have created with
Selenium IDE, you can set breakpoints in your test. You simply right-click on the line
and select breakpoint from the list. It will be similar to the following screenshot:
You can also use the keyboard shortcut of B to allow you to do it quicker.
[ 33 ]
You can do this as many times as you want, and when the Play entire test suite
button is clicked, it will run all the tests in the test suite. It will log all the passes
and failures at the bottom of the Test Case box.
To save this, click on the File menu and then click Save Test Suite and save the test
suite file to a place where you can get to it again. One thing to note is that saving a
test suite does not save the test case. Make sure that you save the test case every time
you make a change and not just the test suite.
[ 34 ]
Chapter 1
To change the name of the test case to something a lot more meaningful, you can
do this by right-clicking on the test and clicking on the Properties item in the
context menu:
You can now add meaningful names to your tests and they will appear in Selenium
IDE instead of falling back to their filenames.
We have managed to create our first test suite. This can be used to group tests
together to be used later. If your tests have been saved, you can update the test
suite properties to give the tests a name that is easier to read.
Parameterization
Parameterization is a key feature in automation testing. It is possible to give test
data (parameter) as input in Selenium IDE as it is with any other automation tool.
Selenium IDE requires configuration before proceeding for parameterization,
which is explained in the following section.
After configuring Selenium IDE for parameterization, in the following example, we
will search a text on Google and verify the text appears on the page. The storeEval
command will read the search data from the google_search.js file and save it in
the searchDataValue variable. With this variable, data types on the Google search
page, verify Download Selenium IDE text using the assertTextPresent command.
In this way, you can achieve data parameterization or data-driven testing by passing
external data to a variable in Selenium IDE.
[ 35 ]
5. Now, record the script to perform Google search and verify the page element
and update the commands as shown in the following screenshot. Then,
execute the test. You can also use the WaitForElementPresent command
till the page element is loaded and verify instead of the pause command.
[ 36 ]
Chapter 1
In this example, we are searching a text on the Google page and verifying the text
appearing on the page. The storeEval command will read the search data from the
google_search.js file and store it in a searchDataValue variable and verify the
Download Selenium IDE text using an assertTextPresent command. In this way,
you can achieve data parameterization or data-driven testing in Selenium IDE.
You need to set enable experimental features on Selenium IDE options to view the
formatters. Check out the following screenshot for formatters:
[ 38 ]
Chapter 1
Record user actions on Selenium IDE, and to view specific programming language
formatters, select the Selenium IDE | Options | Format as shown in the following
screenshot. A confirmation dialog appears, click on OK:
[ 39 ]
Ensure that the code is displayed under the Selenium IDE Source tab as seen in the
screenshot that follows. You can also see the Java/Junit4/WebDriver programming
language formatter. Selenium IDE used the type command to identify text typing
into the textbox. In the source code, the sendkeys command is used in order to type
text into the textbox using the Selenium Java/Junit4/WebDriver programming
language. You can generate a formatter of any of the specific programming
languages. After viewing the source code, you can switch back from a specific
formatter to HTML. If a formatter is not HTML, then recording options will not
work and the Table tab will not be active:
[ 40 ]
Chapter 1
In the preceding recorded action, we searched a text on Google and verified the
text appears on the page. Selenium IDE exports these actions to view a specific
programming language. This is one of the useful features of Selenium IDE; it
provides an option to set the clipboard format. You can copy commands from
the Table tab and paste in the format of that specific programming language.
Saving tests
Saving tests is done in the same manner as saving a test suite. Click on the File menu
and then click Save Test Case. This will give you a Save dialog box; save this to a
place where you can get to it later. When you save your tests and your test suite,
Selenium IDE will try to keep the relationships between the folders in step when
saving the tests and the test suites.
[ 41 ]
Selenium IDE does not work with Canvas elements on the page either,
so you will not be able to make your tests move items around on a page.
Selenium cannot do file uploads. This is due to the JavaScript sandbox not
allowing JavaScript to interact with <input type=file> elements on a page.
While you might be able to send the text to the box, it will not always do
what you expect, so I would recommend not doing it.
Summary
We learned a lot in this chapter about Selenium IDE, learning how to create your first
test using the record and replay functions and we now understand some of the basic
concepts such as moving between multiple windows that can appear in a test, and
saving our tests for future use.
Specifically, we covered the following topics:
What Selenium IDE is made up of: The breakup of Selenium IDE allowed
us to see what makes up Selenium IDE. It allowed us to understand the
different parts that make up a command that will be executed in a test as well
as its basic format. We had a look at how to load Selenium IDE and how to
get started with recording tests. We saw that a Selenium IDE command is
made up of three sections: the command, the target, and the value that might
be used.
How to add comments to tests: In this section of the chapter, we saw how to
add comments to the tests so that they are more maintainable.
[ 42 ]
Chapter 1
Working with AJAX applications: AJAX applications do not have the items
needed for the tests when the tests get to commands. To get around this, we
had a look at adding waitFor commands to the tests. This is due to the fact
that Selenium does not implicitly wait for elements to appear on the page.
Debugging tests: Creating tests does not always go according to the plan,
so in this section, we saw some of the different ways to debug your tests.
Saving test suites: Finally, we saw how we can save tests for future use and
how we can save them into different groups by saving them into test suites.
We also discussed what cannot be tested using Selenium IDE. We saw that
Silverlight and Flex/Flash applications cannot be tested, and that when working
with a number of HTML5 elements, the tests say that they have completed the tasks
even though the UI has not changed. In later chapters, we will discuss different
mechanisms that we can use within our tests that might be useful against HTML5
elements on the page.
Now that we've learnt about Selenium IDE, we're ready to look at all the different
techniques we can use to find elements on the page.
Self-test questions
1. What is the main language that drives Selenium IDE?
1. Ruby.
2. Python.
3. JavaScript.
2. Selenium IDE works on Internet Explorer.
1. True.
2. False.
[ 43 ]
[ 44 ]
www.PacktPub.com
Stay Connected: