Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to test the UI interface with Espresso

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to test the UI interface with Espresso. I hope you will get something after reading this article. Let's discuss it together.

Basic test

Let's first take a look at some common tests that you might need to write:

Write test cases according to the requirements according to the above pattern:

Test the view outside the default window hierarchy

There may be several situations in which you need to test a view that is not part of the default window hierarchy. For example, testing a view that might be rendered using WindowManager:

Test an AdapterView

Now continue with the AdapterView test. To test AdapterView,Espresso provides a separate onData () entry, test the adapter project before doing anything to itself or its children. If you want to test an AdapterView, you need to use the onData () method instead of the onView () method.

You can find a complete list of available ObjectMatcher,DataOptions,ViewAction and ViewAssertion in.

Here is a simple example of finding a String type list item that matches the word "Espresso" and executing the click () event on it:

The following example finds a list item with the content "item content:10" and executes a click event on its child view with id R.id.item_id:

Test a RecyclerView

Because the behavior of the RecyclerView object is different from that of the AdapterView object, you cannot use onData () to test the RecyclerView object.

If you want to test RecyclerView, you need to add espresso-contrib package dependencies. This package contains a collection of RecyclerViewActions that you can use to scroll to locations or perform operations on these items.

To interact with RecyclerView using Espresso, you can use the espresso-contrib package (adding a "com.android.support.test.espresso:espresso-contrib:2.2.2" dependency in app / build.gradle) RecyclerViewActions can be used to scroll to a location or perform actions on a project. The collection includes the following RecylerViewActions:

ScrollTo ()-this RecyclerViewActions scrolls to the matching view.

ScrollToHolder ()-this RecyclerViewActions scrolls to the matching view holder.

ScrollToPosition ()-this RecyclerViewActions scrolls to a specific location.

ActionOnHolderItem ()-this RecyclerViewActions executes ViewAction on the matching View Holder.

ActionOnItem ()-this RecyclerViewActions executes ViewAction on the matching View.

ActionOnItemAtPosition ()-this RecyclerViewActions executes ViewAction on the view at a specific location.

Let's look at an example of testing RecyclerView:

The above example will first find the RecyclerView with ID R.id.rv_espresso, then scroll to the fifth location and execute a click event on the project.

Similarly, we can also use other RecyclerViewActions.

Now let's see how to test a custom RecyclerView, such as ExpandableRecyclerView. Consider when you need to test a child view of an expandable view.

First, write a custom action that can be performed:

Now, do this on the child view:

Now let's look at how to write and use Custom Matcher and Custom Failure Handler.

Custom Matcher & FailureHandler

When writing test cases, you may need to implement Custom Matcher or Custom Failure Handler, because in some cases, the default processor may not be sufficient.

1.Custom Matcher

In some cases, the default available matcher may not be enough. In this case, we need to write our own matcher. Consider a situation where when you need to check whether the text in EditText matches the regular expression, because there is no default matcher to do this, you need to do it yourself.

Let's take a look at how to write a Custom Matcher for validating schemas:

Use this matcher:

2.Custom Failure Handler

In some cases, we may want to handle some exceptions thrown by Espresso and customize some implementations, such as recording additional data that might be more meaningful.

After we have written a custom failure handler program, we need to set it up like this:

After reading this article, I believe you have a certain understanding of "how to test the UI interface with Espresso". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report