90
How To Use Selenium, Successfully by Dave Haeffner, @TourDeDave

Mastering Test Automation: How to Use Selenium Successfully

Embed Size (px)

Citation preview

Page 1: Mastering Test Automation: How to Use Selenium Successfully

How To Use Selenium, Successfully

by Dave Haeffner, @TourDeDave

Page 2: Mastering Test Automation: How to Use Selenium Successfully

http://www.wpclipart.com/geography/features/chasm.png.html

Page 3: Mastering Test Automation: How to Use Selenium Successfully

http://en.wikipedia.org/wiki/Optimal_solutions_for_Rubik's_Cube

Page 4: Mastering Test Automation: How to Use Selenium Successfully

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

Then package and scale them for you & your team.

Page 5: Mastering Test Automation: How to Use Selenium Successfully

Selenium Overview

• What it is — the Reader’s Digest version

• What it is and is not good at

• IDE vs. Local vs. Remote

• Slow, brittle, and hard to maintain?

Page 6: Mastering Test Automation: How to Use Selenium Successfully

Step 1 Define a Test Strategy

Page 7: Mastering Test Automation: How to Use Selenium Successfully

Test Strategy1. 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

Page 8: Mastering Test Automation: How to Use Selenium Successfully

Step 2 Pick a Programming

Language

Page 9: Mastering Test Automation: How to Use Selenium Successfully

Programming Language

• Same language as the app?

• Who will own it?

• Build a framework or use an existing one?

• http://se.tips/seleniumframeworks

Page 10: Mastering Test Automation: How to Use Selenium Successfully

Step 3 Use Selenium fundamentals

Page 11: Mastering Test Automation: How to Use Selenium Successfully

Selenium Fundamentals

• Mimics human action

• Uses a few common actions

• Works with “locators”

Locators tell Selenium which HTML element to interact with

Page 12: Mastering Test Automation: How to Use Selenium Successfully

Common Actions

• get();

• findElement();

• click(); //or submit();

• sendKeys();

• isDisplayed();

Page 13: Mastering Test Automation: How to Use Selenium Successfully

Locator Strategies• Class

• CSS selectors

• ID

• Link Text

• Partial Link Text

• Tag Name

• XPath

Good locators are: • unique • descriptive • unlikely to change

That rules a few of these out

Page 14: Mastering Test Automation: How to Use Selenium Successfully

Locator Strategies• Class

• CSS selectors

• ID

• Link Text

• Partial Link Text

• Tag Name

• XPath

Good locators are: • unique • descriptive • unlikely to change

That rules a few of these out

Page 15: Mastering Test Automation: How to Use Selenium Successfully

Locator Strategies• Class

• CSS selectors

• ID

• Link Text

• Partial Link Text

• Tag Name

• XPath

Good locators are: • unique • descriptive • unlikely to change

That rules a few of these out

Start with IDs and Classes

Page 16: Mastering Test Automation: How to Use Selenium Successfully

Locator Strategies• Class

• CSS selectors

• ID

• Link Text

• Partial Link Text

• Tag Name

• XPath

Good locators are: • unique • descriptive • unlikely to change

That rules a few of these out

Start with IDs and Classes

Use CSS or XPath (with care)

Page 17: Mastering Test Automation: How to Use Selenium Successfully

Locator Strategies• Class

• CSS selectors

• ID

• Link Text

• Partial Link Text

• Tag Name

• XPath

CSS vs XPath http://se.tips/seleniumbenchmarkshttp://se.tips/cssxpathexamples

Page 18: Mastering Test Automation: How to Use Selenium Successfully

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

Page 19: Mastering Test Automation: How to Use Selenium Successfully
Page 20: Mastering Test Automation: How to Use Selenium Successfully

Step 4 Write your first test

Page 21: Mastering Test Automation: How to Use Selenium Successfully

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

Page 22: Mastering Test Automation: How to Use Selenium Successfully

A Login Example

1. Visit the login form

2. Find the login form’s username field and input text

3. Find the login form’s password field and input text

4. Find the submit button and click it

1. or, find the form and submit it

Page 23: Mastering Test Automation: How to Use Selenium Successfully

http://the-internet.herokuapp.com/login

Page 24: Mastering Test Automation: How to Use Selenium Successfully
Page 25: Mastering Test Automation: How to Use Selenium Successfully
Page 26: Mastering Test Automation: How to Use Selenium Successfully

Now to find an assertion

1. Login

2. Inspect the page

3. Find a locator

4. Verify it

5. Add it to the test

