SlideShare a Scribd company logo
 
	
  
	
  
	
  
	
  
T9	
  
Test	
  Automation	
  
10/5/17	
  11:15	
  
	
  
	
  
	
  
	
  
Say	
  Goodbye	
  to	
  Flaky	
  Selenium	
  Tests	
  
	
  
Presented	
  by:	
  
	
  
Craig	
  Schwarzwald	
  
	
  Vanguard	
  
	
  
Brought	
  to	
  you	
  by:	
  	
  
	
  	
  
	
  
	
  
	
  
	
  
	
  
350	
  Corporate	
  Way,	
  Suite	
  400,	
  Orange	
  Park,	
  FL	
  32073	
  	
  
888-­‐-­‐-­‐268-­‐-­‐-­‐8770	
  ·∙·∙	
  904-­‐-­‐-­‐278-­‐-­‐-­‐0524	
  -­‐	
  info@techwell.com	
  -­‐	
  http://www.starwest.techwell.com/	
  	
  	
  
	
  
	
  	
  
	
  
	
  
Craig	
  Schwarzwald	
  
Vanguard	
  
	
  
Craig	
  Schwarzwald	
  has	
  more	
  than	
  a	
  decade	
  of	
  professional	
  scripting	
  and	
  
automation	
  experience.	
  For	
  the	
  past	
  six	
  years	
  he	
  has	
  focused	
  on	
  creating	
  and	
  
maintaining	
  the	
  Selenium	
  framework	
  used	
  by	
  hundreds	
  of	
  testers,	
  developers,	
  
and	
  automation	
  engineers	
  at	
  a	
  large	
  financial	
  organization.	
  Widely	
  regarded	
  as	
  
his	
  company’s	
  Selenium	
  expert,	
  Craig	
  holds	
  weekly	
  ‰ÛÏoffice	
  hours‰Û	
  
sessions	
  to	
  supply	
  solutions	
  to	
  teams’	
  most	
  difficult	
  Selenium-­‐based	
  challenges.	
  
He	
  teaches	
  a	
  two-­‐day	
  Selenium	
  course	
  to	
  new	
  automation	
  engineers	
  looking	
  to	
  
learn	
  Selenium.	
  In	
  his	
  spare	
  time,	
  Craig	
  enjoys	
  bowling,	
  playing	
  softball,	
  and	
  
having	
  passionate	
  discussions	
  about	
  Selenium,	
  test	
  automation,	
  and	
  any	
  other	
  
Shift	
  Left	
  related	
  topics.	
  Follow	
  Craig	
  on	
  Twitter	
  @AutomationCraig.	
  
	
  
1
Say Goodbye to
Flaky Selenium Tests
Craig Schwarzwald | Vanguard
Craig_Schwarzwald@vanguard.com @AutomationCraig
About Craig Schwarzwald
▪  Worked for Vanguard over 10 years
▪  Many different roles:
▪  System Tester, UI Developer, Mobile Developer, Automation Engineer
▪  Always focused on Automated Testing
▪  2007-2010: QTP
▪  2010-Present: Selenium
▪  Contributor to OCPSoft Blog
▪  Majority of the contents in this presentation can be found in my blog article:
http://www.ocpsoft.org/opensource/flakiness-of-corporate-selenium-suites-and-how-to-get-rid-of-it/
2
Agenda
Goal: Throughout The Entire Enterprise
▪ Stability
▪ Maintainability
▪ Readability
3
Stability
Wait Strategies
1) Hard wait
2) Implicit Wait
3) Explicit Wait
4
Hard Wait
Ex:	
  Thread.sleep(3000);	
  
	
  
	
  
	
  
Implicit Wait
Ex:	
  driver.manage().9meouts.implicitWait(3,	
  TimeUnit.SECONDS);	
  
	
  
	
  
	
  
5
Explicit Wait
Ex:	
  
WebDriverWait	
  wait	
  =	
  new	
  WebDriverWait(driver,	
  3);	
  
Wait.un9l(ExpectedCondi9ons.elementToBeClickable(locator);	
  
	
  
	
  
Maintainability
6
Issue: Poor Maintainability
Solution:
1) Good locators
2) Page Objects
What are good locators?
1) Unique
2) Descriptive
3) Unlikely to change
7
Issue: Bad Locators
driver.findElement(By.xpath(“//div[34]/span[21]”	
  +	
  	
  
	
  “/table/tbody/tr[17]/td[2]/input”)).click();	
  
	
  
Solution: Use better ones
By	
  con9nueBtn	
  =	
  By.id(“con9nueBuWon”);	
  
click(con9nueBtn);	
  
