Book Image

Mobile Test Automation with Appium

By : Nishant Verma
Book Image

Mobile Test Automation with Appium

By: Nishant Verma

Overview of this book

Appium is an open source test automation framework for mobile applications. It allows you to test all three types of mobile applications: native, hybrid, and mobile web. It allows you to run the automated tests on actual devices, emulators, and simulators. Today, when every mobile app is made on at least two platforms, iOS and Android, you need a tool that allows you to test across platforms. Having two different frameworks for the same app increases the cost of the product and time to maintain it as well. Appium helps save this cost. With mobile app growth exploding, mobile app automation is mainstream now. In this book, author Nishant Verma provides you with a firm grounding in the concepts of Appium while diving into how to set up appium & Cucumber-jvm test automation framework, implement page object design pattern, automate gestures, test execution on emulators and physical devices, and implement continuous integration with Jenkins. The mobile app we have referenced in this book is Quikr because of its relatively lower learning curve to understand the application. It's a local classifieds shopping app.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
5
Understanding Appium Inspector to Find Locators
7
How to Automate Gestures
9
How to Run Appium Test on Devices and Emulators

Preface

With the growing popularity of mobile apps and the enormous growth in the number of mobile devices all around the world, mobile ecosystems are poised to further scale up. Until a couple of years ago, the IT world was dominated by web and enterprise application development and testing. With the growth of mobile apps around the world, the trend is shifting toward mobile development and testing as a niche skill set. Mobile testing had largely been manual until the advent of standard test automation libraries, such as Calabash and Appium.

This book is an effort toward gearing up a better testing workforce by making them educated and aware of a mobile testing and automation tool called Appium. Appium is the most widely adopted mobile test automation tool. The community support has been vibrant, but there is a lack of a structured step-by-step guide or documentation around building a framework. This book is an attempt to bridge that gap and serves as a handheld guide for each tester who wants to build their own mobile test automation framework from scratch.

This book is intended for developers and testers who want to learn mobile app testing and automation using Appium. The book takes you on a journey of understanding Appium and slowly gets you started with the test automation ecosystem. Cucumber is one of the most promising technologies, and is rising in popularity due to the wide adoption of the agile and behavior-driven development methodologies. This book introduces you to the concept of Cucumber and shows how you can build your own testing framework in Cucumber and Appium from scratch. It contains example code snippets of creating a sample project, writing first Appium tests, evolving the test framework, and setting up Jenkins.

The book is organized into two parts:

  • Appium basics: This largely covers an understanding of Appium, desired capabilities in Appium, Appium inspector, and how to use it to find locators, test synchronization, and automate widely used gestures, such as tap, scroll, press, and long press.
  • Appium advanced: This covers design patterns for the automation framework, how to run tests on actual devices and emulators, how to run tests on a Genymotion emulator, continuous integration with Jenkins, and Appium tips and tricks.

What this book covers

Chapter 1, Introduction to Appium, starts with an introduction to the mobile app. It talks about different types of mobile app, that is, native, hybrid, and mobile web. We then take a little closer look at the advantages and limitations of each type of mobile app. We learn about Appium's architecture and about two different automation frameworks Appium uses, XCUITest and UIAutomator2 for iOS and Android, respectively.

Chapter 2, Machine Setup, starts with instructions for setting up your machine in order to start using Appium and write automated tests. In this chapter, we address the setup for both Windows and Mac machines. Some of the prerequisites to install are the most recent Java, Android SDK, Genymotion Emulator, Appium, IntelliJ as the preferred IDE, and the app under test. We will also learn to create the sample Android emulator as well as the Genymotion emulator. We will learn how to install Appium, both via npm and the Appium GUI app. We will take a detailed look at the Appium GUI app and the iOS and Android settings Appium allows.

Chapter 3, Writing Your First Appium Test, helps us write our first Appium test. We will start by creating a Java project in IntelliJ and then get introduced to Cucumber. We create a sample feature file and write our first scenario using the Given-When-Then format. We will learn how to start Appium session and use Appium Inspector. We will then write our first automated test and learn how to run the cucumber test. We will also learn how to write our first test for mobile web app and learn how to use the Chrome developer tools to find the locators. We then run these tests via the IDE.

Chapter 4, Understanding Desired Capabilities, tells us about the concept of desired capabilities in Appium. We learn about the mandatory capabilities and the device-specific desired capabilities, such as Android and iOS. We will look into the server argument and the various flags it exposes along with its sample usage.

