automated ui testing done right (qmsdnug)

154
Automated UI Testing Done Right

Upload: mehdi-khalili

Post on 22-May-2015

2.358 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Automated UI Testing Done Right (QMSDNUG)

Automated UI Testing

Done Right

Page 2: Automated UI Testing Done Right (QMSDNUG)

Mehdi KhaliliSenior Developer at Readify

Active Open Source Projects:• BDDfy• Seleno• Humanizer

Blog: www.mehdi-khalili.comEmail: [email protected]: @MehdiKhalili

Page 3: Automated UI Testing Done Right (QMSDNUG)

These practices are performed by professional developers and testers.

Please DO try this at home

Authorized and written by Mehdi Khalili

Page 4: Automated UI Testing Done Right (QMSDNUG)

framework agnostic ideas and patterns

Page 5: Automated UI Testing Done Right (QMSDNUG)

can apply these with any UI and UI Testing framework

Page 6: Automated UI Testing Done Right (QMSDNUG)

… but for this talk we are going to use

Page 7: Automated UI Testing Done Right (QMSDNUG)

Seleniuman awesome automated UI testing

framework

Page 8: Automated UI Testing Done Right (QMSDNUG)

Seleniumhttp://seleniumhq.org/projects/webdriver/

PM> Install-Package

Selenium.WebDriver

Page 9: Automated UI Testing Done Right (QMSDNUG)
Page 10: Automated UI Testing Done Right (QMSDNUG)

BDDfyA simple BDD framework to use and extend!

BDDfy turns your traditional unit testsinto BDD behaviours

Page 11: Automated UI Testing Done Right (QMSDNUG)

BDDfyhttp://teststack.github.com/TestStack.BDDfy/

PM> Install-Package

TestStack.BDDfy

Page 12: Automated UI Testing Done Right (QMSDNUG)

Selenohelps you write

Automated UI Tests the RIGHT way!

Page 13: Automated UI Testing Done Right (QMSDNUG)

Selenohttp://teststack.github.com/TestStack.Seleno/

PM> Install-Package TestStack.Seleno

Page 14: Automated UI Testing Done Right (QMSDNUG)

samples are from Seleno codebase and can be found at

https://github.com/TestStack/TestStack.Seleno

Page 15: Automated UI Testing Done Right (QMSDNUG)

Agenda

UI Testing: a likely failureFrom horrid to awesome in three stepsA few tipsQ&A

Page 16: Automated UI Testing Done Right (QMSDNUG)

UI Testing!a likely failure

speaking of experience

Page 17: Automated UI Testing Done Right (QMSDNUG)

a lot of teamsdo

UI Testing

Page 18: Automated UI Testing Done Right (QMSDNUG)

a lot of teamshave a great start at

UI Testing

Page 19: Automated UI Testing Done Right (QMSDNUG)
Page 20: Automated UI Testing Done Right (QMSDNUG)

theythen struggle with

UI Testing

Page 21: Automated UI Testing Done Right (QMSDNUG)

and a lot of teamsthen fail atUI Testing

Page 22: Automated UI Testing Done Right (QMSDNUG)

because UI Tests are

Page 23: Automated UI Testing Done Right (QMSDNUG)

because UI Tests are

hard to write

Page 24: Automated UI Testing Done Right (QMSDNUG)

because UI Tests are

hard to maintain

Page 25: Automated UI Testing Done Right (QMSDNUG)

because UI Tests are

brittle

Page 26: Automated UI Testing Done Right (QMSDNUG)

but

you can

mitigatethese issues

Page 27: Automated UI Testing Done Right (QMSDNUG)

If you

do it

RIGHT

Page 28: Automated UI Testing Done Right (QMSDNUG)

test codeis

code

Page 29: Automated UI Testing Done Right (QMSDNUG)

apply

S.R.P. on your

code?

Page 30: Automated UI Testing Done Right (QMSDNUG)

apply

S.R.P.on your

tests

Page 31: Automated UI Testing Done Right (QMSDNUG)

apply

D.R.Y.on your

tests

Page 32: Automated UI Testing Done Right (QMSDNUG)

care about your

tests as much as you care about your

code

Page 33: Automated UI Testing Done Right (QMSDNUG)

