+ All Categories
Home > Technology > Selenium WebDriver - Test automation for web applications

Selenium WebDriver - Test automation for web applications

Date post: 15-Jan-2017
Category:
Upload: tsundberg
View: 548 times
Download: 0 times
Share this document with a friend
78
Selenium WebDriver Test automation for web applications
Transcript
Page 1: Selenium WebDriver - Test automation for web applications

Selenium WebDriver

Test automation for web applications

Page 2: 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

Page 3: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

TESTING OF WEB APPLICATIONS

Page 4: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Testing of web applications

• Introduction to testing• Manual testing• Automated testing• Selenium

Page 5: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Introduction

• Why testing?• What to test?• When to test?• How to test?

Page 6: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Why testing?

• Create confidence to release

Page 7: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Why release?

• Create value• Reduce waste• Get feedback from real users

– Was this what they wanted?

Page 8: Selenium WebDriver - Test automation for web applications

© 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

Page 9: Selenium WebDriver - Test automation for web applications

© 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

Page 10: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Feedback from real users

• Is this was what they wanted?• Release early• Release often

Page 11: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

When to test?

• As soon as possible

Page 12: Selenium WebDriver - Test automation for web applications

© 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

Page 13: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

How to test?

• Manual• Automated

Page 14: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Manual

• Always through the user interface• Slow• Inconsistent• Doesn't scale• Will find new, interesting bugs

Page 15: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Automated

• Different levels– User interface– Code

• Faster• Consistent• Scales• Will never find new, interesting bugs

Page 16: Selenium WebDriver - Test automation for web applications

© 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

Page 17: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

SELENIUM

Page 18: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Browser automation

• Can be used for tests• Doesn’t have to be used for testing

Page 19: Selenium WebDriver - Test automation for web applications

© 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

Page 20: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Tests

• Many frameworks• Test NG• Spock• JUnit• Others

Page 21: Selenium WebDriver - Test automation for web applications

© 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

Page 22: Selenium WebDriver - Test automation for web applications

© 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…

Page 23: Selenium WebDriver - Test automation for web applications

© 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

Page 24: Selenium WebDriver - Test automation for web applications

© 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

Page 25: Selenium WebDriver - Test automation for web applications

© 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

Page 26: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Good tests

• Must be easy to– Understand– Maintain– Change– Extend

Page 27: Selenium WebDriver - Test automation for web applications

© 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

Page 28: Selenium WebDriver - Test automation for web applications

© 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

Page 29: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Selenium echo system

• Selenium IDE• Selenium WebDriver

Page 30: Selenium WebDriver - Test automation for web applications

© 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

Page 31: Selenium WebDriver - Test automation for web applications

© 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

Page 32: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Selenium WebDriver

• W3C standard– Not done, but well on its way

• Every W3C browser must support WebDriver

Page 33: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Selenium Hub

• Automate a browser remote• Enable us to do cross browser testing

– Browser– Operating system

Page 34: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

WebDriver remote

• Same API as regular WebDriver– If it works local, you can execute it remote– SauceLabs

Page 35: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

SELENIUM IDE

Page 36: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Hello, world!

• http://selenium.thinkcode.se• Find an element and verify the text

Page 37: Selenium WebDriver - Test automation for web applications

Fill out a form

• Change password

© Think Code AB, http://www.thinkcode.se

Page 38: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

WHAT TO TEST?

Page 39: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

What should you test?

• Testing theatre• Test stuff that matters

Page 40: Selenium WebDriver - Test automation for web applications

© 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

Page 41: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Don’t test everything

• Enough to feel safe

Page 42: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Testing pyramid

End to end

Integration

Unit

Page 43: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

User interface

• Do not test everything through the user interface

• Test at the right level

Page 44: Selenium WebDriver - Test automation for web applications

© 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

Page 45: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Integration

• Connect to the database• Connect to a queue

Page 46: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Unit level

• Password algorithm

Page 47: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

You must be in control

• Collaborators• Mock or Stub

Page 48: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Database

• Know about the content• Decide what the content should be• This makes End to end complicated

Page 49: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

What should you automate?

• Any test that should be repeated• At the right level...

Page 50: Selenium WebDriver - Test automation for web applications

© 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

Page 51: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

One off

• If it truly exists• Automate it the second time

Page 52: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

SELENIUM WEBDRIVER

Page 53: Selenium WebDriver - Test automation for web applications

© 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

Page 54: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Browser tools

• Firebug• Firepath• Other favorites?

Page 55: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

First handmade WebDriver test

• Hello world• Without page object• With page object

Page 56: Selenium WebDriver - Test automation for web applications

© 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

Page 57: Selenium WebDriver - Test automation for web applications

© 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

Page 58: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Lazy

• Hard to solve a hard problem• Better to solve many small

Page 59: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

One class per page

• Abstracts the page functionality• Hide the page navigation

Page 60: Selenium WebDriver - Test automation for web applications

© 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

Page 61: Selenium WebDriver - Test automation for web applications

© 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

Page 62: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

assertThat

• Signals through exception• assertThat(actaulTitle, is(expectedTitle));

Page 63: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Example - Check boxes

http://selenium.thinkcode.se/selectColor.html

Page 64: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Test organization

By technology By functional area

Page 65: Selenium WebDriver - Test automation for web applications

© 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

Page 66: Selenium WebDriver - Test automation for web applications

© 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

Page 67: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Testing pyramid

End to end

Integration

Unit

Page 68: Selenium WebDriver - Test automation for web applications

Test things at the right level

• End to end• Integration• Unit

Page 69: Selenium WebDriver - Test automation for web applications

© 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

Page 70: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Integration

• Slow• Many reasons to fail• Doesn’t scale

Page 71: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Unit

• Fast• Only one reason to fail• Algorithms• Password

Page 72: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Testing theater

• Verifying important parts in the system• Not for satisfying a managers metric

Page 73: Selenium WebDriver - Test automation for web applications

© 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()

Page 74: Selenium WebDriver - Test automation for web applications

© 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.

Page 75: Selenium WebDriver - Test automation for web applications

© 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()

Page 76: Selenium WebDriver - Test automation for web applications

© Think Code AB, http://www.thinkcode.se

Page objects

• Separate navigation and logic• Clearer and easier tests• Supply the browser• Verify the correct page

Page 77: Selenium WebDriver - Test automation for web applications

© 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/

Page 78: 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


Recommended