Introducing the Android Test Tool espress and installation use

Source: Internet
Author: User

Espresso is a new tool, and the API is more accurate relative to other tools. and smaller, simpler and easier to learn. It was originally launched at the 2013 Gtac Convention, with the goal of getting developers to write more concise UI test code for the app.

Although it is for developers (Developer), it is also available for testers. Because it has some key features that Robotiun and selendroid lack.

Key Features

1, fast;

2, the code quickly start;

3, easy to expand;

4, does not need to consider the complex multithreading test;

5, Google as a backer.

Main components of the espresso

espresso– the portal that interacts with views, and exposes some views-independent APIs (such as a fallback button).

viewmatchers– a set of objects that implements a match. Allows you to find the target view (views) in a hierarchy diagram through multiple Onview methods.

viewactions– A view trigger action (for example, click).

viewassertions– a set of assertions used to insert test-critical points that can be used to determine the state of a view.

Example

Onview (Withid (R.id.my_view))//Withid (R.id.my_view) is a matching device
. Perform (click ())//click () event
. Check (Matches (isdisplayed ())); Matches (isdisplayed ()) is an assertion

Code complexity comparisons for the same UI

Uiaotomator:

UiObject setnum = Uiutil.findbyresourceid ("Android:id/edtno1");
Setnum.click ();
Setnum.settext ("10");

Robotium:

EditText TxtVal1 = (edittext) solo.getview (R.ID.EDTNO1);
Solo.clearedittext (TXTVAL1);
Solo.entertext (TxtVal1, "12");

Espresso:

Onview (Withid (R.id.edtno1)). Perform (Cleartext ()). Perform (TypeText ("12"));

As you can see, espresso code is more integrated than other frameworks, and feature blocks are more centralized: Onview is used to locate views, perform to generate events, and check is used to detect checkpoint.

The use of espresso

Setup

1, first download Espresso's stand-alone jar package, and then reference the jar package in the established Android Test project.

2. Include Users-library and instrumentation fields in the Androidmanifest.xml to indicate the test base and the target being tested:

3, on the project name right key->run->run configurations, the instrumentation runner set as follows:

Then click Apply,run to start the test.

Problems that you may encounter:

Test Run Failed:instrumentation run failed due to ' java.lang.ClassNotFoundException '

In this problem, you need to select Java build Path on the left side of the project->properties, and then tick the espresso jar package in the last tab order and export. As follows:

As you can see, since espresso is based on instrumentation (inherited ActivityInstrumentationTestCase2), it is in line with instrumentation in the construction and coding of the project.

Writing case

The established test project content is as follows, mainly realizes to the default Hello World field Search, the click, the judgment function, may see the Espresso code structure and the execution process roughly:

public void testhelloworldtextclicked ()
{
Find
Viewinteraction Helloworldtext = Onview (Withtext ("Hello world!"));

Action
Helloworldtext.perform (click ());

Check
Helloworldtext.check (Viewassertions.matches (isdisplayed ()));
}

Run Test

At the beginning of execution, the bottom console area outputs a log with the espresso tag, and when the APK is installed and the test is started, the contents are displayed in Logcat for easy viewing of running status and debugging.

Run Result:

At the same time, because it is based on instrumentation, it can not escape the limitations of the framework itself:

1. Inability to control activity outside of app process

2. Cannot control manipulating activity while the UI thread is animating

Summarize

Currently, espresso is still in the developer preview State, provided on Google code. But the idea is to wait until it's mature and stable, and migrate it to the Android SDK. Google uses espresso to test its own more than 30 applications, including g+, maps, and drive.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.