selenium tips & tricks - starwest 2015
TRANSCRIPT
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
Revcontent
• Market Leader for Native Advertising
• 2-3 times greater RPM than Competitors
• Related Stories Around the Web
Revcontent
We’re Hiring!
!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
Selenium
• Sent from the Future
• Is a toolbox
• ! Tool
Expected Outcome
foreach attendee in here:
AssertThat( learnedNewSelenium = True)
To do that just raise your hand every time you learn
! 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
See what I did there?
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
–Johnny Appleseed
“Sprinkles are for winners.”
You are all winners!
Headless
Not ThisHeadless Horseman
https://www.flickr.com/photos/fanabouttown/10719478555
Or ThisHorseless Headsman
http://www.framestore.com/work/snickers-horseless-headsman
What is it?
• No GUI
• Generally
Benefits
• Speed
• Grid Maintenance
• Screenshots
Headless with Xvfbfor *nix
Xvfb?
• X virtual frame buffer
• In-memory display server
• run graphical applications with out display
Xvfb Option 1• Start Xvfb on a port
• Tell terminal to use the port
• Run Test
Xvfb :99
export DISPLAY=:99
ruby example.rb
Option 2
• Use Xvfb-run to launch tests
xvfb-run ruby example.rb
Option 3
• Use the headless gem
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
Visual Testing
What?!?!?
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!!!
Challenges
• Complex
• False Positives
• Shifted content
• Dynamic content
Training Skynet
Why Applitools• https://applitools.com
• Compare Layout of Page
• Disregard areas
• Finds related problems
• Deals with the Challenges
• I can do more stuff!!!
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();
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”);
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”);
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”);
Why can’t Selenium get HTTP Status Codes?
https://shanicea2mediablog.wordpress.com/2012/04/16/bbfc-mpaa/
Copyright 2015
– Every Selenium Contributor Ever
“No.”
Still want HTTP Status Codes?
• Proxy
• BrowserMob Proxy
Where does a proxy fit?What does a proxy do
Why is it Awesome?• Blacklisting
• Bad Image Checking
• Performance Testing!
• Sort of
• Load Testing!
• Sort of
How to use BrowserMob
• Browser profile with BrowserMob Proxy
• Firefox
• Chrome
• Java jar
• Built-in Configuration options
Blacklisting
• Add it to the Blacklist
• Forces all requests to specified URL to be returned as 404
Broken Image Checking
• Proxy Server
• HTTP Library
• JavaScript
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
HTTP Library
• Mostly same as before
• Use library to make a request to each source
• Validate response = 200
JavaScript
• Validate each image has a width
executeScript(“return arguments[0].complete && typeof arguments[0].naturalWidth != \”undefined\” && arguments[0].naturalWidth > 0”, image)
Performance Testing
• HAR = HTTP Archive
• Validate page load times are under SLA
• Bonus
• Send metrics to a dashboard for monitoring
Forgotten Password?Click Here
Mailosaur Sample
MailboxApi mailbox = new MailboxApi(mailbox, apikey);
Email[] emails = mailbox.getEmailsByRecipient("[email protected]");
assertEquals("The subject should be something", "something", emails[0].Subject);
Mailosaur Sample
MailboxApi mailbox = new MailboxApi(mailbox, apikey);
Email[] emails = mailbox.getEmailsByRecipient("[email protected]");
assertEquals("The subject should be something", "something", emails[0].Subject);
A/B Testing
Options A & B for A/B Testing
A. Forge Cookies
B. Appending a query to the URL
Cookie Forging
driver.manage.add_cookie(name: ‘optimizlyOptOut’, value: ‘true’)
URL Appending
driver.get(‘the-internet.herokuapp.com/abtest?optimizley_op_out=true')
URL Appending
driver.get(‘the-internet.herokuapp.com/abtest?optimizley_op_out=true')
File Handeling
Uploading• Don’t use AutoIt or AppleScript
• Takes over OS
• If error tests may or may not continue
• Instead
• Send path to input field
...
Downloading• Configure Profile to download locally
• Use HTTP library
• Check Headers for
• Content Type
• Length
...
Let’s Do Some More Selenium Stuff
Highlight Elements
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
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
Growl Notifications
Growl?
• What is it?
• Step by Step code execution
• Long running tests
• Non-Testing
Growl Options
• JavaScript
• Growl
JavaScript
• Add jQuery to the page
• Add growl.js to the page
• Tell growl to display message
Video Time
Growl
• Add Growl to main machine point tests to send notification to growl
• Bonus
• Forward events to other computers or mobile devices
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,
)
Mobile Device Emulation!
What!?!?!
• From Chrome go to Developer Tools
• Click This ->
• Top left corner
• Reload Page
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);
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);
Training the Terminator
Huh??• Jason Huggins
• @hugs
• Creator of Selenium
• Likes Robots
• Built Robot
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
Questions?
Revcontent
We’re Hiring!