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