Sie sind auf Seite 1von 90

How To Use Selenium,

Successfully

by Dave Haeffner, @TourDeDave


http://www.wpclipart.com/geography/features/chasm.png.html
http://en.wikipedia.org/wiki/Optimal_solutions_for_Rubik's_Cube
Write business valuable tests that are
reusable, maintainable and resilient
across all relevant browsers.

Then package and scale them for


you & your team.
Selenium Overview

What it is the Readers Digest version

What it is and is not good at

IDE vs. Local vs. Remote

Slow, brittle, and hard to maintain?


Step 1
Define a Test Strategy
Test Strategy
1. How does your business make money?

2. What features of your application are being used?

3. What browsers are your users using?

4. What things have broken in the app before?

Outcome:
- What features to test
- Which browsers to care about
Step 2
Pick a Programming
Language
Programming Language

Same language as the app?

Who will own it?

Build a framework or use an existing one?

http://se.tips/seleniumframeworks
Step 3
Use Selenium
fundamentals
Selenium Fundamentals

Mimics human action

Uses a few common actions

Works with locators

Locators tell Selenium which HTML element to interact with


Common Actions
get();

findElement();

click(); //or submit();

sendKeys();

isDisplayed();
Locator Strategies
Class
Good locators are:
CSS selectors
unique
ID descriptive
unlikely to change
Link Text

Partial Link Text That rules a few of these out


Tag Name

XPath
Locator Strategies
Class
Good locators are:
CSS selectors
unique
ID descriptive
unlikely to change
Link Text

Partial Link Text That rules a few of these out


Tag Name

XPath
Locator Strategies
Class
Good locators are:
CSS selectors
unique
ID descriptive
unlikely to change
Link Text

Partial Link Text That rules a few of these out


Tag Name Start with IDs and Classes
XPath
Locator Strategies
Class
Good locators are:
CSS selectors
unique
ID descriptive
unlikely to change
Link Text

Partial Link Text That rules a few of these out


Tag Name Start with IDs and Classes
XPath Use CSS or XPath (with care)
Locator Strategies
Class

CSS selectors
CSS vs XPath
ID http://se.tips/seleniumbenchmarks
http://se.tips/cssxpathexamples
Link Text

Partial Link Text

Tag Name

XPath
Finding Quality Locators
Inspect the page

Verify your selection

e.g., FirePath or FireFinder

http://se.tips/verifyinglocators

e.g., JavaScript console with $$(); or $();

Learn through gaming

http://se.tips/locatorgame

Conversation
Step 4
Write your first test
Good Test Anatomy
Write for BDD or xUnit test framework

Test one thing (atomic)

Each test can be run independently (autonomous)

Anyone can understand what it is doing

Group similar tests together


A Login Example
1. Visit the login form

2. Find the login forms username field and input text

3. Find the login forms password field and input text

4. Find the submit button and click it

1. or, find the form and submit it


http://the-internet.herokuapp.com/login
Now to find an assertion
1. Login

2. Inspect the page

3. Find a locator

4. Verify it

5. Add it to the test


A much better assertion
Automated Visual Testing Primer

Check that an applications UI appears correctly

Can also be used to verify content

Hundreds of assertions for a few lines of code

Open-source libraries, baseline image comparison,


and inherent challenges

http://se.tips/visual-testing-getting-started
In pom.xml
Exception Handling
org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"css
selector","selector":".flash.error"}

Most common ones youll run into:


NoSuchElement and
StaleElementReferenceError

A list of all WebDriver exceptions:


http://se.tips/se-exceptions-java
Exception Handling contd

