+ All Categories
Home > Documents > Selenium Best Practices: Unlocking Test Automation to...

Selenium Best Practices: Unlocking Test Automation to...

Date post: 09-Mar-2018
Category:
Upload: duongdieu
View: 224 times
Download: 2 times
Share this document with a friend
56
Selenium Best Practices: Unlocking Test Automation to Achieve True Agility Leo Laskin, Test Automation Advisor
Transcript
Page 1: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Se l en i um Be s t P ra c t i c e s : Un l o c k i n g Te s t Au tomat i on t o A ch i e ve Tr ue Ag i l i t yL e o L a s k i n , Te s t A u t o m a t i o n A d v i s o r

Page 2: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

A b o u t M e

Page 3: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

About Me

• Twitter:@LeoLaskin

• LinkedIn:https://www.linkedin.com/in/llaskin

• Email:[email protected]

© Sauce Labs, Inc.3

Page 4: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

AUTOMATIONBEST PRACTICESL e o L a s k i n , Te s t A u t o m a t i o n S p e c i a l i s t

Page 5: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Manual Tests

© Sauce Labs, Inc.5

NoManualTests*

*TestingExtremeProgrammingbyLisaCrispinandTipHouse

Page 6: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Start w i th a C leanSystem

• Havingacleansystemeverytimeisparamountforreproducibleresults

• SaaSgridbasedsystemscanbesetuptobefreshwithnosystemcache,browsercache,cookies,etc.

© Sauce Labs, Inc.6

Page 7: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Para l le l i zat ion

• Serializedteststakemoretimeandcostmoremoney

• UsingSauceLabs,parallelizedtestscanspinupmultiplebrowsersforyouormultipletestssimultaneously

© Sauce Labs, Inc.7

Page 8: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Page Object Pattern

• Encouragereusablecodethroughoutyourtestapp

• Testcodemoreorganizedforupdateiftestparameterslikeselectorschange

• TestsaremorereadablewithanaturalfeelingDomain-SpecificLanguage(DSL)

© Sauce Labs, Inc.8

Page 9: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Test Constant ly

• Don’teverstoptesting

• Havingtestsrunoneverycommitisgoingtoprovidesignificantlyhigherconfidenceinyoursoftware

• Youhaveunlimitedamountsoftimetotest.Useit!

© Sauce Labs, Inc.9

Page 10: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Speed th ings up

• Theaverageattentionspanofanadultislessthen5minutes

• Trytoprovideresultsbeforecontextisswitched

• Makethedevs workforyounotagainstyou

© Sauce Labs, Inc.10

Page 11: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Start Smal l

• Don’trushtoautomateeverytestcase

• Identifytestcaseprioritythenautomate

• Don’tautomateend-to-end

• Divideandconquer

© Sauce Labs, Inc.11

Page 12: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Div ide your test ing up

• ByFunctionalArea

• Byhostedserver

• Bybackendserver

© Sauce Labs, Inc.12

Page 13: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Automat ion= Deve lopment Act iv i ty

• Automationisarealdevelopmentendeavor,treatitassuch

• BewareofCowboyCoding

• Copy/Pastinginsideyourcodecanbereplacedbyreusablemethods

• Sourcecontrolischeapinsurance

• Badcodingpractices=badtestingpractices(andviceversa)

© Sauce Labs, Inc.13

Page 14: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Don’t Forget to Wait !

• Webpagesdon’tloadinstantaneously,andnotallelementsloadatthesametime

• Alotofyourfailuresinfindinganelementwilldisappearifyouusethese

• Explicitorfluentwaitsarepreferredforwaitingforsingleelements

• Implicitwaitsforwaitingallthetime

• Fluentwaitsforelementsthatcantakevariabletimetoload

© Sauce Labs, Inc.14

Page 15: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Avoid Dependenc ies

• Don’tallowteststobedependentoneachother

• Onetest’sactionsshouldnotdriveanothertestsassertioncriteria

© Sauce Labs, Inc.15

Page 16: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

PrepYour Env i ronment

