+ All Categories
Home > Internet > Selenium Tips & Tricks - StarWest 2015

Selenium Tips & Tricks - StarWest 2015

Date post: 14-Apr-2017
Category:
Upload: andrew-krug
View: 623 times
Download: 3 times
Share this document with a friend
82
Selenium Tips & Tricks by Andrew Krug @andrewmkrug [email protected]
Transcript
Page 1: Selenium Tips & Tricks - StarWest 2015

Selenium Tips & Tricks

by Andrew Krug @andrewmkrug

[email protected]

Page 2: Selenium Tips & Tricks - StarWest 2015

Who is this person?• Worked in Test Automation entire career

• Currently Lead Test Automation Engineer for Revcontent

• Market Leader for Native Advertising

• Worked at Ceridian HCM/FIS/Ceridian HCM

• Windows, Mac, iOS, Android, Window Phone and Blackberry

Page 3: Selenium Tips & Tricks - StarWest 2015

Revcontent

• Market Leader for Native Advertising

• 2-3 times greater RPM than Competitors

• Related Stories Around the Web

Page 4: Selenium Tips & Tricks - StarWest 2015

Revcontent

We’re Hiring!

Page 5: Selenium Tips & Tricks - StarWest 2015

!Plagiarized• These Slides are adapted with permission from Dave

Haeffner’s Selenium Tips & Tricks

• Runs Elemental Selenium bit.ly/se-weekly-tips

• Weekly Tip Newsletter

• Selenium Guidebook bit.ly/se-guidebook

• Title speaks for itself

• Bootcamp bit.ly/se-bootcamp

Page 6: Selenium Tips & Tricks - StarWest 2015

Selenium

• Sent from the Future

• Is a toolbox

• ! Tool

Page 7: Selenium Tips & Tricks - StarWest 2015

Expected Outcome

foreach attendee in here:

AssertThat( learnedNewSelenium = True)

To do that just raise your hand every time you learn

Page 8: Selenium Tips & Tricks - StarWest 2015

! Talking

• Appium

• Mobile Device Testing

• FBSimulatorControl

• >1 iOS Simulators on 1 Mac

• SWD Recorder

• Generates Page Objects

• Docker-Selenium

• Hub & Node Containers

• SeleniumGridScaler

• On Demand Nodes for Test

• Selenium Grid Extras

• Grid Hub on Steroids

Page 9: Selenium Tips & Tricks - StarWest 2015

See what I did there?

Page 10: Selenium Tips & Tricks - StarWest 2015

Topics• Headless

• Visual Testing

• Proxy Servers

• Load Testing!

• A/B Testing

• Files Handling

• Notifications

• Mobile Browser Emulation

• !Mobile Devices

• Music Playing

• Bonuses are sprinkled throughout

Page 11: Selenium Tips & Tricks - StarWest 2015

–Johnny Appleseed

“Sprinkles are for winners.”

Page 12: Selenium Tips & Tricks - StarWest 2015

You are all winners!

Page 13: Selenium Tips & Tricks - StarWest 2015

Headless

Page 14: Selenium Tips & Tricks - StarWest 2015

Not ThisHeadless Horseman

https://www.flickr.com/photos/fanabouttown/10719478555

Page 15: Selenium Tips & Tricks - StarWest 2015

Or ThisHorseless Headsman

http://www.framestore.com/work/snickers-horseless-headsman

Page 16: Selenium Tips & Tricks - StarWest 2015

What is it?

• No GUI

• Generally

Page 17: Selenium Tips & Tricks - StarWest 2015

Benefits

• Speed

• Grid Maintenance

• Screenshots

Page 18: Selenium Tips & Tricks - StarWest 2015

Headless with Xvfbfor *nix

Page 19: Selenium Tips & Tricks - StarWest 2015

Xvfb?

• X virtual frame buffer

• In-memory display server

• run graphical applications with out display

Page 20: Selenium Tips & Tricks - StarWest 2015

Xvfb Option 1• Start Xvfb on a port

• Tell terminal to use the port

• Run Test

Xvfb :99

export DISPLAY=:99

ruby example.rb