http://se.tips/se-exceptions-howto
Step 5
Write reusable and
maintainable test code
Page Objects
Need to update EVERY test :-(

Test 1 Test 2 Test 3 Test 4 Test 5

Application Under Test


Need to update JUST the page object :-D

Test 1 Test 2 Test 3 Test 4 Test 5

Page Object(s)

Application Under Test


Lets look at a page
object for login
And heres what the test
looks like when using it
Page object helpers:
http://se.tips/po-html-elements
http://se.tips/po-page-factory
Base Page Object
a.k.a.
Selenium Wrapper
Utility Class
etc.
Selenium
Commands

Page Page Page Page Page


Object 1 Object 2 Object 3 Object 4 Object 5
Selenium Global reuse
Commands More readable
Insulates you from
Selenium API changes
http://se.tips/se-upgrade
Base Page
Object

Page Page Page Page Page


Object 1 Object 2 Object 3 Object 4 Object 5
Lets take a look at a
Base Page Object
And here it is
implemented
How everything fits together

Tests use page objects


Test Test Test

Page Page Page objects inherit the


Object Object base page (utility) class

Base The base page object wraps


Page
Object your Selenium commands
Step 6
Make your tests resilient
Waiting
Thread.sleep();
Implicit wait
Explicit waits
Thread.sleep();
Implicit wait
Explicit waits
Thread.sleep();
Implicit wait
Explicit waits
http://se.tips/se-waiting
Explicit Waits

Specify an amount of time, and an action

Selenium will try repeatedly until either:

The action is completed, or

The amount of time specified has been reached


(and throw a timeout exception)
In the Base page object
In the DynamicLoading page object
Browser Timing
Considerations
Step 7
Prep for use
Test Harness
Simple organizational structure

Central setup and teardown

Configurable at run-time (with sensible defaults)

Reporting & Logging

Parallelization

Test Grouping
Folder structure
Central setup/teardown

More on JUnit Rules:


http://bit.ly/junit-rules
Simple config with defaults

Import config where its needed


(e.g., base test, etc.)
Reporting & Logging

Machine readable
e.g., JUnit XML

Human readable
e.g., screenshots, failure message, stack trace

Fantastic Test Report Tool


http://bit.ly/se-reporter (Allure Framework)
Parallelization
In code

Through your test runner

Through your Continuous Integration (CI) server

Recommended approach:
http://bit.ly/mvn-surefire

#protip Enforce random order execution of tests


(turnkey in mvn-surefire)
Test Grouping
Metadata (a.k.a. Categories)
More info:
Enables test packs bit.ly/junit-categories

Some category ideas

defect

shallow & deep

release or story number


Step 8
Add in cross-browser
execution
Locally
http://se.tips/se-chromedriver
http://se.tips/se-firefoxdriver
http://se.tips/se-iedriver
http://bit.ly/edge-driver
http://se.tips/se-safaridriver
Chrome
Grid
Grid
Browser
Node

Grid
Grid Hub Node Browser
Tests

Grid
Node Browser

All done with the Selenium Standalone Server


Just requires additional runtime flags
Grid
Hub

Node(s)
Grid

http://se.tips/grid-getting-started
http://se.tips/se-grid-extras
http://se.tips/se-grid-scaler
Sauce Labs

Sauce Labs Browser


Tests

http://se.tips/sauce-getting-started
http://se.tips/sauce-with-applitools
Step 9
Build an automated
feedback loop
Feedback loops
The goal: Find failures early and often

Done with continuous integration and notifications

Notifications
- remote: Email, chat, SMS
- in-person: audio/visual indicators
Code Promotion
yes yes

Code Unit/Integ. Deploy to Run


Committed (pass?) autom. test automated Deploy to
server tests next env.
(success?) (pass?)

yes

Notify team if no
Bonus points: stop the line
Simple CI configuration
1. Create a Job

2. Pull In Your Test Code

3. Set up Build Triggers

4. Configure Build steps

5. Configure Test Reports

6. Set up Notifications

7. Run Tests & View The Results

8. High-five your neighbor


Step 10
Find information on
your own

http://se.tips/selenium-resources
Steps to solve the puzzle
1. Define a Test Strategy 6. Make your tests resilient

2. Pick a programming language 7. Package your tests into a framework

3. Use Selenium Fundamentals 8. Add in cross-browser execution

4. Write Your First Test 9. Build an automated feedback loop

5. Write re-usable and maintainable 10. Find information on your own


test code
Write business valuable tests that are
reusable, maintainable and resilient
across all relevant browsers.

Then package them and scale them


for you & your team.
You may think your puzzle is unique. But really, everyone is

trying to solve the same puzzle. Yours is just configured

differently and its solvable

Dave Haeffner
FREE sample of my book

First 6 chapters available at


https://seleniumguidebook.com/#sample
Available in Java and Ruby
JavaScript, C#, and Python coming SOON!

FREE t-shirt give-away


1. Sign up for a free Applitools Eyes account
http://se.tips/applitools-free-account
2. Run your first visual test
3. Email webinars@applitools.com
4. Sport your new Visually Perfect Tee

Das könnte Ihnen auch gefallen