how to use selenium successfully

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

Upload: dave-haeffner

Post on 05-Dec-2014

2.572 views

Category:

Technology


6 download

DESCRIPTION

Want to learn how to use Selenium from the ground up? This presentation will show you how to start from nothing and build out a well factored, maintainable, resilient, and parallelized set of tests that will run locally, on a Continuous Integration server, and in the cloud. These tests will not only work well, but exercise relevant functionality that matters to the business.

TRANSCRIPT

Page 1: How To Use Selenium Successfully

How To Use Selenium, Successfully

by Dave Haeffner, @TourDeDave

Page 2: How To Use Selenium Successfully

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

Page 3: How To Use Selenium Successfully

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

Page 4: How To Use Selenium Successfully

1. Preparation

• Define a Test Strategy

• Pick a Programming Language

• Choose a Text Editor

Page 5: How To Use Selenium Successfully

On Defining a Test Strategy1. How does your business make money?

2. How do your users user your application?

3. What browsers are your users using?

4. What things have broken before?

Outcome: What to test and which browsers to care about

Page 6: How To Use Selenium Successfully

On Picking a Programming Language

• Same language as the app?

• Who will own it?

• Build a framework or use an existing one?

• http://bit.ly/seleniumframeworks

• What about a scripting language?

Page 7: How To Use Selenium Successfully

On Choosing a Text Editor

• Emacs

• IntelliJ

• Vim

• Sublime Text

Page 8: How To Use Selenium Successfully

2. Write Tests Well• Atomic

• Autonomous

• Descriptive

• Succinct

• For a test runner

• Stored in Version Control

Page 9: How To Use Selenium Successfully

3. Selenium Fundamentals

• Mimics human action

• Uses a few common actions

• Works with “locators”

Page 10: 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 11: 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 12: 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 13: 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 sanely

Page 14: How To Use Selenium Successfully

Locator Strategies• Class!

• CSS selectors!

• ID!

• Link Text

• Partial Link Text

• Tag Name

• XPath

CSS vs XPath http://bit.ly/seleniumbenchmarks http://bit.ly/cssxpathexamples

Page 15: How To Use Selenium Successfully

Finding Quality Locators• Inspect the page

• Verify your selection

• e.g., FirePath or FireFinder

• http://bit.ly/verifyinglocators

• Learn through gaming

• http://bit.ly/locatorgame

Page 16: How To Use Selenium Successfully

Common Selenium actions

• click

• clear

• send_keys

• text

• displayed?

Look up your language’s bindings http://bit.ly/seleniumwiki

Page 17: How To Use Selenium Successfully

A Login Example

1. Visit the main page of a site

2. Find the login button and click it

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

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

5. Find the submit button and click it

Page 18: How To Use Selenium Successfully

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

Page 19: How To Use Selenium Successfully
Page 20: How To Use Selenium Successfully

4. Write Re-usable/Maintainable Test Code

• Page Object Pattern

• Facade layer (a.k.a. Base Page Object)

Page 21: How To Use Selenium Successfully
Page 22: How To Use Selenium Successfully
Page 23: How To Use Selenium Successfully
Page 24: How To Use Selenium Successfully
Page 25: How To Use Selenium Successfully

5. Make Your Tests Resilient

Page 26: How To Use Selenium Successfully

Explicit waits

• Specify an amount of time, and an action

• Selenium will try until either:

• The action can be accomplished, or

• The amount of time has been reached (and throw a timeout exception)

Page 27: How To Use Selenium Successfully

Added to base page

Put to use in a page object

Page 28: How To Use Selenium Successfully

6. Prepping for use (for humans and robots)

• Central setup/teardown

• Folder structure

• Config file(s)

• Tagging

• Reporting (screenshots, JUnit XML, etc.)

• Command-line execution wrapper

Page 29: How To Use Selenium Successfully

Central setup/teardown

Page 30: How To Use Selenium Successfully

A simple file/folder structureconfigs/

pages/

spec/

vendor/

Gemfile

Rakefile

Page 31: How To Use Selenium Successfully

Tagging• Test packs

• Some tagging ideas

• wip

• critical

• component name

• slow

• story number

What this looks like in RSpec http://bit.ly/rspectagging

Page 32: How To Use Selenium Successfully

Reporting

• For robots: JUnit XML output

• http://bit.ly/rspec-junit

• For humans: screenshots, video, logs, etc.

Page 33: How To Use Selenium Successfully

Human Readable

Page 34: How To Use Selenium Successfully

Robot Ready

Page 35: How To Use Selenium Successfully

7. Scale It

• Scaled execution (e.g., 3rd party cloud provider)

• Parallelization

• Continuous Integration

Page 36: How To Use Selenium Successfully

Cloud execution

http://saucelabs.com

Page 37: How To Use Selenium Successfully

cloud_config.rb

Page 38: How To Use Selenium Successfully

spec_helper.rb

Page 39: How To Use Selenium Successfully

Parallelization (can be done)

• In code

• with threads: http://bit.ly/seleniumparallel1

• with processes: http://bit.ly/seleniumparallel2

• Through a test runner (e.g., TestNG in Java)

• Through your CI server

#protip enforce random order execution of tests

Page 40: How To Use Selenium Successfully

Continuous Integration

• Feedback loops

• Code promotion

Page 41: How To Use Selenium Successfully

Feedback loops• The goal: Find failures early and often

• Notification types

• Email, chat, SMS

• In-person (audio & visual)

Page 42: How To Use Selenium Successfully

Code Committed

Integration (pass?)

Deploy to autom. test

server (success?)

Run automated

tests (pass?)

Deploy to manual test

server (manual)

(success?)

yes

yes

yes

Notify team if no

Code Promotion

Bonus points: break the build

Page 43: How To Use Selenium Successfully

Simple Jenkins 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

Don’t forget about a systems check!

Page 44: How To Use Selenium Successfully

Write business valuable tests that are reusable, maintainable and resilient. !

Then package them for humans and robots — and scale them for you & your team.

Page 45: How To Use Selenium Successfully

–Dave Haeffner, @TourDeDave

“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 46: How To Use Selenium Successfully

http://SeleniumGuidebook.com

Page 47: How To Use Selenium Successfully

d

http://ElementalSelenium.com