Writing Automation Tests

38
WRITING AUTOMATION TESTS Shauvik Roy Choudhary Instructor, Georgia Tech [email protected]

Transcript of Writing Automation Tests

Page 1: Writing Automation Tests

WRITING AUTOMATION TESTSShauvik Roy Choudhary

Instructor, Georgia Tech [email protected]

Page 2: Writing Automation Tests

WHY AUTOMATE TESTS?

Software

Manual Testing

Page 3: Writing Automation Tests

PLATFORMS

Web Tester

Page 4: Writing Automation Tests

Mobile Tester

LollipopAndroid 5.0

4

Page 5: Writing Automation Tests

HOW TO AUTOMATE?

HOW

Infrastructure

Tool Support

WHAT

Parts of your App

Page 6: Writing Automation Tests

INFRASTRUCTURE

• Unit TestsxUnit for different programming languages

• UI TestsSelenium/WebDriver for Web Apps Appium for Mobile (iOS, Android)Espresso for Android

Page 7: Writing Automation Tests

IDEAL TESTING PYRAMIDby Mike Cohn

Page 8: Writing Automation Tests

ICE-CREAM CONE

Page 9: Writing Automation Tests

HOUR GLASS

source: just-about.net

Page 10: Writing Automation Tests

TESTING DIAMOND

Source: toddlittleweb.com

Page 11: Writing Automation Tests

CHRISTMAS TREE

source: just-about.net

Page 12: Writing Automation Tests

UI TESTS CLOSEST TO

END-USER TESTING

Page 13: Writing Automation Tests

UI TESTING ISSUES

• FRAGILITY

• SPEED

• FLAKINESS

Page 14: Writing Automation Tests

1. FRAGILE TESTS

Page 15: Writing Automation Tests

REDUCING FRAGILITY

• Use durable selectors

• IDs over XPath

• Relative XPaths over Absolute XPaths

• Other durable labels (class, content-desc..)

Page 16: Writing Automation Tests

2. SLOW TESTS

Page 17: Writing Automation Tests

MAKING TESTS FAST

• Don’t use Sleeps

• If absolutely necessary, sleep while polling

• Replace slow resources with stubs

Page 18: Writing Automation Tests

3. FLAKY TESTS

Image credits: Google, GTAC talk

Page 19: Writing Automation Tests

doSomething()Thread.sleep(1000) checkSomething()

Page 20: Writing Automation Tests

doSomething()do{ Thread.sleep(1000) }while(!loaded) checkSomething()

Page 21: Writing Automation Tests

FLAKINESSUI Thread

Test Thread

Work in Queue

click

Motion down

Motion up

assert

Page 22: Writing Automation Tests

FLAKINESSUI Thread

Test Thread

Work in Queue

click

Motion down

Motion up

assertsleep

Page 23: Writing Automation Tests

FLAKINESSUI Thread

Test Thread

Work in Queue

click

Motion down

Motion up

assertsleep

Page 24: Writing Automation Tests

TESTS

Page 25: Writing Automation Tests

REDUCING FLAKINESS

• Have a hermetic test environment

Source: googletesting.blogspot.com

Page 26: Writing Automation Tests

ANDROID UI TESTING

Page 27: Writing Automation Tests

ANDROID TESTING TOOLS

Image credits: BitBar

Page 28: Writing Automation Tests

ESPRESSO: HOW IT WORKSUI Thread

Test Thread

Work in Queue

Test Case

Motion down

Motion upEspresso

action assert

Blocked

Page 29: Writing Automation Tests

ESPRESSO METHODS

onView(Matcher) .perform(ViewAction) .check(ViewAssertion)

Page 30: Writing Automation Tests

EXAMPLES

onView(withId(R.id.greeting)) .perform(click());

onView(withText("Hello Steve!")) .check(matches(isDisplayed()));

Find

Do Stuff

Check

Page 31: Writing Automation Tests

onView(Matcher<View>)

●  withId ●  withText ●  withContentDescription ●  isDisplayed ●  hasFocus ●  hasSibling ●  ... ●  mySpecialMatcher

Page 32: Writing Automation Tests

perform(ViewAction)

●  click ●  longClick ●  doubleClick ●  typeText ●  scrollTo ●  ... ●  myCustomAction

Page 33: Writing Automation Tests

check(ViewAssertion)

●  matches(...) ●  doesNotExist ●  myCustomAssertion

Page 34: Writing Automation Tests

HIERARCHYwithParent(Matcher)withChild(Matcher)hasDescendant(Matcher)isDescendantOfA(Matcher)hasSibling(Matcher)isRoot()

UI PROPERTIESisDisplayed()isCompletelyDisplayed()isEnabled()hasFocus()isClickable()isChecked()isNotChecked()withEffectiveVisibility(…)isSelected()

ROOT MATCHERSisFocusable()isTouchable()isDialog()withDecorView(…)isPlatformPopup()

COMMON HAMCRESTMATCHERSallOf(Matchers)anyOf(Matchers)is(...)not(...)endsWith(String)startsWith(String)

SEE ALSOPreference matchersCursor matchers

USER PROPERTIESwithId(…)withText(…)withTagKey(…)withTagValue(…)hasContentDescription(…)withContentDescription(…)withHint(…)withSpinnerText(…)hasLinks()hasEllipsizedText()hasMultilineText()

INPUTsupportsInputMethods(…)hasImeAction(…)

CLASSisAssignableFrom(…)withClassName(…)

MatchersCLICK/PRESSclick()doubleClick()longClick()pressBack()pressImeActionButton()pressKey([int/EspressoKey])pressMenuKey()closeSoftKeyboard()openLink(…)

GESTURESscrollTo()swipeLeft()swipeRight()swipeUp()swipeDown()

TEXTclearText()typeText(String)typeTextIntoFocusedView(String)replaceText(String)

POSITION ASSERTIONSisLeftOf(Matcher)isRightOf(Matcher)isLeftAlignedWith(Matcher)isRightAlignedWith(Matcher)isAbove(Matcher)isBelow(Matcher)isBottomAlignedWith(Matcher)isTopAlignedWith(Matcher)

LAYOUT ASSERTIONSnoEllipsizedText(Matcher)noMultilineButtons()noOverlaps([Matcher])

View Actions

matches(Matcher)doesNotExist()selectedDescendantsMatch(…)

View Assertions

onView(Matcher) .perform(ViewAction) .check(ViewAssertion)

CHEAT SHEET

2.0

Page 35: Writing Automation Tests

BARISTAEspresso Test Generation

http://checkdroid.com/barista

Page 36: Writing Automation Tests

Tests

Page 37: Writing Automation Tests

TEST CLOUD

Page 38: Writing Automation Tests

THANKS!