or you willwaste a lot of time

Page 34: Automated UI Testing Done Right (QMSDNUG)

or you willfail

Page 35: Automated UI Testing Done Right (QMSDNUG)

from horrid to awesomein three steps

Page 36: Automated UI Testing Done Right (QMSDNUG)

a quick look at the sample

https://github.com/TestStack/TestStack.Seleno

Page 37: Automated UI Testing Done Right (QMSDNUG)

guaranteed to

fail

Page 38: Automated UI Testing Done Right (QMSDNUG)
Page 39: Automated UI Testing Done Right (QMSDNUG)

proceduralduplicated logicduplicated selectorsmagic strings

Page 40: Automated UI Testing Done Right (QMSDNUG)

Step 1:

D.R.Y.your tests with

Page Object

Page 41: Automated UI Testing Done Right (QMSDNUG)

Page Objectbrings

OO to tests)

Page 42: Automated UI Testing Done Right (QMSDNUG)

a Page Object per page under test

Page 43: Automated UI Testing Done Right (QMSDNUG)

a textboxon the page becomes a

string propertyon the Page Object

Page 44: Automated UI Testing Done Right (QMSDNUG)

filling a textbox on the page turns into setting a string property on the

Page Object

Page 45: Automated UI Testing Done Right (QMSDNUG)

instead of

you will get

Page 46: Automated UI Testing Done Right (QMSDNUG)

a checkboxon the page becomes a

bool propertyon the Page Object

Page 47: Automated UI Testing Done Right (QMSDNUG)

ticking a checkbox on the page turns into setting a

bool property on the Page Object

Page 48: Automated UI Testing Done Right (QMSDNUG)

a link on the page becomes a

method on the Page Object

Page 49: Automated UI Testing Done Right (QMSDNUG)

clicking a link on the pageturns into calling a method

on the Page Object

Page 50: Automated UI Testing Done Right (QMSDNUG)

instead of

you will get

Page 51: Automated UI Testing Done Right (QMSDNUG)

any actionon the page becomes a

method on the Page Object

Page 52: Automated UI Testing Done Right (QMSDNUG)

… and you will get another Page Object as the return

value of the method

Page 53: Automated UI Testing Done Right (QMSDNUG)

chain your calls

Page 54: Automated UI Testing Done Right (QMSDNUG)
Page 55: Automated UI Testing Done Right (QMSDNUG)

step 2:

Page Components

Compose your Page Objects of smaller pieces

Page 56: Automated UI Testing Done Right (QMSDNUG)

some pages are

big

Page 57: Automated UI Testing Done Right (QMSDNUG)

some pages are

complex

Page 58: Automated UI Testing Done Right (QMSDNUG)

remember

S.R.P.?

Page 59: Automated UI Testing Done Right (QMSDNUG)

would you write

big and

complex classes in your

code?

Page 60: Automated UI Testing Done Right (QMSDNUG)

do NOT write

big and

complex Page Objects

Page 61: Automated UI Testing Done Right (QMSDNUG)

use

Page Componentsto break down your

Page Objects

Page 62: Automated UI Testing Done Right (QMSDNUG)

use

Page Componentsfor

gridspanels

rows inmodal pop-ups

menus

Page 63: Automated UI Testing Done Right (QMSDNUG)

Page ComponentsD.R.Y.

your tests even more

Page 64: Automated UI Testing Done Right (QMSDNUG)

instead of

you will get

Page 65: Automated UI Testing Done Right (QMSDNUG)

and you can compose other

Page Objectsusing these

Page Components

Page 66: Automated UI Testing Done Right (QMSDNUG)
Page 67: Automated UI Testing Done Right (QMSDNUG)

Page Object &Page Component

D.R.Y.your test

Page 68: Automated UI Testing Done Right (QMSDNUG)

Page Object &Page Component

lead into

S.R.P.

Page 69: Automated UI Testing Done Right (QMSDNUG)

proceduralduplicating logicduplicating selectorsmagic strings

Page 70: Automated UI Testing Done Right (QMSDNUG)
Page 71: Automated UI Testing Done Right (QMSDNUG)

… and the code is still

ugly