Page 21: Selenium Tips & Tricks - StarWest 2015

Option 2

• Use Xvfb-run to launch tests

xvfb-run ruby example.rb

Page 22: Selenium Tips & Tricks - StarWest 2015

Option 3

• Use the headless gem

Page 23: Selenium Tips & Tricks - StarWest 2015

Headless with PhantomJS

• phantomjs.org

• not limited by OS

Page 24: Selenium Tips & Tricks - StarWest 2015

PhantomJS Options1.Add to path

WebDriver = new PhantomJSDriver();

2.Start with WebDriver Flag

phantomjs - -webdriver=8910

3.Pass in Executable Path

Similar to Chromedriver or IEDriverServer

Page 25: Selenium Tips & Tricks - StarWest 2015

Visual Testing

Page 26: Selenium Tips & Tricks - StarWest 2015

What?!?!?

Page 27: Selenium Tips & Tricks - StarWest 2015

How Awesome!• Check UI appears correctly

• Find layout bugs

• Font, layout, rendering differences

• Verify Content

• Charts

• Are Multiple Libraries

• Time for some Sprinkles!!!

• Few Lines of Code!!!

Page 28: Selenium Tips & Tricks - StarWest 2015

Challenges

• Complex

• False Positives

• Shifted content

• Dynamic content

Page 29: Selenium Tips & Tricks - StarWest 2015

Training Skynet

Page 30: Selenium Tips & Tricks - StarWest 2015

Why Applitools• https://applitools.com

• Compare Layout of Page

• Disregard areas

• Finds related problems

• Deals with the Challenges

• I can do more stuff!!!

Page 31: Selenium Tips & Tricks - StarWest 2015

Before Testdriver.get(“the-internet.heroku.com/login”);

driver.findElement(By.id(“username”))

.sendKeys(“tomsmith”);

driver.findElement(By.id(“password”))

.sendKeys(“SuperSecretPassword”);

driver.findElement(By.id(“login”)).submit();

Page 32: Selenium Tips & Tricks - StarWest 2015

With Eyeballsdriver.get(“the-internet.heroku.com/login”);

Eyes eyes = new Eyes();

eyes.setApiKey(apikey);

eyes.CheckWindow(“Login”);

driver.findElement(By.id(“username”)).sendKeys(“tomsmith”);

driver.findElement(By.id(“password”)).sendKeys(“SuperSecretPassword”);

driver.findElement(By.id(“login”)).submit();

eyes.checkWindow(“Logged In”);

Page 33: Selenium Tips & Tricks - StarWest 2015

With Eyeballsdriver.get(“the-internet.heroku.com/login”);

Eyes eyes = new Eyes();

eyes.setApiKey(apikey);

eyes.CheckWindow(“Login”);

driver.findElement(By.id(“username”)).sendKeys(“tomsmith”);

driver.findElement(By.id(“password”)).sendKeys(“SuperSecretPassword”);

driver.findElement(By.id(“login”)).submit();

eyes.checkWindow(“Logged In”);

Page 34: Selenium Tips & Tricks - StarWest 2015

With Eyeballs in Test Context

driver.get(“the-internet.heroku.com/login”);

eyes.CheckWindow(“Login”);

driver.findElement(By.id(“username”)).sendKeys(“tomsmith”);

driver.findElement(By.id(“password”)).sendKeys(“SuperSecretPassword”);

driver.findElement(By.id(“login”)).submit();

eyes.checkWindow(“Logged In”);

Page 35: Selenium Tips & Tricks - StarWest 2015

Why can’t Selenium get HTTP Status Codes?

Page 36: Selenium Tips & Tricks - StarWest 2015

https://shanicea2mediablog.wordpress.com/2012/04/16/bbfc-mpaa/

Copyright 2015

Page 37: Selenium Tips & Tricks - StarWest 2015

– Every Selenium Contributor Ever

“No.”

Page 38: Selenium Tips & Tricks - StarWest 2015

Still want HTTP Status Codes?

• Proxy

• BrowserMob Proxy

Page 39: Selenium Tips & Tricks - StarWest 2015