By	
  con9nueBtn	
  =	
  By.xpath(“//input[value=‘Con9nue’]”);	
  
click(con9nueBtn);	
  
or	
  if	
  there	
  is	
  no	
  ID:	
  
Solution: Page Objects
Construc9on:	
  
	
  -­‐	
  By	
  locators	
  pertaining	
  to	
  page/sec9on	
  on	
  top	
  
	
  	
  
	
  
	
  
	
  
	
  -­‐	
  Constructor	
  
	
  
	
  
	
  
	
  
	
  -­‐	
  isLoaded()	
  method	
  
	
  
	
  
	
  
	
  
	
  -­‐	
  Finally	
  create	
  all	
  user	
  ac9on	
  methods	
  specific	
  to	
  that	
  page.	
  
8
Base Page Object
▪ Wraps Selenium core functionality
▪ findElement(),	
  click(),	
  isDisplayed(),	
  visit(),	
  etc.
▪ Also	
  add	
  common	
  func9ons	
  to	
  be	
  used	
  across	
  teams	
  
▪ getCellFromTableContainingText(byTable,	
  text);	
  
	
  
	
  
▪ selectWizzyWidget(value);
Framework Base Page
Corporate Base Page
Readability
9
Rules to Follow
Page Objects
▪  Contain all locators
▪  Contain methods for any actions you can do on
the corresponding page / section of page.
Rules to Follow
Tests
▪  Perform assertion(s)
▪  Call page object methods to progress the tests.
▪  (Assign variables to make tests more readable)
10
Rules to Follow – The DON’Ts
Page Objects DON’T:
▪  Make any assertions
Tests DON’T:
▪  Refer to locators anywhere within the test
NIETHER Page Objects NOR Tests:
▪  Call Selenium commands directly
BONUS
11
Base Page Object Benefits
▪ Encapsulate all Selenium commands
▪ All	
  pages	
  get	
  auto-­‐enabled	
  func9onality	
  
▪ One	
  single	
  place	
  to	
  implement	
  all	
  the	
  null	
  checks,	
  
failsafe’s,	
  and	
  logging.	
  
▪ No	
  more	
  NoSuchElementExcep9ons	
  or	
  
StaleElementReferenceExcep9ons.
Let’s look into the code!
12
Solution: Framework Code
Solution: Framework Code
13
Solution: Framework Code
Solution: Corporate Override Example
14
Solution: Framework Code
“Real World” Corporate
Example
15
Corporate Example – Shopping Home (1 of 2)
Corporate Example – Shopping Home (2 of 2)
16
Corporate Example – Buy Item
Corporate Example – Confirm Page
17
Framework Code – Selenium Base Test
Corporate Example – Selenium Test
18
Summary
Summary – Achieve all 3 pillars 


across our entire Enterprise
▪  Stability (Explicit Waits + Verifying Page Loads)
▪  Maintainability (Page Objects + Good Locators)
▪  Readability (Following Page Object and Test Rules)
▪  More details can be found on the blog post this talk was based on:
http://www.ocpsoft.org/opensource/flakiness-of-corporate-selenium-suites-and-how-to-get-rid-of-it/
Craig_Schwarzwald@vanguard.com @AutomationCraig

More Related Content

Say Goodbye to Flaky Selenium Tests

  • 1.           T9   Test  Automation   10/5/17  11:15           Say  Goodbye  to  Flaky  Selenium  Tests     Presented  by:     Craig  Schwarzwald    Vanguard     Brought  to  you  by:                   350  Corporate  Way,  Suite  400,  Orange  Park,  FL  32073     888-­‐-­‐-­‐268-­‐-­‐-­‐8770  ·∙·∙  904-­‐-­‐-­‐278-­‐-­‐-­‐0524  -­‐  info@techwell.com  -­‐  http://www.starwest.techwell.com/                
  • 2. Craig  Schwarzwald   Vanguard     Craig  Schwarzwald  has  more  than  a  decade  of  professional  scripting  and   automation  experience.  For  the  past  six  years  he  has  focused  on  creating  and   maintaining  the  Selenium  framework  used  by  hundreds  of  testers,  developers,   and  automation  engineers  at  a  large  financial  organization.  Widely  regarded  as   his  company’s  Selenium  expert,  Craig  holds  weekly  ‰ÛÏoffice  hours‰Û   sessions  to  supply  solutions  to  teams’  most  difficult  Selenium-­‐based  challenges.   He  teaches  a  two-­‐day  Selenium  course  to  new  automation  engineers  looking  to   learn  Selenium.  In  his  spare  time,  Craig  enjoys  bowling,  playing  softball,  and   having  passionate  discussions  about  Selenium,  test  automation,  and  any  other   Shift  Left  related  topics.  Follow  Craig  on  Twitter  @AutomationCraig.    
  • 3. 1 Say Goodbye to Flaky Selenium Tests Craig Schwarzwald | Vanguard Craig_Schwarzwald@vanguard.com @AutomationCraig About Craig Schwarzwald ▪  Worked for Vanguard over 10 years ▪  Many different roles: ▪  System Tester, UI Developer, Mobile Developer, Automation Engineer ▪  Always focused on Automated Testing ▪  2007-2010: QTP ▪  2010-Present: Selenium ▪  Contributor to OCPSoft Blog ▪  Majority of the contents in this presentation can be found in my blog article: http://www.ocpsoft.org/opensource/flakiness-of-corporate-selenium-suites-and-how-to-get-rid-of-it/
  • 4. 2 Agenda Goal: Throughout The Entire Enterprise ▪ Stability ▪ Maintainability ▪ Readability
  • 5. 3 Stability Wait Strategies 1) Hard wait 2) Implicit Wait 3) Explicit Wait
  • 6. 4 Hard Wait Ex:  Thread.sleep(3000);         Implicit Wait Ex:  driver.manage().9meouts.implicitWait(3,  TimeUnit.SECONDS);   ��    
  • 7. 5 Explicit Wait Ex:   WebDriverWait  wait  =  new  WebDriverWait(driver,  3);   Wait.un9l(ExpectedCondi9ons.elementToBeClickable(locator);       Maintainability
  • 8. 6 Issue: Poor Maintainability Solution: 1) Good locators 2) Page Objects What are good locators? 1) Unique 2) Descriptive 3) Unlikely to change
  • 9. 7 Issue: Bad Locators driver.findElement(By.xpath(“//div[34]/span[21]”  +      “/table/tbody/tr[17]/td[2]/input”)).click();     Solution: Use better ones By  con9nueBtn  =  By.id(“con9nueBuWon”);   click(con9nueBtn);   By  con9nueBtn  =  By.xpath(“//input[value=‘Con9nue’]”);   click(con9nueBtn);   or  if  there  is  no  ID:   Solution: Page Objects Construc9on:    -­‐  By  locators  pertaining  to  page/sec9on  on  top              -­‐  Constructor            -­‐  isLoaded()  method            -­‐  Finally  create  all  user  ac9on  methods  specific  to  that  page.  
  • 10. 8 Base Page Object ▪ Wraps Selenium core functionality ▪ findElement(),  click(),  isDisplayed(),  visit(),  etc. ▪ Also  add  common  func9ons  to  be  used  across  teams   ▪ getCellFromTableContainingText(byTable,  text);       ▪ selectWizzyWidget(value); Framework Base Page Corporate Base Page Readability
  • 11. 9 Rules to Follow Page Objects ▪  Contain all locators ▪  Contain methods for any actions you can do on the corresponding page / section of page. Rules to Follow Tests ▪  Perform assertion(s) ▪  Call page object methods to progress the tests. ▪  (Assign variables to make tests more readable)
  • 12. 10 Rules to Follow – The DON’Ts Page Objects DON’T: ▪  Make any assertions Tests DON’T: ▪  Refer to locators anywhere within the test NIETHER Page Objects NOR Tests: ▪  Call Selenium commands directly BONUS
  • 13. 11 Base Page Object Benefits ▪ Encapsulate all Selenium commands ▪ All  pages  get  auto-­‐enabled  func9onality   ▪ One  single  place  to  implement  all  the  null  checks,   failsafe’s,  and  logging.   ▪ No  more  NoSuchElementExcep9ons  or   StaleElementReferenceExcep9ons. Let’s look into the code!
  • 15. 13 Solution: Framework Code Solution: Corporate Override Example
  • 16. 14 Solution: Framework Code “Real World” Corporate Example
  • 17. 15 Corporate Example – Shopping Home (1 of 2) Corporate Example – Shopping Home (2 of 2)
  • 18. 16 Corporate Example – Buy Item Corporate Example – Confirm Page
  • 19. 17 Framework Code – Selenium Base Test Corporate Example – Selenium Test
  • 20. 18 Summary Summary – Achieve all 3 pillars 
 across our entire Enterprise ▪  Stability (Explicit Waits + Verifying Page Loads) ▪  Maintainability (Page Objects + Good Locators) ▪  Readability (Following Page Object and Test Rules) ▪  More details can be found on the blog post this talk was based on: http://www.ocpsoft.org/opensource/flakiness-of-corporate-selenium-suites-and-how-to-get-rid-of-it/ Craig_Schwarzwald@vanguard.com @AutomationCraig