Denys Zelenchuk
Android Espresso RevealedWriting Automated UI Tests
Denys Zelenchuk
Zürich, Switzerland
ISBN 978-1-4842-4314-5e-ISBN 978-1-4842-4315-2
Library of Congress Control Number: 2019933720
© Denys Zelenchuk 2019
Standard Apress
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction

Who This Book Is For

This book is a guideline on how to write Android user interface tests for quality assurance engineers and test automation engineers who are interested in Android test automation using Espresso for Android (Espresso). It can also be valuable to Android developers involved in writing UI or integration tests.

This book was written mostly for software or test engineers with medium to advanced knowledge in Android test automation; however, engineers with basic development and test automation experience will benefit from it as well.

What This Book Covers

There is a lot of good official Android testing documentation out there, including GitHub projects with source code, but it is sometimes hard to find the needed portion of information, especially when it comes to plain automated UI end-to-end testing, which Android Espresso users face on a daily basis.

I tried to cover all the major topics of writing functional UI automated tests using the Espresso testing framework, including different ways of running automated tests, architecting test projects in easy and maintainable ways, and using tools that help to implement automated tests with the less effort.

Source Code and Sample Project

To demonstrate all the code examples throughout the book, the Google samples Android architecture TO-DO application project ( https://github.com/googlesamples/android-architecture ) was forked and modified so it was possible to showcase the majority of the Android UI test automation samples using the Espresso for Android and UI Automator testing frameworks.

The sample TO-DO application project contains two branches, where one uses Android Testing Support library dependencies and the other covers AndroidX Test library usage. Readers are free to select the one they prefer.

The source code is also accessible via the Download Source Code link located at www.apress.com/9781484243145 .

Chapter Overview

Chapter 1 : Getting Started with Espresso for Android

This chapter describes the basics of Espresso. It defines goals and approaches of user interface testing and provides examples for setting up tests inside the Android Studio IDE project. It also explains how to identify Android application UI elements, perform actions and assertions, and apply matchers to them. At the end of this chapter, you will be able to write simple tests and execute them from inside the Android Studio IDE on the device or emulator. It also includes examples for how to run tests using Gradle or shell commands.

Chapter 2 : Customizing Espresso for Our Needs

With more advanced examples, you will learn how to implement a custom ViewAction , including clicks and swiping actions; and a ViewMatcher , such as matching complex views as RecyclerView matchers. You will learn how to use custom actions and matchers, implement a custom FailureHandler with the possibility to take and save screenshots upon failure.

Chapter 3 : Writing Espresso Tests with Kotlin

This chapter gives an overview of the benefits of using the Kotlin programming language in tests and explains how to migrate tests written in Java to Kotlin. It also provides an example of creating an Espresso domain specific language in Kotlin.

Chapter 4 : Handling Network Operations and Asynchronous Actions

This chapter explains how to handle application network requests and long-lasting operations during test execution with the help of the IdlingResource interface. It provides an example of using ConditionalWatcher as an alternative to IdlingResource .

Chapter 5 : Verifying and Stubbing Intents with IntentMatchers

This chapter explains using IntentMatchers inside an application as well as how to stub external intents and provide extras. A good example of an external intent is selecting an image from the photo gallery, which then can be used by the application you’re testing.

Chapter 6 : Testing Web Views

This chapter covers testing WebViews inside an application. Implemented WebViews showcase different UI elements that the Espresso-Web API is able to operate on. You will be provided an Espresso-Web cheat sheet as part of the book’s content.

Chapter 7 : Accessibility Testing

This chapter unleashes the topic of how to test application accessibility using Espresso for Android. It raises awareness about the importance of accessibility testing and provides an overview of manual tools that can be used to test application accessibility.

Chapter 8 : Espresso and UI Automator: The Perfect Tandem

This chapter explains one of the most powerful test automation setups for Android, which combines the Espresso test framework with the UI Automator testing tool. Examples show how to test notifications or operate on third-party apps during Espresso tests execution.

Chapter 9 : Dealing with Runtime System Actions and Permissions

This chapter explains different ways that you can deal with system actions like permission request dialogs and describes possible solutions for changing the Android emulator system language programmatically.

Chapter 10 : Android Test Automation Tooling

After reading this chapter, you will understand how to use the Espresso test recorder, set up a test device or emulator to minimize test flakiness, and run tests in the Firebase cloud.

Chapter 11 : Screen Object Design Pattern in Android UI Tests

This chapter shows you how to apply the screen object (the same as page object) architecture approach to the test project, which allows you to reduce the maintenance effort spent on reworking tests after changes in the application’s source code.

Chapter 12 : Testing the Robot Pattern with Espresso and Kotlin

In this chapter, you learn how to apply a testing robot pattern that splits the test implementation from the business logic to the Espresso UI tests.

Chapter 13 : Supervised Monkey Tests with Espresso and UI Automator

This chapter shows how to implement supervised pseudo-monkey tests using Espresso and UI Automator, which can be applicable to applications whose source code you have access to as well as to third-party applications.

Chapter 14 : AndroidX Test Library

This chapter demonstrates how to migrate test code from Android support to the AndroidX Test library. You will find information about new APIs introduced in the AndroidX Test library and see how they can be applied to UI tests.

Chapter 15 : Improving Productivity and Testing Unusual Components

This chapter contains code samples that were not covered in the other chapters and Espresso testing tips that may increase your daily test writing productivity. This includes creating parameterized tests, aggregating tests into test suites, using AndroidStudio Live templates in UI tests, setting SeekBar progress in Espresso UI tests, and Espresso Drawable matchers topics.

What This Book Doesn’t Cover

The goal of the book is to create a guide for how to write end-to-end UI automated tests for Android applications without mocking or stubbing application dependencies. From my point of view, this is the closest way to reproduce end user behavior. The book does not explain how to mock application data and network connection requests or bypass some states in the application workflow.

Tools Requirements

To be able to work with this book, you need to have at least a basic knowledge in working with such tools and platforms as Android Studio IDE, Gradle, GitHub, and shell/bash. In most cases, I explain how to configure your IDE and note which commands should be used to run the specific scripts.

Legal Notice

This book contains code, documentation, and images taken from the Android developers page at https://developer.android.com . They are covered by the Apache 2.0 License ( http://www.apache.org/licenses/ ) mentioned in Appendix C.

Table of Contents

Index 301

About the Author and About the Technical Reviewer

About the Author

Denys Zelenchuk’s
../images/469090_1_En_BookFrontmatter_Figb_HTML.jpg

professional career as a test engineer started in Poland in 2010. Since 2011, he has been involved in testing mobile applications. He has worked at companies such as Tieto Poland and XING (Hamburg, Germany) and currently works and lives in Zurich, Switzerland for Numbrs Personal Finance AG as Senior Quality Assurance Engineer. As of October 2013, he’s been using the Espresso for Android test automation framework to write automated tests.

 

About the Technical Reviewer

Massimo Nardone
../images/469090_1_En_BookFrontmatter_Figc_HTML.jpg

has more than 24 years of experience in security, web/mobile development, and cloud and IT architecture. His true IT passions are security and Android.

He has been programming and teaching others how to program using Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years.

He holds a Master of Science degree in Computing Science from the University of Salerno, Italy.

He has worked as a project manager, software engineer, research engineer, chief security architect, information security manager, PCI/SCADA auditor, and senior lead IT security/Cloud/SCADA architect for many years.

His technical skills include security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, and more.

He worked as a visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki University of Technology (Aalto University). He holds four international patents (in the PKI, SIP, SAML, and Proxy areas).

He currently works as the Chief Information Security Officer (CISO) for Cargotec Oyj and he is member of the ISACA Finland Chapter Board.

Massimo has reviewed more than 45 IT books for different publishers and has coauthored Pro JPA in Java EE 8 (Apress, 2018), Beginning EJB in Java EE 8 (Apress, 2018), and Pro Android Games (Apress, 2015).

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.78.102