Page 27: Mastering Test Automation: How to Use Selenium Successfully
Page 28: Mastering Test Automation: How to Use Selenium Successfully
Page 29: Mastering Test Automation: How to Use Selenium Successfully

A much better assertion

Page 30: Mastering Test Automation: How to Use Selenium Successfully

Automated Visual Testing Primer

• Check that an application’s 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

Page 31: Mastering Test Automation: How to Use Selenium Successfully

In pom.xml

Page 32: Mastering Test Automation: How to Use Selenium Successfully
Page 33: Mastering Test Automation: How to Use Selenium Successfully
Page 34: Mastering Test Automation: How to Use Selenium Successfully
Page 35: Mastering Test Automation: How to Use Selenium Successfully
Page 36: Mastering Test Automation: How to Use Selenium Successfully
Page 37: Mastering Test Automation: How to Use Selenium Successfully
Page 38: Mastering Test Automation: How to Use Selenium Successfully

Exception Handling• org.openqa.selenium.NoSuchElementException:

Unable to locate element: {"method":"css selector","selector":".flash.error"}

• Most common ones you’ll run into: NoSuchElement and StaleElementReferenceError

• A list of all WebDriver exceptions: http://se.tips/se-exceptions-java

Page 39: Mastering Test Automation: How to Use Selenium Successfully

Exception Handling cont’d

http://se.tips/se-exceptions-howto

Page 40: Mastering Test Automation: How to Use Selenium Successfully

Step 5 Write reusable and

maintainable test code

Page 41: Mastering Test Automation: How to Use Selenium Successfully

Page Objects

Page 42: Mastering Test Automation: How to Use Selenium Successfully

Application Under Test

Test 1 Test 2 Test 3 Test 4 Test 5Test 1 Test 2 Test 3 Test 4 Test 5

Need to update EVERY test :-(

Page 43: Mastering Test Automation: How to Use Selenium Successfully

Page Object(s)

Application Under Test

Test 1 Test 2 Test 3 Test 4 Test 5

Need to update JUST the page object :-D

Page 44: Mastering Test Automation: How to Use Selenium Successfully

Let’s look at a page object for login

Page 45: Mastering Test Automation: How to Use Selenium Successfully
Page 46: Mastering Test Automation: How to Use Selenium Successfully

And here’s what the test looks like when using it

Page 47: Mastering Test Automation: How to Use Selenium Successfully

Page object helpers: http://se.tips/po-html-elementshttp://se.tips/po-page-factory

Page 48: Mastering Test Automation: How to Use Selenium Successfully

Base Page Objecta.k.a.

Selenium Wrapper Utility Class

etc.

Page 49: Mastering Test Automation: How to Use Selenium Successfully

Selenium Commands

Page Object 1

Page Object 2

Page Object 3

Page Object 4

Page Object 5

Page 50: Mastering Test Automation: How to Use Selenium Successfully

Base Page Object

Page Object 1

Page Object 2

Page Object 3

Page Object 4

Page Object 5

Selenium Commands

• Global reuse • More readable • Insulates you from

Selenium API changes http://se.tips/se-upgrade

Page 51: Mastering Test Automation: How to Use Selenium Successfully

Let’s take a look at a Base Page Object

Page 52: Mastering Test Automation: How to Use Selenium Successfully
Page 53: Mastering Test Automation: How to Use Selenium Successfully

And here it is implemented

Page 54: Mastering Test Automation: How to Use Selenium Successfully
Page 55: Mastering Test Automation: How to Use Selenium Successfully

How everything fits together

Test TestTest

Page Object

Page Object

Base Page

Object

Tests use page objects

Page objects inherit the base page (utility) class

The base page object wraps your Selenium commands

Page 56: Mastering Test Automation: How to Use Selenium Successfully

Step 6 Make your tests resilient

Page 57: Mastering Test Automation: How to Use Selenium Successfully

Waiting

Page 58: Mastering Test Automation: How to Use Selenium Successfully

Thread.sleep(); Implicit wait Explicit waits

Page 59: Mastering Test Automation: How to Use Selenium Successfully

Thread.sleep(); Implicit wait Explicit waits

Page 60: Mastering Test Automation: How to Use Selenium Successfully

Thread.sleep(); Implicit wait Explicit waits

http://se.tips/se-waiting

Page 61: Mastering Test Automation: How to Use Selenium Successfully

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)

Page 62: Mastering Test Automation: How to Use Selenium Successfully
Page 63: Mastering Test Automation: How to Use Selenium Successfully

In the Base page object

Page 64: Mastering Test Automation: How to Use Selenium Successfully

In the DynamicLoading page object

Page 65: Mastering Test Automation: How to Use Selenium Successfully