Page 72: Automated UI Testing Done Right (QMSDNUG)
Page 73: Automated UI Testing Done Right (QMSDNUG)
Page 74: Automated UI Testing Done Right (QMSDNUG)

step 3:

Strongly typedPage Object

Page 75: Automated UI Testing Done Right (QMSDNUG)

you use view models

in your code

Page 76: Automated UI Testing Done Right (QMSDNUG)
Page 77: Automated UI Testing Done Right (QMSDNUG)
Page 78: Automated UI Testing Done Right (QMSDNUG)
Page 79: Automated UI Testing Done Right (QMSDNUG)

why not use view models

in your tests?

Page 80: Automated UI Testing Done Right (QMSDNUG)
Page 81: Automated UI Testing Done Right (QMSDNUG)

(unofficial) step 4:

Tests as Living Documentation

Page 82: Automated UI Testing Done Right (QMSDNUG)

how do you get

requirements from the business?

Page 83: Automated UI Testing Done Right (QMSDNUG)

asked to work one a new feature or a bug

programmer: “can I see the req. for this?”

Page 84: Automated UI Testing Done Right (QMSDNUG)

feeling very proud for generating the most awesome requirements

BA: “here you go”

Page 85: Automated UI Testing Done Right (QMSDNUG)

requirements book, anyone!?!

Page 86: Automated UI Testing Done Right (QMSDNUG)

requirements in Word ===

a lot of wasted time

Page 87: Automated UI Testing Done Right (QMSDNUG)

requirements in Word ===

out of date requirements

Page 88: Automated UI Testing Done Right (QMSDNUG)

requirements in Word ===

a lot of confusion

Page 89: Automated UI Testing Done Right (QMSDNUG)

requirements in Word ===

misinterpretation

Page 90: Automated UI Testing Done Right (QMSDNUG)

requirements in Word ===

wrong product

Page 91: Automated UI Testing Done Right (QMSDNUG)

BDD to the rescue

http://www.mehdi-khalili.com/bdd-to-the-rescue

Page 92: Automated UI Testing Done Right (QMSDNUG)

BDD to the rescue

reducing misinterpretation

Page 93: Automated UI Testing Done Right (QMSDNUG)

BDD to the rescue

YAGNI

Page 94: Automated UI Testing Done Right (QMSDNUG)

BDD to the rescue

early and frequent feedback

Page 95: Automated UI Testing Done Right (QMSDNUG)

BDD to the rescue

test suite defined by BAs

Page 96: Automated UI Testing Done Right (QMSDNUG)

BDD to the rescue

living documentation

Page 97: Automated UI Testing Done Right (QMSDNUG)

write your UI Tests based on

acceptance criteria

Page 98: Automated UI Testing Done Right (QMSDNUG)

use a BDD framework to implement your

acceptance criteria

Page 99: Automated UI Testing Done Right (QMSDNUG)
Page 100: Automated UI Testing Done Right (QMSDNUG)

use the test results

as a progress report

Page 101: Automated UI Testing Done Right (QMSDNUG)

use the test results

to support and reduce manual testing

Page 102: Automated UI Testing Done Right (QMSDNUG)

computers are having fun while we are doing the repetitive tasks they are built to do

Page 103: Automated UI Testing Done Right (QMSDNUG)

A few tips

Page 104: Automated UI Testing Done Right (QMSDNUG)

Do NOT use Thread.Sleep

Page 105: Automated UI Testing Done Right (QMSDNUG)

Thread.Sleepis slow

Page 106: Automated UI Testing Done Right (QMSDNUG)

Thread.Sleepis brittle

Page 107: Automated UI Testing Done Right (QMSDNUG)

often need to wait a bit longer for things to load?

Page 108: Automated UI Testing Done Right (QMSDNUG)

use implicit waits

Page 109: Automated UI Testing Done Right (QMSDNUG)

need to wait longer for specific elements to load?

Page 110: Automated UI Testing Done Right (QMSDNUG)

use explicit waits

Page 111: Automated UI Testing Done Right (QMSDNUG)

need to wait for an AJAX call to finish?

Page 112: Automated UI Testing Done Right (QMSDNUG)

use javascript

Page 113: Automated UI Testing Done Right (QMSDNUG)

chooseright selectors

Page 114: Automated UI Testing Done Right (QMSDNUG)

