Date post: | 15-Jan-2017 |
Category: |
Technology |
Upload: | tsundberg |
View: | 548 times |
Download: | 0 times |
Selenium WebDriver
Test automation for web applications
© Think Code AB, http://www.thinkcode.se
Thomas Sundberg
Stockholm, SwedenThink Code AB
[email protected]@thomassundberg
Blog: https://thomassundberg.wordpress.com/Code:
https://github.com/tsundberg/selenium-test-automation/tree
/luxsoft-january-2015
© Think Code AB, http://www.thinkcode.se
TESTING OF WEB APPLICATIONS
© Think Code AB, http://www.thinkcode.se
Testing of web applications
• Introduction to testing• Manual testing• Automated testing• Selenium
© Think Code AB, http://www.thinkcode.se
Introduction
• Why testing?• What to test?• When to test?• How to test?
© Think Code AB, http://www.thinkcode.se
Why testing?
• Create confidence to release
© Think Code AB, http://www.thinkcode.se
Why release?
• Create value• Reduce waste• Get feedback from real users
– Was this what they wanted?
© Think Code AB, http://www.thinkcode.se
Create value
• Using the software is the only way our users/customers can benefit from our work
• This is the only reason why they pay us to create it
© Think Code AB, http://www.thinkcode.se
Reduce waste
• Waste until it is used• Unused code is waste• Unreleased code is unused code• Waste until it is deployed
© Think Code AB, http://www.thinkcode.se
Feedback from real users
• Is this was what they wanted?• Release early• Release often
© Think Code AB, http://www.thinkcode.se
When to test?
• As soon as possible
© Think Code AB, http://www.thinkcode.se
Why?
• Small problems are easy to fix• Young problems are small• Catch them before they grow up and become
big problems– Large, old problems can be catastrophic
© Think Code AB, http://www.thinkcode.se
How to test?
• Manual• Automated
© Think Code AB, http://www.thinkcode.se
Manual
• Always through the user interface• Slow• Inconsistent• Doesn't scale• Will find new, interesting bugs
© Think Code AB, http://www.thinkcode.se
Automated
• Different levels– User interface– Code
• Faster• Consistent• Scales• Will never find new, interesting bugs
© Think Code AB, http://www.thinkcode.se
Manual vs. Automated
• Manual test– Understand the problem– Know the application
• Automated test– Understand the problem– Know the application– Programming
© Think Code AB, http://www.thinkcode.se
SELENIUM
© Think Code AB, http://www.thinkcode.se
Browser automation
• Can be used for tests• Doesn’t have to be used for testing
© Think Code AB, http://www.thinkcode.se
Where does the name come from?
• Selenium is a chemical element, Se • Treatment for Mercury poisoning
• Mercury is a closed source web testing tool• Selenium is a cure for Mercury
© Think Code AB, http://www.thinkcode.se
Tests
• Many frameworks• Test NG• Spock• JUnit• Others
© Think Code AB, http://www.thinkcode.se
Testing pyramid
End to end
Integration
Unit
Slow
Faster
Fast
Many reasons to fail
Many reasons to fail
One reason to fail
Faster is better Fewer reasons to fail is better
© Think Code AB, http://www.thinkcode.se
End to end
• From the user interface• Very slow
– It takes forever to start a browser and do something
• Very fragile– Lots of reasons why a test will fail
• The database is broken• The user interface is missing a component• Many more…
© Think Code AB, http://www.thinkcode.se
Doesn’t scale
• Impossible to test all paths– 10 entrances from the ui– 5 paths through the controller– 7 paths through the model– 10 * 5 * 7 = 350 paths– 350 tests
© Think Code AB, http://www.thinkcode.se
Integration
• Verify that components works together– Is the database properly configured?– Is the queue started?
• Slow• Fragile
– Many reasons to fail
© Think Code AB, http://www.thinkcode.se
Unit
• Small pieces of functionality at a low level• Very fast• Stable
– Only one reason to fail– Failures are easy to understand
• Used for verifying all use cases for each implementation
• Possible to verify all paths
© Think Code AB, http://www.thinkcode.se
Good tests
• Must be easy to– Understand– Maintain– Change– Extend
© Think Code AB, http://www.thinkcode.se
Testing pyramid
End to end
Integration
Unit
Slow
Faster
Fast
Many reasons to fail
Many reasons to fail
One reason to fail
Faster is better Fewer reasons to fail is better
© Think Code AB, http://www.thinkcode.se
Inverted Testing pyramid
End to end
Integration
Unit
Slow
Faster
Fast
Many reasons to fail
Many reasons to fail
One reason to fail
Faster is better Fewer reasons to fail is better
© Think Code AB, http://www.thinkcode.se
Selenium echo system
• Selenium IDE• Selenium WebDriver
© Think Code AB, http://www.thinkcode.se
Selenium IDE
• Firefox plugin• Record and replay
• If you want to– create quick bug reproduction scripts– create scripts to aid in automation-aided
exploratory testing
© Think Code AB, http://www.thinkcode.se
Selenium WebDriver• A programming framework• Local in any browser you have installed
– Open source– Anyone can use it
• Many programming languages– We will use Java
• If you want to– create robust, browser-based regression automation suites and
tests– scale and distribute scripts across many environments
© Think Code AB, http://www.thinkcode.se
Selenium WebDriver
• W3C standard– Not done, but well on its way
• Every W3C browser must support WebDriver
© Think Code AB, http://www.thinkcode.se
Selenium Hub
• Automate a browser remote• Enable us to do cross browser testing
– Browser– Operating system
© Think Code AB, http://www.thinkcode.se
WebDriver remote
• Same API as regular WebDriver– If it works local, you can execute it remote– SauceLabs
© Think Code AB, http://www.thinkcode.se
SELENIUM IDE
© Think Code AB, http://www.thinkcode.se
Hello, world!
• http://selenium.thinkcode.se• Find an element and verify the text
Fill out a form
• Change password
© Think Code AB, http://www.thinkcode.se
© Think Code AB, http://www.thinkcode.se
WHAT TO TEST?
© Think Code AB, http://www.thinkcode.se
What should you test?
• Testing theatre• Test stuff that matters
© Think Code AB, http://www.thinkcode.se
Security theatre
• Not aiming for security• Aim to make people feel safe• Security check on airports
– Why do they take you water bottle?– If it is dangerous, remove it far away so it can't be
dangerous for you
© Think Code AB, http://www.thinkcode.se
Don’t test everything
• Enough to feel safe
© Think Code AB, http://www.thinkcode.se
Testing pyramid
End to end
Integration
Unit
© Think Code AB, http://www.thinkcode.se
User interface
• Do not test everything through the user interface
• Test at the right level
© Think Code AB, http://www.thinkcode.se
End to end
• Can you login?• Can you fail to log in?• Can you do a purchase?
– At least place something in the shopping bag
© Think Code AB, http://www.thinkcode.se
Integration
• Connect to the database• Connect to a queue
© Think Code AB, http://www.thinkcode.se
Unit level
• Password algorithm
© Think Code AB, http://www.thinkcode.se
You must be in control
• Collaborators• Mock or Stub
© Think Code AB, http://www.thinkcode.se
Database
• Know about the content• Decide what the content should be• This makes End to end complicated
© Think Code AB, http://www.thinkcode.se
What should you automate?
• Any test that should be repeated• At the right level...
© Think Code AB, http://www.thinkcode.se
What should you not automate?
• Expensive tests– Buying using a credit card– Probably enough to do one manual test– Setup a fake server that mimics the payment
service– http://wiremock.org
© Think Code AB, http://www.thinkcode.se
One off
• If it truly exists• Automate it the second time
© Think Code AB, http://www.thinkcode.se
SELENIUM WEBDRIVER
© Think Code AB, http://www.thinkcode.se
First WebDriver test
• Export the code from Selenium IDE– The code is ok– Not pretty– Ok to start with
• Good for finding elements
© Think Code AB, http://www.thinkcode.se
Browser tools
• Firebug• Firepath• Other favorites?
© Think Code AB, http://www.thinkcode.se
First handmade WebDriver test
• Hello world• Without page object• With page object
© Think Code AB, http://www.thinkcode.se
Hand made is better
• You have better control• You know what you did• Slower?
– Maybe at first, but not in the long run
© Think Code AB, http://www.thinkcode.se
Page objects
• Separation of navigation logic and test– Only need to change the page object when the
layout changes– The tests remain unchanged if the logic is the
same
© Think Code AB, http://www.thinkcode.se
Lazy
• Hard to solve a hard problem• Better to solve many small
© Think Code AB, http://www.thinkcode.se
One class per page
• Abstracts the page functionality• Hide the page navigation
© Think Code AB, http://www.thinkcode.se
Simplifies the tests
• The test focus on WHAT the application should do
• Not on HOW• Behavior is interesting
© Think Code AB, http://www.thinkcode.se
Supply the browser
• Through the constructor– Allow you to use the same page object with
different browsers• Verify that the current page is the right page
– Check title or similar characteristics• Throw an exception if you can’t verify the
page
© Think Code AB, http://www.thinkcode.se
assertThat
• Signals through exception• assertThat(actaulTitle, is(expectedTitle));
© Think Code AB, http://www.thinkcode.se
Example - Check boxes
http://selenium.thinkcode.se/selectColor.html
© Think Code AB, http://www.thinkcode.se
Test organization
By technology By functional area
© Think Code AB, http://www.thinkcode.se
Test organization
• Separate on functional areas• Do not separate on technology
– A new developer want to find the relevant test and page objects fast
• All tests and page object for a certain area should live in the same package
© Think Code AB, http://www.thinkcode.se
Selenium echo system
• Selenium IDE– Firefox plugin
• WebDriver– Programming api
• Selenium hub– Remote– Different browsers– Different operating systems
© Think Code AB, http://www.thinkcode.se
Testing pyramid
End to end
Integration
Unit
Test things at the right level
• End to end• Integration• Unit
© Think Code AB, http://www.thinkcode.se
End to end
• Verify that the system is alive• Most crucial flow
– Buy a product– Book a trip
• Very slow• Lots of reasons to fail
© Think Code AB, http://www.thinkcode.se
Integration
• Slow• Many reasons to fail• Doesn’t scale
© Think Code AB, http://www.thinkcode.se
Unit
• Fast• Only one reason to fail• Algorithms• Password
© Think Code AB, http://www.thinkcode.se
Testing theater
• Verifying important parts in the system• Not for satisfying a managers metric
© Think Code AB, http://www.thinkcode.se
WebDriver• close()• findElement(By by)• findElements(By by)• get(java.lang.String url)• getCurrentUrl()• getPageSource()• getTitle()• getWindowHandle()• getWindowHandles()• manage()• navigate()• quit()• switchTo()
© Think Code AB, http://www.thinkcode.se
Locators• By.id(java.lang.String id)
– The preferred locater if the IDs are unique• By.name(java.lang.String name)
– Almost as good as ID, if the name is unique• By.xpath(java.lang.String xpathExpression)
– Depends on the page structure• By.cssSelector(java.lang.String selector)
– Finds elements via the driver's underlying W3 Selector engine.• By.linkText(java.lang.String linkText)
– Complicated if your site supports many languages• By.partialLinkText(java.lang.String linkText)
– Complicated if your site supports many languages• By.tagName(java.lang.String name)
– Probably not a unique element, all h1• By.className(java.lang.String className)
– Finds elements based on the value of the class attribute.
© Think Code AB, http://www.thinkcode.se
WebElement• clear()• click()• findElement(By by)• findElements(By by)• getAttribute(java.lang.String name)• getCssValue(java.lang.String propertyName)• getLocation()• getSize()• getTagName()• getText()• isDisplayed()• isEnabled()• isSelected()• sendKeys(java.lang.CharSequence... keysToSend)• submit()
© Think Code AB, http://www.thinkcode.se
Page objects
• Separate navigation and logic• Clearer and easier tests• Supply the browser• Verify the correct page
© Think Code AB, http://www.thinkcode.se
Interested in more?
Join me in Timisoara, Romania in April forA two day, in depth, Selenium course.
http://mozaicworks.com/public-trainings-and-workshops/selenium-webdriver-test-automation-for-web-applications/
© Think Code AB, http://www.thinkcode.se
Thomas Sundberg
Stockholm, SwedenThink Code AB
[email protected]@thomassundberg
Blog: https://thomassundberg.wordpress.com/Code:
https://github.com/tsundberg/selenium-test-automation/tree
/luxsoft-january-2015