• MakesureyourCIrigcanhandlethenumberofjobsrunningonitsimultaneously

• MakesureyourSUTcanhandlethenumberoftestsrunningonitsimultaneously

© Sauce Labs, Inc.16

Page 17: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Col lect Metr ics

• Howlongdidtestrunstakebeforeandafterautomation?

• Howmanybugsdoautomatedtestsidentifyperrelease?

• Howmanyengineersdoesatestrunrequire?

© Sauce Labs, Inc.17

Page 18: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Para l le l i zat ionL e o L a s k i n , Te s t A u t o m a t i o n S p e c i a l i s t

Page 19: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Ser ia l Test ing

• Onetestatatime

• Testtimeisequivalenttosumofalltests

© Sauce Labs, Inc.19

Page 20: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Para l le l Test ing

• Runasmanytestsasyouhaveconcurrenttestinstances

• Testtimeisequivalenttolongestrunningtest

© Sauce Labs, Inc.20

Page 21: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Best Pract ices

• Don’ttrytorunparallellocally

• Testinthecloudduetocostefficiency

• Releaseappsfasterwithmoreparalleltests

© Sauce Labs, Inc.21

Page 22: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

CommonPara l le l i zat ion too ls

• Java– MavenSurefire

• .Net – ParallelSelenium

• Python– Py.Test

• Ruby– Rake

• Javascript - Grunt

© Sauce Labs, Inc.22

Page 23: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Exerc ise!

• LogintoyourSauceLabs account(www.saucelabs.com)

• Username:firstinitialLastName+seminar

• Password:seminar

• Downloadyourframeworkofchoicefromhttps://github.com/saucelabs-sample-test-frameworks

• Followthereadmetomakeitrun

© Sauce Labs, Inc.23

Page 24: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

LocatorsL e o L a s k i n , Te s t A u t o m a t i o n S p e c i a l i s t

Page 25: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Locators def ined

• LocatorsarehowwetellSeleniumwhichelementwearelookingfor

• Needtobeuniquesothatwecanfindeachelement

• Ifnotunique,findmultipleelementsandparsethearrayforyourappropriateelement

© Sauce Labs, Inc.25

Page 26: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Locators order

• CSSSelector

• ID

• Name

• ClassName

• TagName

• LinkText

• PartialLinkText

• XPath

© Sauce Labs, Inc.26

Page 27: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Why i s Xpath Last?

• XPath hasbeengivenabadnameinthepast• Slowness• Maintainability

• WhenshouldyouuseXpath• Parentelements• Textbasedelements

© Sauce Labs, Inc.27

Page 28: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

WhyNot ID as F i rst?

• PositivesofbeingUniqueonpage

• NegativesofbeingUniqueonpage

• Potentiallygeneratedbyyourframework

• Notmuchfasterthenanyotherchoices

© Sauce Labs, Inc.28

Page 29: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Why I Prefer CSS Se lectors

• JackofallTrades

• Maintainable

• Easytofollow

• Easytowrite

https://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-16048

© Sauce Labs, Inc.29

Page 30: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

PageObjectsL e o L a s k i n , Te s t A u t o m a t i o n S p e c i a l i s t

Page 31: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Quick Rev iew

• PageObjectsModel

• Whatisit?

• Whenshouldyoubeusingit?

© Sauce Labs, Inc.31

Page 32: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Naming

• Objectsshouldbeeasilyidentifiedbytheirname

• Considernamingbypage• pageName.objectType.element

• Namethingsotherthensimpleelements

© Sauce Labs, Inc.32

Page 33: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Don’t Use Se len ium

Ifyou’reusingSeleniuminyourtests,you’redoingitwrong.–SimonStewart

• TestsshouldnothaveSeleniumcode

• Callmethods/interfacesthatcallSeleniumcode

• TestersshouldnotwriteSeleniumcode

© Sauce Labs, Inc.33

Page 34: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Before andAf ter

• Avoidcodereuseineverymethod

• Commonsetupstepsshouldmovehere

• Whataresomecommonstepsyoucurrentlytake?