Chapter 5, Understanding Appium Inspector to Find Locators, shows us how to use the Appium inspector to find the locator of a UI element. We learn to derive the xPath over the Appium-generated xPath values. We looked into another tool, UIAutomatorViewer, and how to use it. We also learn how to debug the mobile apps using Chrome's inspect feature.

Chapter 6, How to Synchronize Tests, explores the different types of drivers Appium allows you to create, along with the various synchronization strategies. We will learn about the implicit wait, explicit wait, and fluent wait. We also learn about ExpectedConditions and the various predefined conditions it allows.

Chapter 7, How to Automate Gestures, explains implementing various gestures that Appium supports. We will learn how to implement the most frequently used gestures, such as tap, swipe, scroll, and drag and drop. We will also learn about the orientation and how to change the orientation of devices between the landscape and portrait modes.

Chapter 8, Design Patterns in Test Automation, covers the concept of the design pattern in test automation. In this chapter, we will take a detailed look at the page object pattern and then learn how to implement it in the current framework, which we have been building since Chapter 3, Writing Your First Appium Test. We will learn about assertions and where they belong, and we will also learn about the concepts of setup and teardown and how to implement them using pre-specified hooks in cucumber.

Chapter 9, How to Run Appium Test on Devices and Emulators, shows you how to connect physical devices and prepare them for development and testing purposes. It also demonstrates how to configure the Genymotion emulator and run tests. We learn how to retrieve the UDID of iOS devices, the libraries we need to install, and the process for running the test on iOS.

Chapter 10, Continuous Integration with Jenkins, teaches the concept of Gradle and writing Gradle tasks. We start by creating a Gradle task to run the test via command line and moving the project to Git. We navigate through downloading and installing Jenkins. We learn how to set up a Jenkins job and trigger it and view the report. This chapter explains how to implement continuous testing using Jenkins.

Chapter 11, Appium Tips and Tricks, shows you some tips and tricks in the form of code snippets, that can be used to make your test framework more intelligent and innovative. We will learn about switching between webviews and native views, taking screenshots, and recording video using adb commands. We will also explore the approach of running tests in parallel on multiple devices and about the network simulation API.

Chapter 12, Appium Desktop App, explores the new Appium Desktop App. It discusses in details about how to install the new Appium GUI app, how to start an appium server with basic and advanced options. It also explains how to use the Desired Capabilities while setting up a session and how to connect to different end points (the non local server).

By the end of this book, you will have learned about Appium, how to build a test automation framework from scratch in Cucumber and Appium, and how to set up Jenkins to run tests.

Appendix, Appendix takes a deeper insight into different how to's which are needed across chapters. It includes a deep dive into Cucumber and explains various concepts of Cucumber. It also talks about finding details needed for Appium for android installer. Very importantly, it tells us how to install the Google Play services on the Genymotion Emulator.

What you need for this book

To get started with this book, you need basic knowledge of Java. You should be aware of the OOPS concept and should be able to use loops and define classes. A basic understanding of mobile apps and knowledge of Android would be an added advantage; however, it is not a must. The book provides hands-on experience with writing and executing code. There are some software prerequisites, which are explained in the second chapter, which helps set up the development environment and readies your machine for any future mobile automation work using Appium.

Who this book is for

This book is intended for developers/testers who want to learn mobile automation using Appium. It doesn't require any prior experience in testing mobile applications or automation. This book serves as a detailed guide for Appium and a step-by-step guide to building a mobile test automation framework from scratch. The only prerequisite for this book is to have a basic knowledge of Java programming. By the end of this book, you would have gained advanced knowledge of Appium and would have learned how to build a framework in Cucumber and Appium. You will be able to leverage this framework building knowledge by replacing Appium with any other UI automation tool, such as Selenium.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "With version 1.6, Appium has provided support to UiAutomator 2. Appium uses the appium-android-bootstrap module to interact with UI Automator. "

A block of code is set as follows:

@Before
public void startAppiumServer() throws IOException {
    AppiumDriverLocalService appiumService = 
    AppiumDriverLocalService.buildDefaultService();
    appiumService.start();
}

Any command-line input or output is written as follows:

automationName: XCUITest

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking on Start Session will launch a new Appium inspector screen, as illustrated."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mobile-Test-Automation-with-Appium. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.