Where does a proxy fit?What does a proxy do

Page 40: Selenium Tips & Tricks - StarWest 2015

Why is it Awesome?• Blacklisting

• Bad Image Checking

• Performance Testing!

• Sort of

• Load Testing!

• Sort of

Page 41: Selenium Tips & Tricks - StarWest 2015

How to use BrowserMob

• Browser profile with BrowserMob Proxy

• Firefox

• Chrome

• Java jar

• Built-in Configuration options

Page 42: Selenium Tips & Tricks - StarWest 2015

Blacklisting

• Add it to the Blacklist

• Forces all requests to specified URL to be returned as 404

Page 43: Selenium Tips & Tricks - StarWest 2015

Broken Image Checking

• Proxy Server

• HTTP Library

• JavaScript

Page 44: Selenium Tips & Tricks - StarWest 2015

Proxy Image Checking• Get HAR

• HAR = HTTP Archive

• Find all image elements

• Go thru all entries in HAR

• Validate that each image source was not 404

Page 45: Selenium Tips & Tricks - StarWest 2015

HTTP Library

• Mostly same as before

• Use library to make a request to each source

• Validate response = 200

Page 46: Selenium Tips & Tricks - StarWest 2015

JavaScript

• Validate each image has a width

executeScript(“return arguments[0].complete && typeof arguments[0].naturalWidth != \”undefined\” && arguments[0].naturalWidth > 0”, image)

Page 47: Selenium Tips & Tricks - StarWest 2015

Performance Testing

• HAR = HTTP Archive

• Validate page load times are under SLA

• Bonus

• Send metrics to a dashboard for monitoring

Page 48: Selenium Tips & Tricks - StarWest 2015

Load Testing

• Sort of

• HAR -> JMX

• https://flood.io/har2jmx

Page 49: Selenium Tips & Tricks - StarWest 2015

Forgotten Password?Click Here

Page 50: Selenium Tips & Tricks - StarWest 2015

Options

• Gmail API to going to Open Auth

• Mailosaur

• Exchange

• Yahoo Mail

Page 51: Selenium Tips & Tricks - StarWest 2015

Mailosaur Sample

MailboxApi mailbox = new MailboxApi(mailbox, apikey);

Email[] emails = mailbox.getEmailsByRecipient("[email protected]");

assertEquals("The subject should be something", "something", emails[0].Subject);

Page 52: Selenium Tips & Tricks - StarWest 2015

Mailosaur Sample

MailboxApi mailbox = new MailboxApi(mailbox, apikey);

Email[] emails = mailbox.getEmailsByRecipient("[email protected]");

assertEquals("The subject should be something", "something", emails[0].Subject);

Page 53: Selenium Tips & Tricks - StarWest 2015

A/B Testing

Page 54: Selenium Tips & Tricks - StarWest 2015

Options A & B for A/B Testing

A. Forge Cookies

B. Appending a query to the URL

Page 55: Selenium Tips & Tricks - StarWest 2015

Cookie Forging

driver.manage.add_cookie(name: ‘optimizlyOptOut’, value: ‘true’)

Page 56: Selenium Tips & Tricks - StarWest 2015

URL Appending

