Selenium Best Practices: Unlocking Test Automation to...

Preview:

Citation preview

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

A b o u t M e

About Me

• Twitter:@LeoLaskin

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

• Email:leo@saucelabs.com

© Sauce Labs, Inc.3

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

Manual Tests

© Sauce Labs, Inc.5

NoManualTests*

*TestingExtremeProgrammingbyLisaCrispinandTipHouse

Start w i th a C leanSystem

• Havingacleansystemeverytimeisparamountforreproducibleresults

• SaaSgridbasedsystemscanbesetuptobefreshwithnosystemcache,browsercache,cookies,etc.

© Sauce Labs, Inc.6

Para l le l i zat ion

• Serializedteststakemoretimeandcostmoremoney

• UsingSauceLabs,parallelizedtestscanspinupmultiplebrowsersforyouormultipletestssimultaneously

© Sauce Labs, Inc.7

Page Object Pattern

• Encouragereusablecodethroughoutyourtestapp

• Testcodemoreorganizedforupdateiftestparameterslikeselectorschange

• TestsaremorereadablewithanaturalfeelingDomain-SpecificLanguage(DSL)

© Sauce Labs, Inc.8

Test Constant ly

• Don’teverstoptesting

• Havingtestsrunoneverycommitisgoingtoprovidesignificantlyhigherconfidenceinyoursoftware

• Youhaveunlimitedamountsoftimetotest.Useit!

© Sauce Labs, Inc.9

Speed th ings up

• Theaverageattentionspanofanadultislessthen5minutes

• Trytoprovideresultsbeforecontextisswitched

• Makethedevs workforyounotagainstyou

© Sauce Labs, Inc.10

Start Smal l

• Don’trushtoautomateeverytestcase

• Identifytestcaseprioritythenautomate

• Don’tautomateend-to-end

• Divideandconquer

© Sauce Labs, Inc.11

Div ide your test ing up

• ByFunctionalArea

• Byhostedserver

• Bybackendserver

© Sauce Labs, Inc.12

Automat ion= Deve lopment Act iv i ty

• Automationisarealdevelopmentendeavor,treatitassuch

• BewareofCowboyCoding

• Copy/Pastinginsideyourcodecanbereplacedbyreusablemethods

• Sourcecontrolischeapinsurance

• Badcodingpractices=badtestingpractices(andviceversa)

© Sauce Labs, Inc.13

Don’t Forget to Wait !

• Webpagesdon’tloadinstantaneously,andnotallelementsloadatthesametime

• Alotofyourfailuresinfindinganelementwilldisappearifyouusethese

• Explicitorfluentwaitsarepreferredforwaitingforsingleelements

• Implicitwaitsforwaitingallthetime

• Fluentwaitsforelementsthatcantakevariabletimetoload

© Sauce Labs, Inc.14

Avoid Dependenc ies

• Don’tallowteststobedependentoneachother

• Onetest’sactionsshouldnotdriveanothertestsassertioncriteria

© Sauce Labs, Inc.15

PrepYour Env i ronment

• MakesureyourCIrigcanhandlethenumberofjobsrunningonitsimultaneously

• MakesureyourSUTcanhandlethenumberoftestsrunningonitsimultaneously

© Sauce Labs, Inc.16

Col lect Metr ics

• Howlongdidtestrunstakebeforeandafterautomation?

• Howmanybugsdoautomatedtestsidentifyperrelease?

• Howmanyengineersdoesatestrunrequire?

© Sauce Labs, Inc.17

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

Ser ia l Test ing

• Onetestatatime

• Testtimeisequivalenttosumofalltests

© Sauce Labs, Inc.19

Para l le l Test ing

• Runasmanytestsasyouhaveconcurrenttestinstances

• Testtimeisequivalenttolongestrunningtest

© Sauce Labs, Inc.20

Best Pract ices

• Don’ttrytorunparallellocally

• Testinthecloudduetocostefficiency

• Releaseappsfasterwithmoreparalleltests

© Sauce Labs, Inc.21

CommonPara l le l i zat ion too ls

• Java– MavenSurefire

• .Net – ParallelSelenium

• Python– Py.Test

• Ruby– Rake

• Javascript - Grunt

© Sauce Labs, Inc.22

Exerc ise!

• LogintoyourSauceLabs account(www.saucelabs.com)

• Username:firstinitialLastName+seminar

• Password:seminar

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

• Followthereadmetomakeitrun

© Sauce Labs, Inc.23

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

