Espresso workshop

37
Getting High on Espresso!

Transcript of Espresso workshop

Page 1: Espresso workshop

Getting High on Espresso!

Page 2: Espresso workshop

• Why do we automate?

Page 3: Espresso workshop

• Do you trust your tests 100%?

Page 4: Espresso workshop
Page 5: Espresso workshop
Page 6: Espresso workshop

• Synchronization

Why are they flaky?

Page 7: Espresso workshop

Why Flakiness?

UI Thread

Motion down

Test Thread Click

Motion Up

AssertSleep Assert

Page 8: Espresso workshop

ANDROID INSTRUMENTATION FRAMEWORK

Android Test Automation Frameworks

UI Automator

Robotium

AppiumCalabash

JUNIT

Page 9: Espresso workshop

Simple & Easy

•Developers/QAs avoid installing heavy tools•Doesn’t want to learn new language just for testing stuff.•Make it easy for developers and QA so that they do it

Page 10: Espresso workshop

Reliable

• do{ Thread.sleep(5000)} while (!loaded) assert(“Let me have a coffee until it loads”)

• Synchronizing multiple threads

Page 11: Espresso workshop

Durable

• Antonym of fragile• Test should not break if String of any

resource has changed• Refer the elements by resource id rather

than content description

Page 12: Espresso workshop

ANDROID INSTRUMENTATION FRAMEWORK

Android Test Automation Frameworks

UI Automator

Robotium

AppiumCalabash

JUNIT

Page 13: Espresso workshop

How Espresso Works?

Main Thread

Motion down

Test /Instrumentatio

n Thread

Motion Up

Test Case

Some work in queue Espresso

AssertActionBlocked

Page 14: Espresso workshop

Enough of talk! Let’s CodeClone the repository [email protected]:ketansoni/android-booksearch-demo.git

Page 15: Espresso workshop

Automation scenario

1. Launch app2. Tap on search icon3. Search using keyword “Android Testing”4. Count the number of items returned5. View the item details6. Share the item and verify it is shared

Page 16: Espresso workshop

How to install?• Add following in your build.gradle of your app and sync it

dependencies {//Testing dependenciesandroidTestCompile 'com.android.support.test:runner:0.5'androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2’

}

• Set the Instrument Runner in android.defaultconfig

android {defaultConfig {testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner”

}

Page 17: Espresso workshop

Settings

• On your device, under Settings->Developer options disable the following 3 settings:o Window animation scaleo Transition animation scaleo Animator duration scale

Page 18: Espresso workshop

Launch app• In your app/src/androidTest/java/com.codepath.android.booksearch

@RunWith(AndroidJUnit4.class)public class ApplicationTest {

@Rule public ActivityTestRule<BookListActivity> mActivityRule = new ActivityTestRule(BookListActivity.class);

@Test public void test() { }}

Page 19: Espresso workshop

Syntax

• onView(Matcher). perform(ViewActions) . check(ViewAssertions)

Page 20: Espresso workshop

Tap on search Icon

• onView(withId(R.id.action_search)).perform(click());

Page 21: Espresso workshop

Search using keyword “Android Testing”

• onView(withId(R.id.search_src_text)) .perform(typeText("Android Testing"), pressKey(KeyEvent.KEYCODE_ENTER));

Page 22: Espresso workshop

Count the number of items returned

public static int getListViewCount(final int id) { final int[] counts = new int[1]; onView(withId(id)).check(matches(new TypeSafeMatcher<View>() { @Override public boolean matchesSafely(View view) { ListView listView = (ListView) view; counts[0] = listView.getCount(); return true; }

@Override public void describeTo(Description description) { } }));

return counts[0]; }

assertThat("No. of reminders are: ", getListViewCount(R.id.lvBooks), CoreMatchers.equalTo(1));

Page 23: Espresso workshop

View the item details

onView(withText("Android application testing guide")) .perform(click());

Page 24: Espresso workshop

What is intent?• Intents facilitate communication between

components called Activities

• Types– Explicit– Implicit

Page 25: Espresso workshop

Explicit Intent• E.g. start a service to download a file in the

background

Page 26: Espresso workshop

Implicit Intent• E.g. you want to share a photo

Page 27: Espresso workshop

Share the item and verify intent

Add dependenciesandroidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

//IntentsRulepublic IntentsTestRule<BookListActivity> mActivityRule = new IntentsTestRule(BookListActivity.class);

Page 28: Espresso workshop

Share the item and verify intent// Build a result to return when a particular activity is launched. Intent resultData = new Intent(); Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, null);

// Set up result stubbing when an intent sent to "contacts" is seen. intending(hasAction(Intent.ACTION_SEND)).respondWith(result);

// User action that results in ”external message" activity being launched. // Launching activity expects title to be returned and displays it on the screen.onView(withId(R.id.action_share)).perform(click());

// intent validation with existing Intent matchers:Matcher<Intent> intentMatcher = AllOf.allOf( hasAction(Intent.ACTION_CHOOSER), hasExtras(AllOf.allOf( hasEntry(Matchers.equalTo(Intent.EXTRA_TITLE), Matchers.equalTo("Share Image")))));

// Assert that data we set up above is shown. intended(intentMatcher);

Page 29: Espresso workshop

Command-Line?

• ./gradlew connectedAndroidTest

Page 30: Espresso workshop

Need HTML Report?

• Spoon to your rescue• Awesome library from foursquare• Spoon runs tests on all devices detected by adb. • Generates easy, meaningful summary in HTML.• Takes a screenshot for debugging• Runs tests parallel on multiple simulators as well

as real devices

Page 31: Espresso workshop

Install spoon• Add into your build script dependencies

buildscript { repositories { mavenCentral() maven { url 'https://maven.fabric.io/public' } }

dependencies { classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.0.2' classpath 'com.squareup.spoon:spoon-runner:1.2.1' }}

apply plugin: 'spoon’

packagingOptions { exclude 'LICENSE.txt'}

spoon { debug = true adbTimeout = 3000}

//To Run and generate report./gradlew spoon

Page 32: Espresso workshop

Spoon Reports

Page 33: Espresso workshop
Page 34: Espresso workshop

Supports

• IntentsoExplicito Implicit

• Web control• Custom ViewMatchers for nested controls• Custom Failure Handler

Page 35: Espresso workshop

Advantages

• Google supports it • Blazingly Fast• Deterministic as test thread is synchronized

with main thread• Custom Matchers• Debugging become easy because you can view

nested control using hierarchy viewer• No new language to be learned specially for

testing

Page 36: Espresso workshop

Tools Comparison

Page 37: Espresso workshop

Thank you

ketan_soni

[email protected]

References:

https://www.raywenderlich.com/103044/android-intents-tutorial

http://testdroid.com/tech/top-5-android-testing-frameworks-with-examples

https://github.com/emmasuzuki/EspressoSpoonDemo