driver.get(‘the-internet.herokuapp.com/abtest?optimizley_op_out=true')

Page 57: Selenium Tips & Tricks - StarWest 2015

URL Appending

driver.get(‘the-internet.herokuapp.com/abtest?optimizley_op_out=true')

Page 58: Selenium Tips & Tricks - StarWest 2015

File Handeling

Page 59: Selenium Tips & Tricks - StarWest 2015

Uploading• Don’t use AutoIt or AppleScript

• Takes over OS

• If error tests may or may not continue

• Instead

• Send path to input field

...

Page 60: Selenium Tips & Tricks - StarWest 2015

Downloading• Configure Profile to download locally

• Use HTTP library

• Check Headers for

• Content Type

• Length

...

Page 61: Selenium Tips & Tricks - StarWest 2015

Let’s Do Some More Selenium Stuff

Page 62: Selenium Tips & Tricks - StarWest 2015

Highlight Elements

Page 63: Selenium Tips & Tricks - StarWest 2015

Highlighting in JS• Get current element styling

• Execute JS

“arguments[0].setAttribute(arguments[1], arguments[2])”, element, “style”, “border: 2px solid red;”

“arguments[0].setAttribute(arguments[1], arguments[2])”, element, “style”, original_style

Page 64: Selenium Tips & Tricks - StarWest 2015

Highlighting in JS• Get current element styling

• Execute JS

“arguments[0].setAttribute(arguments[1], arguments[2])”, element, “style”, “border: 2px solid red;”

“arguments[0].setAttribute(arguments[1], arguments[2])”, element, “style”, original_style

Page 65: Selenium Tips & Tricks - StarWest 2015

Growl Notifications

Page 66: Selenium Tips & Tricks - StarWest 2015

Growl?

• What is it?

• Step by Step code execution

• Long running tests

• Non-Testing

Page 67: Selenium Tips & Tricks - StarWest 2015

Growl Options

• JavaScript

• Growl

Page 68: Selenium Tips & Tricks - StarWest 2015

JavaScript

• Add jQuery to the page

• Add growl.js to the page

• Tell growl to display message

Page 69: Selenium Tips & Tricks - StarWest 2015

Video Time

Page 70: Selenium Tips & Tricks - StarWest 2015

Growl

• Add Growl to main machine point tests to send notification to growl

• Bonus

• Forward events to other computers or mobile devices

Page 71: Selenium Tips & Tricks - StarWest 2015

Growl Samplegrowl=gntp.notifier.GrowlNotifier(

applicationName = "My App Name",

notifications = ["New Updates","New Messages"],

defaultNotifications = ["New Messages"],

hostname = "localhost", # Default

password = "" # Default

)

growl.register()

growl.notify(

noteType = "New Messages",

title = "You have a new message",

description = "A longer message description",

icon = "http://example.com/icon.png",

sticky = False,

priority = 1,

)

Page 72: Selenium Tips & Tricks - StarWest 2015

Mobile Device Emulation!

Page 73: Selenium Tips & Tricks - StarWest 2015

What!?!?!

• From Chrome go to Developer Tools

• Click This ->

• Top left corner

• Reload Page

Page 74: Selenium Tips & Tricks - StarWest 2015

Mobile Emulation Magic

Map<String, String> mobileEmulation = new HashMap<String, String>(); mobileEmulation.put("deviceName", "Google Nexus 5");

Map<String, Object> chromeOptions = new HashMap<String, Object>(); chromeOptions.put("mobileEmulation", mobileEmulation); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); WebDriver driver = new ChromeDriver(capabilities);

Page 75: Selenium Tips & Tricks - StarWest 2015

Mobile Emulation Magic

Map<String, String> mobileEmulation = new HashMap<String, String>(); mobileEmulation.put("deviceName", "Google Nexus 5");

Map<String, Object> chromeOptions = new HashMap<String, Object>(); chromeOptions.put("mobileEmulation", mobileEmulation); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); WebDriver driver = new ChromeDriver(capabilities);

Page 76: Selenium Tips & Tricks - StarWest 2015

Training the Terminator

Page 77: Selenium Tips & Tricks - StarWest 2015

Huh??• Jason Huggins

• @hugs

• Creator of Selenium

• Likes Robots

• Built Robot

Page 78: Selenium Tips & Tricks - StarWest 2015

Robot Playing Music

https://youtu.be/9F-Vb9EhdIc

Page 79: Selenium Tips & Tricks - StarWest 2015

More Selenium Resources• bit.ly/se-bootcamp

• bit.ly/se-guidebook

• bit.ly/se-weekly-tips

• Selenium Conference YouTube Channel

• the-internet.herokuapp.com

• Great resource for practicing Selenium

Page 80: Selenium Tips & Tricks - StarWest 2015

Questions?

Page 81: Selenium Tips & Tricks - StarWest 2015

Thanks for Staying

@andrewmkrug

[email protected]

Thanks

Page 82: Selenium Tips & Tricks - StarWest 2015

Revcontent

We’re Hiring!


Recommended