Automated UI Testing Done Right
Mehdi Khalili Senior Developer at Readify Active Open Source Projects: • BDDfy • Seleno • Humanizer Blog: www.mehdi-khalili.com Email: me@mehdi-khalili.com Twitter: @MehdiKhalili
These practices are performed by professional developers and testers. Please DO try this at home Authorized and written by Mehdi Khalili
framework agnostic ideas and patterns
can apply these with any UI and UI Testing framework
… but for this talk we are going to use
Selenium an awesome automated UI testing framework
Selenium http://seleniumhq.org/projects/webdriver/ PM> Install-Package Selenium.WebDriver
BDDfy A simple BDD framework to use and extend! BDDfy turns your traditional unit tests into BDD behaviours
BDDfy http://teststack.github.com/TestStack.BDDfy/ PM> Install-Package TestStack.BDDfy
Seleno helps you write Automated UI Tests the RIGHT way!
Seleno http://teststack.github.com/TestStack.Seleno/ PM> Install-Package TestStack.Seleno
samples are from Seleno codebase and can be found at https://github.com/TestStack/TestStack.Seleno
Agenda UI Testing: a likely failure From horrid to awesome in three steps A few tips Q&A
UI Testing! a likely failure speaking of experience
a lot of teams do UI Testing
a lot of teams have a great start at UI Testing
they then struggle with UI Testing
and a lot of teams then fail at UI Testing
because UI Tests are
because UI Tests are hard to write
because UI Tests are hard to maintain
because UI Tests are brittle
but you can mitigate these issues
If you do it RIGHT
test code is code
apply S.R.P. on your code?
apply S.R.P. on your tests
apply D.R.Y. on your tests
care about your tests as much as you care about your code
or you will waste a lot of time
or you will fail
from horrid to awesome in three steps
a quick look at the sample https://github.com/TestStack/TestStack.Seleno
guaranteed to fail
procedural duplicated logic duplicated selectors magic strings
Step 1: your tests with Page Object
Page Object brings OO to tests
a Page Object per page under test
textbox a on the page becomes a string property on the Page Object
filling a textbox on the page turns into setting a string property on the Page Object
instead of you will get
acheckbox on the page becomes a bool property on the Page Object
ticking a checkbox on the page turns into setting a bool property on the Page Object
a link on the page becomes a method on the Page Object
clicking a link on the page turns into calling a method on the Page Object
instead of you will get
anyaction on the page becomes a method on the Page Object
… and you will get another Page Object as the return value of the method
chain your calls
step 2: Page Components Compose your Page Objects of smaller pieces
some pages are
some pages are complex
remember S.R.P.?
would you write big and complex classes in your code?
do NOT write big and complex Page Objects
use Page Components to break down your Page Objects
use Page Components for panels menus rows in grids modal pop-ups
Page Components D.R.Y. your tests even more
instead of you will get
and you can compose other Page Objects using these Page Components
Page Object & Page Component D.R.Y. your test
Page Object & Page Component lead into S.R.P.
procedural duplicating logic duplicating selectors magic strings
… and the code is still ugly
step 3: Strongly typed Page Object
you use view models in your code
why not use view models in your tests?
(unofficial) step 4: Tests as Living Documentation
how do you get requirements from the business?
asked to work one a new feature or a bug programmer: “can I see the req. for this?”
feeling very proud for generating the most awesome requirements BA: “here you go”
requirements book, anyone!?!
requirements in Word === a lot of wasted time
requirements in Word === out of date requirements
requirements in Word === a lot of confusion
requirements in Word === misinterpretation
requirements in Word === wrong product
BDD to the rescue http://www.mehdi-khalili.com/bdd-to-the-rescue
BDD to the rescue reducing misinterpretation
BDD to the rescue YAGNI
BDD to the rescue early and frequent feedback
BDD to the rescue test suite defined by BAs
BDD to the rescue living documentation
write your UI Tests based on acceptance criteria
use a BDD framework to implement your acceptance criteria
use the test results as a progress report
use the test results to support and reduce manual testing
computers are having fun while we are doing the repetitive tasks they are built to do
A few tips
Do NOT use Thread.Sleep
Thread.Sleep is slow
Thread.Sleep is brittle
often need to wait a bit longer for things to load?
use implicit waits
need to wait longer for specific elements to load?
use explicit waits
need to wait for an AJAX call to finish?
use javascript
choose right selectors
page structure changes
do NOT be fuzzy with your selectors
do NOT rely on the structure of your page
do NOT rely on the surrounding elements
By.XPath("//ul[@id='album-list']/li[3]/a/span") you’re kidding, right?!
we use interfaces and DI all over our code to make it unit testable
do what it takes to support your tests
be explicit: add id on your elements to support your tests
TARGET your elements directly when possible
only one test per action
do you have workflows?
do one test per page/action
then do one test for entire flow
do NOT setup your required state through UI
that will be slow and brittle
setup your data through code
and navigate to the page under test directly
use strongly typed actions for that
design by interface! when you need it
do you support multiple devices?
do you do A/B testing?
create interface for your Page Objects and use the interface in your test scripts
ISomePage A/B testing PCPage iPadPage pages
… and use one test script for all page variations
apply YAGNI in your test code
do NOT create a Page Object until you need it
do NOT add an action to Page Object until you need it
do NOT add a property to Page Object until you need it
do NOT add a getter to your property until you need it
run and maintain your tests
run your tests frequently
fix the broken test right when it breaks
tests you do not run === broken tests
broken tests you do not fix === ignored tests
… and finally
Confucius says …
UI Testing is hard
and could be a waste of time
so do NOT do it or do it RIGHT
thanks for attending
time for a few Qs & hopefully few As Blog: www.mehdi-khalili.com Twitter: @MehdiKhalili

Automated UI Testing Done Right (QMSDNUG)

Editor's Notes

  • #9 The framework is called BDDfy because it BDDfies (as in turns into BDD) your otherwise traditional unit tests
  • #12 The framework is called BDDfy because it BDDfies (as in turns into BDD) your otherwise traditional unit tests
  • #55 Some code here + demoWhy it is different from the previous approachBut changing a name still breaks the tests
  • #72 Show a demo of a change on an id that breaks a test
  • #81 Showing the demo with Page<T>Showing the differenceShowing that changing a property name gives compile time error
  • #121 UI Tests are no different
  • #123 Add a photo of an element being targetted