Selenium
Tips & Tricks
by Dave Haeffner
@TourDeDave
New to Selenium?
• Selenium Bootcamp http://bit.ly/se-bootcamp
• Selenium Guidebook http://bit.ly/se-guidebook
• Weekly Selenium Tips http://bit.ly/se-weekly-tips
http://ElementalSelenium.com
etc.
http://the-internet.herokuapp.com/
Headless
Benefits:
- Speed
- Maintenance
- Screenshotting
Headless w/ Xvfb
http://elementalselenium.com/tips/38-headless
Why use it?
Ideal for running small test suites on a headless machine
e.g., a Continuous Integration (CI) Server
NOTE
- display port collisions
- use unique values
- e.g., CI build number
Headless w/ GhostDriver
http://elementalselenium.com/tips/46-headless-ghostdriver
http://phantomjs.org/
Why use it?
• Faster execution times
• Also useful for a CI server
• Not limited to just Linux
Step 1 - Download PhantomJS
http://phantomjs.org/download.html
Step 2 - Start PhantomJS w/ WebDriver flag
Step 3 - Connect Your Test to PhantomJS
using Selenium Remote
NOTE
You can also connect PhantomJS to a Selenium Grid
http://bit.ly/ghost-driver-grid
Visual Testing
A Visual Testing Primer
• check that an application’s UI appears correctly to its users
• goal: find visual bugs before the user does
• e.g., font, layout, rendering issues
• can also be used to verify content
• e.g., charts, dashboards, etc.
• hundreds of assertions for a few lines of code
• at least 16 open source solutions available to choose from
Visual Testing Challenges
• Managing complexity
• False positives
• e.g., shifting content, dynamic content, typos
Visual Testing Write-ups
http://bit.ly/se-visual-1 Getting Started
http://bit.ly/se-visual-2 False Positives part 1
http://bit.ly/se-visual-3 False Positives part 2
http://bit.ly/se-visual-4 Add Visual Testing To Your Existing Tests
http://bit.ly/se-visual-5 Add Visual Testing To Your BDD Tests
Selenium Grid Resources
• http://elementalselenium.com/tips/26-cloud
• http://elementalselenium.com/tips/52-grid
• http://elementalselenium.com/tips/70-grid-extras
Fun with a Proxy Server
Application
Proxy
Selenium Browser Under
Server
Test
HTTP Status Codes
http://elementalselenium.com/tips/17-retrieve-http-status-codes
Configuration
• Use a proxy server to capture the traffic from your
Selenium test(s)
• Find the status code for the action you’re interested
in (e.g., visiting a URL)
• Assert that the status code is what you expect
Blacklisting
http://elementalselenium.com/tips/66-blacklist
Configuration
• Use a proxy server to manipulate the traffic from
your Selenium test(s)
• Identify third-party resources that are slow to load
(which could negatively impact your tests)
• Blacklist them (e.g., make it so they don’t load)
Load Testing
http://elementalselenium.com/tips/68-load-testing-revisited
Configuration
• Use a proxy server to capture the traffic from your
Selenium test(s)
• Convert the HTTP Archive into a JMeter JMX file
• Run the new JMX file with JMeter to enact load on
your application (modify as needed)
Adapted from ruby-jmeter example from flood.io
https://github.com/flood-io/ruby-jmeter
Broken Image Checking
http://elementalselenium.com/tips/67-broken-images
Option 1 - Proxy Server
Option 2 - HTTP Library
Option 3 - JavaScript
Option 1: Proxy Server
Proxy Server Cont’d
Proxy Server Cont’d
Proxy Server Cont’d
Option 2: HTTP Library
HTTP Library Cont’d
HTTP Library Cont’d
Option 3: JavaScript
JavaScript Cont’d
JavaScript Cont’d
JavaScript Cont’d
Forgot Password
http://elementalselenium.com/tips/43-forgot-password
Configuration
• Use Selenium to trigger a forgot password workflow
(to a Gmail account) and keep the browser session
active
• Retrieve the e-mail and the password information
via the Gmail API
• Use the password information in the active
Selenium session (if applicable)
A/B Testing
http://elementalselenium.com/tips/12-opt-out-of-ab-tests
http://the-internet.herokuapp.com/abtest
• 3 page states available
• You can identify which state you’re in because
there is different header text for each
• When you’re in the control: A/B Test Control
• For the variation: A/B Test Variation 1
• When you’re not in a test: No A/B Test
Configuration
• You can easily opt-out of A/B tests by
• forging a cookie
• appending a query to the URL
• This way you get a known state of the page which
isn’t likely to change without your knowledge
File Management
Uploading a File
Workaround
• Rather than use something like AutoIt (which is a
bad idea)
• You can send the file path you want to upload into
the form input field (side-stepping the system
dialog entirely)
http://bit.ly/se-file-detector
Downloading a File
http://elementalselenium.com/tips/2-download-a-file
http://elementalselenium.com/tips/8-download-a-file-revisited
http://elementalselenium.com/tips/15-download-secure-files
Two approaches
• Configure Selenium to download to local disk, and
delete the file when done
• Use an HTTP library, perform a HEAD request, and
check the headers for the correct content type &
length.
Why?
• An order of magnitude faster using Selenium
• No need to download the file
With Selenium
With Selenium
With an HTTP library
With an HTTP library (for secure files)
Additional Output
Highlight Elements
http://elementalselenium.com/tips/65-highlight-elements
Growl Notifications
http://elementalselenium.com/tips/53-growl
New to Selenium?
• Selenium Bootcamp http://bit.ly/se-bootcamp
• Selenium Guidebook http://bit.ly/se-guidebook
• Weekly Selenium Tips http://bit.ly/se-weekly-tips
http://ElementalSelenium.com http://bit.ly/oss-es-tips
Get in touch
@TourDeDave
[email protected]
DaveHaeffner.com