page structure changes

Page 115: Automated UI Testing Done Right (QMSDNUG)

do NOT

be fuzzywith your selectors

Page 116: Automated UI Testing Done Right (QMSDNUG)

do NOT

rely on the structure of your

page

Page 117: Automated UI Testing Done Right (QMSDNUG)

do NOT

rely on the surrounding elements

Page 118: Automated UI Testing Done Right (QMSDNUG)

By.XPath("//ul[@id='album-list']/li[3]/a/span")

you’re kidding, right?!

Page 119: Automated UI Testing Done Right (QMSDNUG)

we use interfaces and DI all over our code to make it unit testable

Page 120: Automated UI Testing Done Right (QMSDNUG)

do what it takes to

support your tests

Page 121: Automated UI Testing Done Right (QMSDNUG)

be explicit:add id on your elements to

support your tests

Page 122: Automated UI Testing Done Right (QMSDNUG)

TARGET your elements

directlywhen possible

Page 123: Automated UI Testing Done Right (QMSDNUG)

only one test

per action

Page 124: Automated UI Testing Done Right (QMSDNUG)

do you have workflows?

Page 125: Automated UI Testing Done Right (QMSDNUG)

do one test per page/action

Page 126: Automated UI Testing Done Right (QMSDNUG)

then do one test for entire flow

Page 127: Automated UI Testing Done Right (QMSDNUG)

do NOT setup your required

state through UI

Page 128: Automated UI Testing Done Right (QMSDNUG)

that will be slow and brittle

Page 129: Automated UI Testing Done Right (QMSDNUG)

setup your data through code

Page 130: Automated UI Testing Done Right (QMSDNUG)

and navigate to the page under

test directly

Page 131: Automated UI Testing Done Right (QMSDNUG)

use strongly typed actions for that

Page 132: Automated UI Testing Done Right (QMSDNUG)

design byinterface!

when you need it

Page 133: Automated UI Testing Done Right (QMSDNUG)

do you support multiple devices?

Page 134: Automated UI Testing Done Right (QMSDNUG)

do you doA/B testing?

Page 135: Automated UI Testing Done Right (QMSDNUG)

create interface for your Page Objects and

use the interface in your test scripts

Page 136: Automated UI Testing Done Right (QMSDNUG)

ISomePage

PCPage A/B testing pages iPadPage

Page 137: Automated UI Testing Done Right (QMSDNUG)

… and use one test script for all page variations

Page 138: Automated UI Testing Done Right (QMSDNUG)

apply

YAGNIin your

test code

Page 139: Automated UI Testing Done Right (QMSDNUG)

do

NOTcreate a

Page Object

until you need it

Page 140: Automated UI Testing Done Right (QMSDNUG)

do

NOTadd an action to

Page Object

until you need it

Page 141: Automated UI Testing Done Right (QMSDNUG)

do

NOTadd a property to

Page Object

until you need it

Page 142: Automated UI Testing Done Right (QMSDNUG)

do

NOTadd a getter to your property

until you need it

Page 143: Automated UI Testing Done Right (QMSDNUG)

run and maintain your tests

Page 144: Automated UI Testing Done Right (QMSDNUG)

run your tests

frequently

Page 145: Automated UI Testing Done Right (QMSDNUG)

fix the broken test right

when it breaks

Page 146: Automated UI Testing Done Right (QMSDNUG)

tests you do not run

===broken tests

Page 147: Automated UI Testing Done Right (QMSDNUG)

broken tests you do not fix

===ignored tests

Page 148: Automated UI Testing Done Right (QMSDNUG)

… and finally

Page 149: Automated UI Testing Done Right (QMSDNUG)

Confucius says …

Page 150: Automated UI Testing Done Right (QMSDNUG)

UI Testing is hard

Page 151: Automated UI Testing Done Right (QMSDNUG)

and could be a waste of time

Page 152: Automated UI Testing Done Right (QMSDNUG)

so

do NOT do itor

do it RIGHT

Page 153: Automated UI Testing Done Right (QMSDNUG)

thanks for attending

Page 154: Automated UI Testing Done Right (QMSDNUG)

time for a few Qs & hopefully few As

Blog: www.mehdi-khalili.comTwitter: @MehdiKhalili