practical tips & tricks for selenium test automation

89
Selenium Tips & Tricks by Dave Haeffner

Upload: sauce-labs

Post on 07-Jan-2017

5.581 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Practical Tips & Tricks for Selenium Test Automation

Selenium Tips & Tricks

by Dave Haeffner

Page 2: Practical Tips & Tricks for Selenium Test Automation

New to Selenium?

• Selenium Bootcamp http://se.tips/se-bootcamp

• Selenium Guidebook http://se.tips/se-guidebook

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

Page 3: Practical Tips & Tricks for Selenium Test Automation

http://ElementalSelenium.com

Page 4: Practical Tips & Tricks for Selenium Test Automation

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

Page 5: Practical Tips & Tricks for Selenium Test Automation

Headless

Benefits: - Speed - Maintenance - Screenshotting

http://se.tips/all-headless-browsers

Page 6: Practical Tips & Tricks for Selenium Test Automation

Headless w/ Xvfbhttp://elementalselenium.com/tips/38-headless

Page 7: Practical Tips & Tricks for Selenium Test Automation

Why use it?

Ideal for running small test suites on a headless machine e.g., a Continuous Integration (CI) Server

Page 8: Practical Tips & Tricks for Selenium Test Automation

NOTE- display port collisions - use unique values - e.g., CI build number

Page 9: Practical Tips & Tricks for Selenium Test Automation

NOTE- display port collisions - use unique values - e.g., CI build number

Page 10: Practical Tips & Tricks for Selenium Test Automation

Headless w/ GhostDriverhttp://elementalselenium.com/tips/46-headless-ghostdriver

Page 11: Practical Tips & Tricks for Selenium Test Automation

http://phantomjs.org/

Page 12: Practical Tips & Tricks for Selenium Test Automation

Why use it? • Faster execution times • Also useful for a CI server • Not limited to just Linux

Page 13: Practical Tips & Tricks for Selenium Test Automation

Step 2 - Start PhantomJS w/ WebDriver flag

Step 1 - Download PhantomJShttp://phantomjs.org/download.html

Step 3 - Connect Your Test to PhantomJS using Selenium Remote

NOTEYou can also connect PhantomJS to a Selenium Grid http://se.tips/ghost-driver-grid

Option 1

Page 14: Practical Tips & Tricks for Selenium Test Automation
Page 15: Practical Tips & Tricks for Selenium Test Automation

Step 2 - Tell Selenium where it is

Step 1 - Download PhantomJShttp://phantomjs.org/download.html

Step 3 - Launch Selenium with PhantomJS locally

Option 2

Page 16: Practical Tips & Tricks for Selenium Test Automation
Page 17: Practical Tips & Tricks for Selenium Test Automation

Visual Testing

Page 18: Practical Tips & Tricks for Selenium Test Automation

A 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

Page 19: Practical Tips & Tricks for Selenium Test Automation

Visual Testing Challenges

• Managing complexity

• False positives

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

Page 20: Practical Tips & Tricks for Selenium Test Automation
Page 21: Practical Tips & Tricks for Selenium Test Automation

In pom.xml

Page 22: Practical Tips & Tricks for Selenium Test Automation
Page 23: Practical Tips & Tricks for Selenium Test Automation
Page 24: Practical Tips & Tricks for Selenium Test Automation
Page 25: Practical Tips & Tricks for Selenium Test Automation
Page 26: Practical Tips & Tricks for Selenium Test Automation
Page 27: Practical Tips & Tricks for Selenium Test Automation
Page 28: Practical Tips & Tricks for Selenium Test Automation
Page 29: Practical Tips & Tricks for Selenium Test Automation

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

Page 30: Practical Tips & Tricks for Selenium Test Automation

Selenium Grid Resources

• http://elementalselenium.com/tips/26-cloud

• http://elementalselenium.com/tips/52-grid

• http://elementalselenium.com/tips/70-grid-extras

Page 31: Practical Tips & Tricks for Selenium Test Automation

Fun with a Proxy Server

Page 32: Practical Tips & Tricks for Selenium Test Automation

Selenium Browser Proxy Server

Application Under Test

Page 33: Practical Tips & Tricks for Selenium Test Automation

http://bmp.lightbody.net/

Page 34: Practical Tips & Tricks for Selenium Test Automation

HTTP Status Codeshttp://elementalselenium.com/tips/17-retrieve-http-status-codes

Page 35: Practical Tips & Tricks for Selenium Test Automation

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

Page 36: Practical Tips & Tricks for Selenium Test Automation
Page 37: Practical Tips & Tricks for Selenium Test Automation
Page 38: Practical Tips & Tricks for Selenium Test Automation

Blacklistinghttp://elementalselenium.com/tips/66-blacklist

Page 39: Practical Tips & Tricks for Selenium Test Automation

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)

