0% found this document useful (0 votes)
135 views90 pages

SeTipsTricks Java Dave Haeffer

This document provides tips and tricks for using Selenium, including running tests headlessly using Xvfb or PhantomJS, visual testing, using a proxy server for tasks like load testing or checking HTTP status codes, handling A/B testing and file uploads/downloads, and additional Selenium resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views90 pages

SeTipsTricks Java Dave Haeffer

This document provides tips and tricks for using Selenium, including running tests headlessly using Xvfb or PhantomJS, visual testing, using a proxy server for tasks like load testing or checking HTTP status codes, handling A/B testing and file uploads/downloads, and additional Selenium resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Selenium

Tips & Tricks


by Dave Haeffner

http://ElementalSelenium.com

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

Headless
http://se.tips/all-headless-browsers
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
- e.g., xvfb-run -a

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

Option 1
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://se.tips/ghost-driver-grid

Option 2

Step 1 - Download PhantomJS


http://phantomjs.org/download.html
Step 2 - Tell Selenium where it is

Step 3 - Launch Selenium with PhantomJS locally

Visual Testing

A 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

Visual Testing Challenges

Managing complexity

False positives

e.g., shifting content, dynamic content, typos

In pom.xml

Visual Testing Write-ups


http://se.tips/se-visual-1

Getting Started

http://se.tips/se-visual-2

False Positives part 1

http://se.tips/se-visual-3

False Positives part 2

http://se.tips/se-visual-4

Add Visual Testing To Your Existing Tests

http://se.tips/se-visual-5

Add Visual Testing To Your BDD Tests

Fun with a Proxy Server

Selenium

Browser

Proxy
Server

Application
Under
Test

http://bmp.lightbody.net/

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 youre 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 dont 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)

https://flood.io/har2jmx

http://se.tips/selenium-to-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

Option 1: Proxy Server

Option 1: Proxy Server

Option 2: HTTP Library

Option 2: HTTP Library

Option 2: HTTP Library

Option 3: JavaScript

Option 3: JavaScript

Option 3: JavaScript

Forgot Password
http://elementalselenium.com/tips/43-forgot-password

Configuration

Use Selenium to trigger a forgot password workflow


(to email a real address) and keep the browser
session active

Retrieve the e-mail and the password information


via the email providers API

Use the password information in the active


Selenium session (if applicable)

http://se.tips/watirmelon-email-testing

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 youre in because


there is different header text for each

When youre in the control: A/B Test Control

For the variation: A/B Test Variation 1

When youre 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


is far less likely to change without your knowledge

File Management

Uploading a File
http://elementalselenium.com/tips/1-upload-a-file

Options

GUI manipulation with something like AutoIt

Input the file path into the form field (side-stepping


the system dialog)

http://se.tips/grid-file-upload

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
1.Configure Selenium to download to local disk, and
delete the file when done
2.Use an HTTP library, perform a HEAD request, and
check the headers for the correct content type &
length.
Why #2?
An order of magnitude faster
No need to download the file

With Selenium

With Selenium

With an HTTP library

With an HTTP library

Additional Output

Highlight Elements
http://elementalselenium.com/tips/65-highlight-elements

Growl Notifications
http://elementalselenium.com/tips/53-growl

More Selenium Resources

Selenium Bootcamp

http://se.tips/se-bootcamp

Weekly Selenium Tips http://se.tips/se-weekly-tips

Weekly Se Tip Code

http://se.tips/oss-tip-code

Selenium Guidebook

http://se.tips/se-guidebook

http://seleniumconf.co.uk

You might also like