Android Studio 2.2 Espresso Test Recorder-----Hands-free, test by recording test process

Source: Internet
Author: User

To create a UI by espresso test Recorder

Note: The espresso Test Recorder is only a beta version in Android Stuido 2.2.

The tool espresso Test Recorder allows you to create your UI tests without writing any code. By recording a test scenario, you can record the interaction with your device, or you can add assertions to your app snapshot to test specific UI elements. The espresso Test Recorder automatically generates the corresponding UI test file based on the saved records.

The test file for the Espresso Test Recorder is written according to the Espresso testing framework, and the Espresso testing framework is an API in the Android testing support library. The Espresso API creates a concise and stable UI test file based on the user's actions. Because the interaction and assertion do not directly manipulate the interface and components of the app, the running is fragmented and the speed performance is also not good.


Turn off animation for a device

Before you can use the Espresso test recorder, you need to turn off all animations on the test device in case of unexpected results. You can view the "Set up Espresso" guide for the testing UI for a single app page, but be aware that the test recorder will automatically add dependencies when you execute save a recording all you don't need to add manually.


Record a espresso test file

Espresso test File Bao Kuang has two main parts: UI interaction and assertion of view element. UI interactions include clicks and input actions that users may have with your app. Assertions are used to detect the contents of a component in a screen or the presence of a component. For example, a note testing app's espresso test contains UI interaction that clicks on a button and detects the presence of a button and the content of the note through assertions.

This section will review how to create a test file through the Espresso Test recorder and how to save a test file that is automatically generated by recording.


Recording UI Interaction

The steps to test the logger using espresso are as follows:

1. Click Run > Record Espresso Test

