Vous êtes sur la page 1sur 38

advanced selenium

NEAL FORD software architect / meme wrangler

ThoughtWorks ®

nford@thoughtworks.com
3003 Summit Boulevard, Atlanta, GA 30319
www.nealford.com
www.thoughtworks.com
blog: memeagora.blogspot.com
twitter: neal4d
Questions, Slides, and Samples

Please feel free to ask questions anytime

The slides and samples will be available at


www.nealford.com

I’ll show that address again at the end


Samples denoted via => document_name
What This Session Covers

Selenium’s scope
TestRunner techniques
Remote Control
Ajax
IDE
Extensions
Best practices
Selenium’s Scope
Selenium is an acceptance testing tool for
web applications

Selenium tests are fragile by design


Selenium Test Runner Mode

Selenium Core

Selenium Test
Runner Application
Under Test
The Selenium IDE
FireFox extension
Not just a recorder
Intelligent field selection
Autocomplete for all
common Selenium
commands
Walk through tests
Debug and set
breakpoints
String Matching Patterns
glob:pattern
Match a string against a "glob" (aka "wildmat") pattern.
"*" represents any sequence of characters
"?" represents any single character.
Glob patterns match against the entire string.
regexp:regexp
Match a string using a regular-expression.
The full power of JavaScript regular-expressions is available.
exact:string
Match a string exactly, verbatim, without any of that fancy
wildcard stuff.
If no pattern prefix is specified, Selenium assumes that it's
a "glob" pattern.
=> End 2 End Test
TestRunner Techniques
End-to-end Testing

Example of end to end testing => End 2


end Test

Always make sure you leave your


application in a known good state
Generating Unique Values
The problem: you need unique user
names

Solution: create user names based on


time values

'fred ' + (new Date().getTime())


Data Driven Tests
Test cases (and suites) don’t have to be
static HTML
Verifying # of Rows in a Table
Assert that nth row exists and that nth+1
does not
assertElementPresent //table[@id='mytable']/tr[10]

assertElementNotPresent //table[@id='mytable']/tr[11]

Use the assertXPath extension to count


columns
assertXpath count(//table[@id='mytable']/tr) 10
Parameters and Variables
Parameter and variable declarations range
from simple values to Javascript evaluation

store, storeValue, and storeText


store values for later access

Internally, Selenium uses a map called


storedVars, keyed to variable names
store, storeValue
store( valueToStore, variableName )
Stores a value into a variable.
Variable substitution or javascript evaluation

storeValue( inputLocator, variableName )


Stores the value of an input field into a
variable.
storeText, storeAttribute
storeText(elementLocator, variableName )
Stores the text of an element into a variable.

storeAttribute
(elementLocator@attributeName,
variableName)
Stores the value of an element attribute into a
variable.
Variable Substitution
Provides a simple way to access variables
using ${xxx} syntax
JavaScript Evaluation
You can use JavaScript to construct
whatever values you want

The entire parameter value is prefixed with


‘javascript{‘ with a trailing ‘}’

Text inside the braces is a pure JavaScript


expression
Harvesting Values
Variables allow you to harvest information
from an “information only” page
Steps:
Build a page with values
On startup of the test, harvest those values
Use the variables for assertions in subsequent
tests

=> harvesting values


Selenium
Remote
Control
Selenium Remote Control

Selenium Core

JUnit
Proxy Server
Test Application
Under Test
Interactive Selenium
Start the proxy
Create an instance of the browser
The proxy will provide a unique ID that must be
used in all subsequent commands
You’ll see a separate instance of the browser
launch
Issue Selenium commands
When done, kill the browser instance
=> Interactive Selenium
Test Code Reuse
One of the advantages of remote control

Create “helper” methods for common tasks


Login helper => DecisionDemo

When designing tests


Keep them modular

Refactor frequently
Multiple Language Support
The Selenium IDE generates tests in a
variety of languages
All your tests don’t have to be in the same
language
Java == Ruby example =>
Selenium Remote Control, Selenium Remote
Control Ruby
Use the language that makes sense for the
situation
Decisions
TestRunner tests can’t make deciscions
The Selenium language was designed to be
strictly declarative, not imperative
In remote control, you write unit tests
Allows you to make testing decisions
Firefox makes you logon, IE doesn’t
Different roles for different users
Remote control gives you imperative tests
in Selenium
=> Decision Demo
The Selenium IDE
More than just a pretty face!
Supports multiple languages
TestRunner (declarative)
Java, Ruby, C#, Python, etc. (imperative)

A round-trip tool…
…but only for declarative tests
Don’t make the imperative plunge lightly
Ajax
Because Selenium works directly with the
DOM, testing Ajax is easy
Testing XmlHttpRequest =>
Testing collapsable divs => End 2 End Test
Testing absence of controls =>
Extending Selenium
Selenium allows you to add your own
actions, checks, and locator strategies

Selenium uses naming patterns to discover


extensions at run-time
Actions
All methods in the form of doFoo are added
as actions
For each foo, and fooAndWait is created
Checks
All assertFoo methods are added as checks
For each foo, you get assertFoo & verifyFoo
Locator Strategies
All locateElementByFoo methods on
PageBot are added as locator strategies
Locators take 2 parameters
Locator string (minus the prefix)
Document in which to search

Add a "valuerepeated=" locator, that finds


the first element a value attribute equal to
the the supplied value repeated.
Custom Locator
User Extensions
By default, Selenium looks for a file called
"user-extensions.js", and loads the
javascript code found in that file.

A convenient location for adding features to


Selenium, without needing to modify the
core Selenium sources.

This file doesn’t exist by default (you’ll have


to add it)
Handy User Extensions
Alert =>
Interactive debugger =>
GUI_Map =>
Allows you to handle auto-generated fields
names more gracefully
Include =>
Check out
http://wiki.openqa.org/display/SEL/Contributed+User-Extensions
Uploading Files
Normally, JavaScript permissions block you
from filling in an input path for a file upload
Workaround:
Set the browser property
“signed.applets.codebase_principal_support” to true
Add “netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);” to
selenium-api.js in function Selenium.prototype.doType

Currently only works in Firefox


Selenium Best Practices
Create modular test methods for remote
control
Allows for test code reuse
Easier to refactor
Not as fragile
Record acceptance tests
End users/business analysts only have to verify
it once
Regression tests catch errors
Selenium Best Practices
Write selenium tests late in the
development cycle

User acceptance tests are fragile

Require lots of refactoring

The danger is that you’ll stop running


regression tests if they break too much
Questions?
Please fill out the session evaluations
Samples & slides at www.nealford.com
NEAL FORD thoughtworker / meme wrangler

ThoughtWorks
14 Wall St, Suite 2019, New York, NY 10005

This work is licensed under the Creative Commons Attribution- nford@thoughtworks.com


www.nealford.com
Noncommercial-Share Alike 2.5 License. www.thoughtworks.com
memeagora.blogspot.com
http://creativecommons.org/licenses/by-nc-sa/2.5/

Vous aimerez peut-être aussi