© Sauce Labs, Inc.34

Page 35: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Before andAf ter

• Avoidcodereuseineverymethod

• Commonsetupstepsshouldmovehere

• Whataresomecommonstepsyoucurrentlytake?

© Sauce Labs, Inc.35

Page 36: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

S ing le Base Test C lass

• Alltestsshouldextendonebasetestclass

• AllWebDriver instantiationshouldhappenhere

© Sauce Labs, Inc.36

Page 37: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

L e t ’s P r a c t i c e

Page 38: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Pract ice Informat ion

• git clonetherepoofyourchoice(fromthenextslide)

• FollowReadmestepstorunit

• RaiseyourhandifyouhaveanyquestionsandI’llberightover

© Sauce Labs, Inc.38

Page 39: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Pract ice URL

• https://github.com/saucelabs-sample-test-frameworks/• Java:Java-Junit-Selenium• .Net:CSharp-Nunit-Selenium• Ruby:Ruby-Cucumber-Selenium• Python:Python-Pytest-Selenium• JS-Mocha-WebdriverJS

• Username:firstname+seminar orPassword:seminar

© Sauce Labs, Inc.39

Page 40: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Gett ing Deve lopers andQAWork ing TogetherL e o L a s k i n , Te s t A u t o m a t i o n S p e c i a l i s t

Page 41: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Organizat iona l Sh i f t s Happening

• QualityAssuranceisnowQualityEngineering/DevOps

• SoftwareDevelopersinTest

© Sauce Labs, Inc.41

Page 42: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

ThePast

• Codethrownoverthewall

• Minimaltesting

• Manualtesting

© Sauce Labs, Inc.42

Page 43: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

TheFuture

• UnitandIntegrationTesting

• SDETactaspartofscrum

• Automationforeverything

© Sauce Labs, Inc.43

Page 44: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Prov ide Va lue

• Managetherelationship

• Avoidgettingresourceconstrained

© Sauce Labs, Inc.44

Page 45: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

ToEmbedor Not Embed

• EmbeddingSDETaspartofDevelopmentcanbebeneficial

• AvoidCommonPitfalls• EmbedLocallynotRemotely• Clearmanagementlines

• Getinformed,getclose

© Sauce Labs, Inc.45

Page 46: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Takeaway

• QEnotQA

• Embed

• Goodreporting

© Sauce Labs, Inc.46

Page 47: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Se len ium3.0 UpdateL e o L a s k i n , Te s t A u t o m a t i o n S p e c i a l i s t

Page 48: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Big Changes

• SeleniumCoreandRCbeingremoved

• Webdriver W3CSpec

© Sauce Labs, Inc.48

Page 49: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

3NewPackages

• Selenium-Java

• Selenium3Server

• Leg-rc

© Sauce Labs, Inc.49

Page 50: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

Browser Vendors

• Eachvendorwillowntheirowndriver

• Maintenanceofthatdriverwilltypicallyfalltothevendor

© Sauce Labs, Inc.50

Page 51: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

W3CSpec: Part 2

• Browsercoathangers• Geolocation• UserMedia• DesktopNotifications

• ShadowDOMobjects

© Sauce Labs, Inc.51

Page 52: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

What ’s not chang ing

• Methodcalls

• Mobile

• AnythingthatispartofWebDriver 2.0(exceptRC)

• SeleniumGrid

© Sauce Labs, Inc.52

Page 53: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

When’s i t coming

• Datestillunknown

• Soonerthenwethink

© Sauce Labs, Inc.53

Page 54: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

T h a n k Yo u !

Page 55: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

About Me

• Twitter:@LeoLaskin

• LinkedIn:https://www.linkedin.com/in/llaskin

• Email:[email protected]

© Sauce Labs, Inc.55

Page 56: Selenium Best Practices: Unlocking Test Automation to ...fall2016.stpcon.com/wp-content/uploads/2016/09/Laskin-Workshop... · Selenium Best Practices: Unlocking Test Automation ...

test.allTheThings()


Recommended