Web-testing automationmmsa.kpi.ua/sites/default/files/disciplines/Розробка і...
Transcript of Web-testing automationmmsa.kpi.ua/sites/default/files/disciplines/Розробка і...
Web-testing
automation Selenium IDE
Maryna Didkovska
1
To Automate or Not to Automate?
That is the Question!
Test automation supports:
Frequent regression testing
Rapid feedback to developers during the development process
Virtually unlimited iterations of test case execution
Customized reporting of application defects
Support for Agile and eXtreme development methodologies
Disciplined documentation of test cases
Finding defects missed by manual testing
2
Selenium Components Selenium is composed of three major tools. Each one has a specific role in aiding the development of web.
These tools are:
Selenium-IDE;
Selenium Remote Control (RC); Selenium WebDriver
Selenium Grid;
3
4
Selenium
Selenium is a robust set of tools that supports rapid
development of test automation for web-based
applications.
It is open source software, released under the Apache 2.0
license and can be downloaded and used without
charge.
5
Selenium-IDE
Selenium-IDE (Integrated
Development Environment) is a
Firefox add-on that records clicks,
typing, and other actions to make
a test, which you can play back
in the browser.
6
Selenium-RC
Selenium Remote Control
(RC) is a test tool that
allows to write automated
web application UI tests in
any programming
language against any HTTP
website using any
mainstream JavaScript-
enabled browser.
7
Selenium Grid Selenium Grid extends Selenium RC to distribute your tests
across multiple servers, saving you time by running tests in
parallel, cutting down on the time it takes to test multiple
browsers or operating systems.
With Selenium-Grid, multiple instances of Selenium-RC are
running on various operating system and browser
configurations.
Installation
http://seleniumhq.org/download/
8
9
10
11
12
Open the IDE
13
14
Building Test Cases During recording, Selenium-IDE will automatically insert
commands into your test case based on your actions.
Typically, this will include:
clicking a link - click or clickAndWait commands
entering values - type command
selecting options from a drop-down listbox - select
command
clicking checkboxes or radio buttons - click command
Task 1
Record script with following steps:
Open http://www.google.com.ua/
Search for web application testing
Goto Selenium site
Playback it
15
Base url :
http://www.google.com.ua/
Open /
type id=lst-ib web application
testing
clickAndWait name=btnK
clickAndWait
link=Selenium web
application testing
system
16
17
Running Test Cases Run a Test Case
Run a Test Suite
Stop and Start
Stop in the Middle
Start from the Middle
To set a startpoint, select a command, right-click, and from
the context menu select Set/Clear Start Point.
Run Any Single Command
Double-click any single command to run it by itself.
18
Using Base URL to Run Test
Cases in Different Domains
19
Selenium Commands –
Selenese
Selenium provides a rich set of commands for fully testing
your web-application in virtually any way you can imagine.
A command is what tells Selenium what to do.
The command set is often called selenese.
These commands essentially create a testing language.
20
Selenium Basics Selenium commands come in three “flavors”:
Actions are commands that generally manipulate the state of the application. They do things like “click this link” and “select that option”. If an Action fails, or has an error, the execution of the current test is stopped.
Accessors examine the state of the application and store the results in variables, e.g. “storeTitle”.
Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”.
21
Assertions All Selenium Assertions can be used in 3 modes: “assert”
“verify” “waitFor”
When an “assert” fails, the test is aborted. When a “verify” fails, the test will continue execution, logging the failure. “waitFor” commands wait for some condition to become true. They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout setting.
22
Script Syntax Selenium commands are simple, they consist of the command and
two parameters. For example:
The parameters are not always required; it depends on the
command.
Here are a couple more examples:
Command Target Value
verifyText //div//a[2] Login
Command Target Value
goBackAndWait
verifyTextPresent Welcome to My Home
Page
type id=phone (555) 666-7066
type id=phone ${myVariableAddress}
23
Parameters
Parameters vary, however they are typically:
a locator for identifying a UI element within a page.
a text pattern for verifying or asserting expected page
content.
a text pattern or a selenium variable for entering text in
an input field or for selecting an option from an option
list.
24
Locators For many Selenium commands, a target is required. This target identifies an element in the content of the web application, and consists of the location strategy followed by the location in the format
locatorType=location.
Locator Types:
id (id=loginForm);
name (name=username);
xpath (xpath=/html/body/form[1]);
link (link=Cancel);
dom (dom=document.forms[0]);
css (css=input.required[type="text"]);
25
Commonly Used Selenium
Commands
open opens a page using a URL
click/clickAndWait performs a click operation, and optionally waits for a new page to load
verifyTitle/assertTitle verifies an expected page title
verifyTextPresent verifies expected text is somewhere on the page
verifyElementPresent verifies an expected UI element, as defined by its HTML tag, is present on the
page
verifyText verifies expected text and it’s corresponding HTML tag are present on the
page
waitForPageToLoad pauses execution until an expected new page loads. Called automatically
when clickAndWait is used
waitForElementPresent pauses execution until an expected UI element, as defined by its HTML tag, is
present on the page
Matching Text Patterns -
Globbing Patterns Globbing is fairly limited. Only two special characters are supported in the Selenium implementation:
* which translates to “match anything,” i.e., nothing, a single character, or many characters.
[ ] (character class) which translates to “match any single character found inside the square brackets.” A dash (hyphen) can be used as a shorthand to specify a range of characters (which are contiguous in the ASCII character set).
examples:
[aeiou] matches any lowercase vowel
[0-9] matches any digit
[a-zA-Z0-9] matches any alphanumeric character
26
example of two commands that use globbing patterns. The actual link text on the page being tested was “Film/Television Department”; by using a pattern rather than the exact text, the click command will work even if the link text is changed to “Film & Television Department” or “Film and Television Department”. The glob pattern’s asterisk will match “anything or nothing” between the word “Film” and the word “Television”.
The actual title of the page reached by clicking on the link was “De Anza Film And Television Department - Menu”. By using a pattern rather than the exact text, the verifyTitle will pass as long as the two words “Film” and “Television” appear (in that order) anywhere in the page’s title.
Command Target Value
click link=glob:Film*Television Department
verifyTitle glob:*Film*Television*
27
Regular Expression Patterns PATTERN MATCH
. any single character
[ ] character class: any single character that appears inside the brackets
* quantifier: 0 or more of the preceding character (or group)
+ quantifier: 1 or more of the preceding character (or group)
? quantifier: 0 or 1 of the preceding character (or group)
{1,5} quantifier: 1 through 5 of the preceding character (or group)
| alternation: the character/group on the left or the character/group on the right
( ) grouping: often used with alternation and/or quantifier
28
Command Target Value
click link=regexp:Film.*Television
Department
verifyTitle regexp:.*Film.*Television.*
Command Target Value
open http://weather.yahoo.com/for
ecast/USAK0012.html
verifyTextPresent regexp:Sunrise: *[0-9]{1,2}:[0-
9]{2} [ap]m
Sunrise: * The string Sunrise: followed by 0 or more spaces
[0-9]{1,2} 1 or 2 digits (for the hour of the day)
: The character : (no special characters involved)
[0-9]{2} 2 digits (for the minutes) followed by a space
[ap]m “a” or “p” followed by “m” (am or pm)
29
Exact Patterns
select //select regexp:Real \*
select //select exact:Real *
It uses no special characters at all. So, if you
needed to look for an actual asterisk character
(which is special for both globbing and regular
expression patterns), the exact pattern would be
one way to do that.
30
Store Commands and
Selenium Variables
Command Target Value
store [email protected] userName
Command Target Value
verifyText //div/p ${userName}
Command Target Value
type id=login ${userName}
31
A little bit more about store storeElementPresent
This corresponds to verifyElementPresent. It simply stores a boolean value–”true” or “false”–depending on whether the UI element is found.
storeText
StoreText corresponds to verifyText. It uses a locater to identify specific page text. The text, if found, is stored in the variable. StoreText can be used to extract text from the page being tested.
32
echo - The Selenese Print
Command
Selenese has a simple command that allows
you to print text to your test’s output. This is
useful for providing informational progress
notes in your test which display on the
console as your test is running.
Command Target Value
echo Testing page
footer now.
echo Username is
${userName}
33
What to test? The simplest type of test, a content test, is a simple test for the existence of a static, non-changing, UI element. For instance Does each page have its expected page title?
This can be used to verify your test found an expected page after following a link.
Does the application’s home page contain an image expected to be at the top of the page?
Does each page of the website contain a footer area with links to the company contact page, privacy policy, and trademarks information?
Does each page begin with heading text using the <h1> tag? And, does each page have the correct text within that header?
34
Verify
Command Target Value
verifyTextPresent Marketing Analysis
Command Target Value
verifyElementPresent //div/p
verifyElementPresent id=Login
verifyElementPresent link=Go to Marketing Research
Command Target Value
verifyText //table/tr/td/div/p This is my text and it occurs right after the div inside the table.
Alt : // ~ css=
35
Task 2
Open http://seleniumhq.org
Goto link http://seleniumhq.org/download/
Assure that Title of the new page is
Downloads, when not – stop testing
Verify that text Download is present on the
page and is used with h2 tag
Assure that in table language Ruby is present
on the page, when not – stop testing
Verify that version 2.25.0 is written in the table
36
Tips and Tricks
Do I only check that the text exists on the page? (verify/assertTextPresent)
Do I only check that the HTML element exists on the page? That is, the text, image, or other content is not to be checked, only the HTML tag is what is relevant. (verify/assertElementPresent)
Must I test both, the element and it’s text content? (verify/assertText)
37
Task 3
Open http://www.google.com.ua/
Search for web application testing
Goto Selenium site
Save PageTitle of Selenium page into
variable – (store)
Print out the value of this variable (echo)
38
Task 4 Go to
http://demo.opensourcecms.com/wordpress/wp-login.php
Enter “admin” in the “Username” field
Enter “demo123″ in the “Password” field
Click on the “Log In” button
Verify that the text “Howdy, admin” is present
Click on the “Posts” link
Click on the “Add New” button
Type “Selenium Demo Post” in the title field
Click on the “Publish” button
Verify that the text “Post published” is present
39
Location Strategy the element ID
the element name attribute
an Xpath statement
document object model (DOM)
Generally, using an ID locator is more efficient as it makes your test code
more readable. Using the name attribute also has similar advantages.
These also give the best performance. Xpath statements have been
known to be slow in Internet Explorer due to limations of IE’s Xpath
processor.
Sometimes you must use an Xpath or DOM locator , i.e. if the page source
does not have an ID or name attribute.
There is an advantage to using Xpath or DOM, you can locate an object
with respect to another object on the page.
For example, if there is a link that must occur within the second paragraph
within a <div> section, you can use Xpath or DOM to specify this. With ID
and name locators, you can only specify that they occur on the page–
somewhere on the page. If you must test that an image displaying the
company logo appears at the top of the page within a header section
Xpath may be the better locator.
40
41
Find Button The Find button is used to see which UI element on the
currently displayed webpage (in the browser) is used in
the currently selected Selenium command. This is useful
when building a locator for a command’s first parameter
(see the section on locators in the Selenium Commands
chapter). It can be used with any command that identifies
a UI element on a webpage, i.e. click, clickAndWait, type,
and certain assert and verify commands, among others.
From Table view, select any command that has a locator
parameter. Click the Find button. Now look on the
webpage: There should be a bright green rectangle
enclosing the element specified by the locator parameter.
42
Locator Assistance
Selenium and Javascript JavaScript can also be used to help generate
values for parameters, even when the
parameter is not specified to be of type script.
Special syntax is required–the JavaScript snippet
must be enclosed inside curly braces and
preceded by the label javascript, as in
javascript {*yourCodeHere*}.
43
Command Target Value
store league of nations searchString
type q
javascript{storedVars[‘se
archString’].toUpperCase
()}
Task 5 Goto
http://www.theautomatedtester.co.uk/tutorials/selenium/Selenium_JavaScript.htm
Enter numbers into each of the text boxes and then click on one of the buttons
Compare it against what we think the answer is going to be. For this we need to use the verifyEval or assertEval commands. Your next step in the script should look a lot like the following: verifyEval | javascript{1+1} | ${total}
44
TBC…
See you next week!
45