Page 40: Practical Tips & Tricks for Selenium Test Automation
Page 41: Practical Tips & Tricks for Selenium Test Automation
Page 42: Practical Tips & Tricks for Selenium Test Automation
Page 43: Practical Tips & Tricks for Selenium Test Automation

Load Testinghttp://elementalselenium.com/tips/68-load-testing-revisited

Page 44: Practical Tips & Tricks for Selenium Test Automation

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)

Page 45: Practical Tips & Tricks for Selenium Test Automation
Page 46: Practical Tips & Tricks for Selenium Test Automation

https://flood.io/har2jmx

Page 47: Practical Tips & Tricks for Selenium Test Automation

http://se.tips/selenium-to-jmeter

Page 48: Practical Tips & Tricks for Selenium Test Automation

Broken Image Checkinghttp://elementalselenium.com/tips/67-broken-images

Page 49: Practical Tips & Tricks for Selenium Test Automation

Option 1 - Proxy Server

Option 2 - HTTP Library

Option 3 - JavaScript

Page 50: Practical Tips & Tricks for Selenium Test Automation

Option 1: Proxy Server

Page 51: Practical Tips & Tricks for Selenium Test Automation

Option 1: Proxy Server

Page 52: Practical Tips & Tricks for Selenium Test Automation

Option 1: Proxy Server

Page 53: Practical Tips & Tricks for Selenium Test Automation

Option 2: HTTP Library

Page 54: Practical Tips & Tricks for Selenium Test Automation

Option 2: HTTP Library

Page 55: Practical Tips & Tricks for Selenium Test Automation

Option 2: HTTP Library

Page 56: Practical Tips & Tricks for Selenium Test Automation

Option 3: JavaScript

Page 57: Practical Tips & Tricks for Selenium Test Automation

Option 3: JavaScript

Page 58: Practical Tips & Tricks for Selenium Test Automation

Option 3: JavaScript

Page 59: Practical Tips & Tricks for Selenium Test Automation

Forgot Passwordhttp://elementalselenium.com/tips/43-forgot-password

Page 60: Practical Tips & Tricks for Selenium Test Automation

Configuration• Use Selenium to trigger a forgot password workflow

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

• Retrieve the e-mail and the password information via the email provider’s API

• Use the password information in the active Selenium session (if applicable)

Page 61: Practical Tips & Tricks for Selenium Test Automation

NOTE- not free - open issue

Page 62: Practical Tips & Tricks for Selenium Test Automation

A/B Testinghttp://elementalselenium.com/tips/12-opt-out-of-ab-tests

Page 63: Practical Tips & Tricks for Selenium Test Automation
Page 64: Practical Tips & Tricks for Selenium Test Automation
Page 65: Practical Tips & Tricks for Selenium Test Automation

• 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

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

Page 66: Practical Tips & Tricks for Selenium Test Automation

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

Page 67: Practical Tips & Tricks for Selenium Test Automation
Page 68: Practical Tips & Tricks for Selenium Test Automation
Page 69: Practical Tips & Tricks for Selenium Test Automation
Page 70: Practical Tips & Tricks for Selenium Test Automation

File Management

Page 71: Practical Tips & Tricks for Selenium Test Automation

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

Page 72: Practical Tips & Tricks for Selenium Test Automation
Page 73: Practical Tips & Tricks for Selenium Test Automation

Options

• GUI manipulation with something like AutoIt

• Input the file path into the form field (side-stepping the system dialog)

Page 74: Practical Tips & Tricks for Selenium Test Automation

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

Page 75: Practical Tips & Tricks for Selenium Test Automation

Downloading a Filehttp://elementalselenium.com/tips/2-download-a-file http://elementalselenium.com/tips/8-download-a-file-revisited http://elementalselenium.com/tips/15-download-secure-files

Page 76: Practical Tips & Tricks for Selenium Test Automation

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

Page 77: Practical Tips & Tricks for Selenium Test Automation

With Selenium

Page 78: Practical Tips & Tricks for Selenium Test Automation

With Selenium

Page 79: Practical Tips & Tricks for Selenium Test Automation

With an HTTP library

Page 80: Practical Tips & Tricks for Selenium Test Automation

With an HTTP library

Page 81: Practical Tips & Tricks for Selenium Test Automation

Additional Output

Page 82: Practical Tips & Tricks for Selenium Test Automation

Highlight Elementshttp://elementalselenium.com/tips/65-highlight-elements

Page 83: Practical Tips & Tricks for Selenium Test Automation
Page 84: Practical Tips & Tricks for Selenium Test Automation
Page 85: Practical Tips & Tricks for Selenium Test Automation

Growl Notificationshttp://elementalselenium.com/tips/53-growl

Page 86: Practical Tips & Tricks for Selenium Test Automation
Page 87: Practical Tips & Tricks for Selenium Test Automation
Page 88: Practical Tips & Tricks for Selenium Test Automation

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

Page 89: Practical Tips & Tricks for Selenium Test Automation

Available in Java and Ruby(C#, Python, & JS coming soon!)

https://seleniumguidebook.com