Locators def ined

• LocatorsarehowwetellSeleniumwhichelementwearelookingfor

• Needtobeuniquesothatwecanfindeachelement

• Ifnotunique,findmultipleelementsandparsethearrayforyourappropriateelement

© Sauce Labs, Inc.25

Locators order

• CSSSelector

• ID

• Name

• ClassName

• TagName

• LinkText

• PartialLinkText

• XPath

© Sauce Labs, Inc.26

Why i s Xpath Last?

• XPath hasbeengivenabadnameinthepast• Slowness• Maintainability

• WhenshouldyouuseXpath• Parentelements• Textbasedelements

© Sauce Labs, Inc.27

WhyNot ID as F i rst?

• PositivesofbeingUniqueonpage

• NegativesofbeingUniqueonpage

• Potentiallygeneratedbyyourframework

• Notmuchfasterthenanyotherchoices

© Sauce Labs, Inc.28

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

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

Quick Rev iew

• PageObjectsModel

• Whatisit?

• Whenshouldyoubeusingit?

© Sauce Labs, Inc.31

Naming

• Objectsshouldbeeasilyidentifiedbytheirname

• Considernamingbypage• pageName.objectType.element

• Namethingsotherthensimpleelements

© Sauce Labs, Inc.32

Don’t Use Se len ium

Ifyou’reusingSeleniuminyourtests,you’redoingitwrong.–SimonStewart

• TestsshouldnothaveSeleniumcode

• Callmethods/interfacesthatcallSeleniumcode

• TestersshouldnotwriteSeleniumcode

© Sauce Labs, Inc.33

Before andAf ter

• Avoidcodereuseineverymethod

• Commonsetupstepsshouldmovehere

• Whataresomecommonstepsyoucurrentlytake?

© Sauce Labs, Inc.34

Before andAf ter

• Avoidcodereuseineverymethod

• Commonsetupstepsshouldmovehere

• Whataresomecommonstepsyoucurrentlytake?

© Sauce Labs, Inc.35

S ing le Base Test C lass

• Alltestsshouldextendonebasetestclass

• AllWebDriver instantiationshouldhappenhere

© Sauce Labs, Inc.36

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

Pract ice Informat ion

• git clonetherepoofyourchoice(fromthenextslide)

• FollowReadmestepstorunit

• RaiseyourhandifyouhaveanyquestionsandI’llberightover

© Sauce Labs, Inc.38

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

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

Organizat iona l Sh i f t s Happening

• QualityAssuranceisnowQualityEngineering/DevOps

• SoftwareDevelopersinTest

© Sauce Labs, Inc.41

ThePast

• Codethrownoverthewall

• Minimaltesting

• Manualtesting

© Sauce Labs, Inc.42

TheFuture

• UnitandIntegrationTesting

• SDETactaspartofscrum

• Automationforeverything

© Sauce Labs, Inc.43

Prov ide Va lue

• Managetherelationship

• Avoidgettingresourceconstrained

© Sauce Labs, Inc.44

ToEmbedor Not Embed

• EmbeddingSDETaspartofDevelopmentcanbebeneficial

• AvoidCommonPitfalls• EmbedLocallynotRemotely• Clearmanagementlines

• Getinformed,getclose

© Sauce Labs, Inc.45

Takeaway

• QEnotQA

• Embed

• Goodreporting

© Sauce Labs, Inc.46

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

Big Changes

• SeleniumCoreandRCbeingremoved

• Webdriver W3CSpec

© Sauce Labs, Inc.48

3NewPackages

• Selenium-Java

• Selenium3Server

• Leg-rc

© Sauce Labs, Inc.49

Browser Vendors

• Eachvendorwillowntheirowndriver

• Maintenanceofthatdriverwilltypicallyfalltothevendor

© Sauce Labs, Inc.50

W3CSpec: Part 2

• Browsercoathangers• Geolocation• UserMedia• DesktopNotifications

• ShadowDOMobjects

© Sauce Labs, Inc.51

What ’s not chang ing

• Methodcalls

• Mobile

• AnythingthatispartofWebDriver 2.0(exceptRC)

• SeleniumGrid

© Sauce Labs, Inc.52

When’s i t coming

• Datestillunknown

• Soonerthenwethink

© Sauce Labs, Inc.53

T h a n k Yo u !

About Me

• Twitter:@LeoLaskin

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

• Email:leo@saucelabs.com

© Sauce Labs, Inc.55

test.allTheThings()

Recommended