2. In the Select Target Device window (select Deployment target Windows), select the device you want to record the test. If necessary, create a new Android virtual appliance (create a to device:https://developer.android.com/studio/run/managing-avds.html). then click OK.

The 3.Espresso Test Recorder will trigger a rebuild of the project, so you must install and run the app before interacting with the device through the espresso Test recorder. After the app runs, the Record Your Test window will appear and the main panel will show "No events recorded yet" because you have not interacted with the device. Events such as "tap" and "type" are output by interacting with the device.

Note: You may see a "Waiting for Debugger" or "Attaching Debugger" window appear on your device before you interact. The espresso test logger uses the debugger (debugger) to output UI events. When the debugger is connected, the pop-up window will automatically close; Do not click force Close.


The interaction record will be displayed in the main interface of the Record Your test window shown in 1. When you run the tests, the espresso tests are executed in the order in which they are performed.


Figure 1. The Record Your Test window for the output UI interaction



Note:

Click Run > Record Espresso Test


Select Target Device


Pop-up record Your test interface


Click Add Assertion to pop up the adding assertion box


Then manipulate the device and output the log of the Operation event



Select the control to be tested by the assertion



The selected control is highlighted in red in the screenshot to the right, and the type of assertion is selected in the second drop-down box


Tap Save assertion to save the assertion or click Save and add another to add another new assertion. Click on the "Save assertion" button in the pop-up window, you can continue to select Add assertion added assertions, you can also click the complete recording to end.


After clicking Complete recording, enter the class name of the generated test file in the pop-up box, use the default if not modified, then click Save


In the project you can see the class named when you just clicked save


Click Mainactivitytest, then right-click and select Run ' mainactivitytest '


Select the target device to run


Click OK, and then the Run window under window will appear in the Mainactivitytest tab page and display the progress and information



Adding assertions to test UI elements

Assert that there are three ways to determine whether a view element exists and what it contains:

---text is: Tests the contents of text in the selected view element (the content is consistent with the assertion)

---exists: Tests whether the selected view element exists (the control exists)

---does not exist: Tests that the current view element is not in the current view level (the control does not exist)


The steps to add an assertion to a test are as follows:

1. Click Add Assertion.espresso to get a bullet box for the UI level and other capture screen for the app's status information. After the espresso snaps to the screen, the frame is automatically closed.

The main interface of the 2.Record Your Test window displays the layout of the current screen. Select the element you want to use in the snapshot or select the view element in the Edit assertion drop-down box below the window to create the assertion. The selected View object will be displayed in a red highlighted box.

3. Select the desired assertion from the second drop-down box. The Espresso menu is populated with assertions that are valid for the view element.

---If the assertion selected is "text is", espresso automatically adds text information to the selected view element. Of course, you can edit the text information required by the target assertion under edit assertition.

4. Click Save and ADD another to create another assertion or click Save assertion to close the assertion interface.


The snapshot in Figure 2 is the detection of whether the header information is "Happy testing!" The "text is" assertion


Figure 2: Edit Assertion box After selecting a view element (Edit assertion box) (Red section)


When you create an assertion, even when the assertion interface opens the Record Your Test window, you can continue to interact with the app, espresso the test recorder will continue to output the log of the event, but if the assertion you are editing at this time, the interaction information record will be saved.


Note: Because the espresso Test recorder is an experimental feature, the tool currently only supports adding assertions for the vertical screen. Although you can add assertions horizontally, the screen catcher still displays it as portrait, and the red bounds of the selected UI element do not match the actual position of the element.


Save the recorded information

After interacting with the app and saving the assertion information, use the following steps to save the recorded information and generate the espresso test:

1. Click Complete Recording. A window will then appear to select the name of the class you want to test.

The 2.Espresso Test Recorder gives the generated test Class A unique name based on the package name of the running app and the name of the open activity. You can change the class name by using test class name. Click Save.

---If you don't add Espresso dependencies to your app, a missing Espresso dependencies pop-up will pop up when you save the test. Click Yes to move the dependency to the Build.gradle.

3. When espresso Test Recorder generates the file, it is automatically opened, and Android Studio creates the class name that will be tested in project.

Where---Test is saved depends on where your instrumentation test is saved, and where the activity is running. For example, the Note testing app's test is saved in the Directory Src>androidtest>java>com.example.username.appname folder of the project you are recording.



Run a local espresso test

In Android Studio, click the Project window on the left:

1. Open the name you want to run and open the test file you want to run. The address of the test file depends on the location of the instrumentation test and the package name of the open activity. The following example shows where the test files for the Notest testing app will be saved:
---If you are in Android view, navigate to Java>com.example.username.appname (Androidtest).

---If you are in Project view, navigate to the Src>androidtest>java>com.example.username.appname

2. Right-click on the test file you want to run, then click Run ' testname '

---can also open the test file, right-click on the class name or method. See more information on the TTest Your App (https://developer.android.com/studio/test/index.html#run_a_test) page.

3. In the Select the Target Device window, select the device you want to run. If necessary, create a new emulator. Click OK.


Note:

Project in Android view, test the location of the file


Project location test files under Project view



Monitor the process of testing in the Run window at the bottom of the IDE. Android studio recreated and generated the project, and in the Run window opens a new tab page based on the name of the test, as shown in 3. You can see in this tab page whether the test passed and how long it will take to run. When the run is received, the tab page will output "Tests ran to completion".


Figure 3: Running the local espresso test after the output from the Run window


For more information on if write test configuration, view Create and Edit run/debug configurations (https://developer.android.com/studio/run/ rundebugconfig.html#creating section of "Defining a test configuration for a class or method".



Run a espresso test with firebase test lab

You can use the Firebase test lab to run test files generated by the espresso Test recorder in a variety of different configuration information devices in the cloud. Create a Firebase project for your app, then follow the instructions Run your tests with Firebase Test Lab(https://developer.android.com/ TRAINING/TESTING/UNIT-TESTING/INSTRUMENTED-UNIT-TESTS.HTML#RUN-CTL) is created directly in Android Studio.


Figure 4: Output after running on multiple devices on Firebase Test Lab




Android Studio 2.2 Espresso Test Recorder-----Hands-free, test by recording test process

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.