Browser Timing Considerations

Page 66: Mastering Test Automation: How to Use Selenium Successfully

Step 7 Prep for use

Page 67: Mastering Test Automation: How to Use Selenium Successfully

Test Harness• Simple organizational structure

• Central setup and teardown

• Configurable at run-time (with sensible defaults)

• Reporting & Logging

• Parallelization

• Test Grouping

Page 68: Mastering Test Automation: How to Use Selenium Successfully

Folder structure

Page 69: Mastering Test Automation: How to Use Selenium Successfully

Central setup/teardown

More on JUnit Rules: http://bit.ly/junit-rules

Page 70: Mastering Test Automation: How to Use Selenium Successfully

Simple config with defaults

Import config where it’s needed (e.g., base test, etc.)

Page 71: Mastering Test Automation: How to Use Selenium Successfully

Reporting & Logging

• Machine readablee.g., JUnit XML

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

Fantastic Test Report Tool http://bit.ly/se-reporter (Allure Framework)

Page 72: Mastering Test Automation: How to Use Selenium Successfully

Parallelization• In code

• Through your test runner

• Through your Continuous Integration (CI) server

#protip Enforce random order execution of tests (turnkey in mvn-surefire)

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

Page 73: Mastering Test Automation: How to Use Selenium Successfully

Test Grouping• Metadata (a.k.a. Categories)

• Enables “test packs”

• Some category ideas

• defect

• shallow & deep

• release or story number

More info: bit.ly/junit-categories

Page 74: Mastering Test Automation: How to Use Selenium Successfully

Step 8 Add in cross-browser

execution

Page 75: Mastering Test Automation: How to Use Selenium Successfully

Locallyhttp://se.tips/se-chromedriverhttp://se.tips/se-firefoxdriverhttp://se.tips/se-iedriverhttp://bit.ly/edge-driverhttp://se.tips/se-safaridriver

Page 76: Mastering Test Automation: How to Use Selenium Successfully

Chrome

Page 77: Mastering Test Automation: How to Use Selenium Successfully

Grid

Grid Hub

Browser

Tests

All done with the Selenium Standalone ServerJust requires additional runtime flags

Grid Node

Grid Node

Grid Node

Browser

Browser

Page 78: Mastering Test Automation: How to Use Selenium Successfully

GridHub

Node(s)

Page 79: Mastering Test Automation: How to Use Selenium Successfully

Grid

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

Page 80: Mastering Test Automation: How to Use Selenium Successfully

Sauce Labs

Sauce Labs BrowserTests

http://se.tips/sauce-getting-startedhttp://se.tips/sauce-with-applitools

Page 81: Mastering Test Automation: How to Use Selenium Successfully

Step 9 Build an automated

feedback loop

Page 82: Mastering Test Automation: How to Use Selenium Successfully

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

Page 83: Mastering Test Automation: How to Use Selenium Successfully

Code Committed

Unit/Integ. (pass?)

Deploy to autom. test

server (success?)

Run automated

tests (pass?)

Deploy to next env.

yes

yes

yes

Notify team if no

Code Promotion

Bonus points: stop the line

Page 84: Mastering Test Automation: How to Use Selenium Successfully

Simple CI configuration1. 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

Page 85: Mastering Test Automation: How to Use Selenium Successfully
Page 86: Mastering Test Automation: How to Use Selenium Successfully

Step 10 Find information on

your own

http://se.tips/selenium-resources

Page 87: Mastering Test Automation: How to Use Selenium Successfully

Steps to solve the puzzle1. Define a Test Strategy

2. Pick a programming language

3. Use Selenium Fundamentals

4. Write Your First Test

5. Write re-usable and maintainable test code

6. Make your tests resilient

7. Package your tests into a framework

8. Add in cross-browser execution

9. Build an automated feedback loop

10. Find information on your own

Page 88: Mastering Test Automation: How to Use Selenium Successfully

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

Then package them and scale them for you & your team.

Page 89: Mastering Test Automation: How to Use Selenium Successfully

–Dave Haeffner

“You may think your puzzle is unique. But really, everyone is

trying to solve the same puzzle. Yours is just configured

differently — and it’s solvable”

Page 90: Mastering Test Automation: How to Use Selenium Successfully

1. Sign up for a free Applitools Eyes accounthttp://se.tips/applitools-free-account

2. Run your first visual test3. Email [email protected]. Sport your new “Visually Perfect” Tee

FREE t-shirt give-away

FREE sample of my book

• First 6 chapters available athttps://seleniumguidebook.com/#sample

• Available in Java and Ruby• JavaScript, C#, and Python coming SOON!