Chapter 6 APP Stability Testing
1. Overview
The automated test framework arkxtest is an important part of the toolset. It supports JS/TS language unit test framework (JsUnit) and UI test framework (UiTest). JsUnit provides unit test case execution capabilities, basic interfaces for writing test cases, and generates corresponding reports for testing system or application interfaces. UiTest provides concise and easy-to-use APIs for finding and operating interface controls, supporting users to develop automated test scripts based on interface operations. This guide introduces the main functions, implementation principles, environment preparation of the test framework, as well as test script writing and execution, etc.
2. Environment Preparation
Environment requirements: The writing of automated scripts is mainly based on DevEco Studio. It is recommended to use version 3.0 or later. Script execution requires PC to connect to hardware device.
Script execution requires PC to connect to board card hardware device.
DevEco Studio environment setup can be referred to the introduction in Chapter 03 - Development Environment Preparation for download and related configuration.
3. Create and Write Test Script
Create test script in DevEco Studio to create an application development project. The ohos directory is where the test scripts are located. Open the ets file under the module to be tested in the project directory, place the cursor at any position in the code, right-click > Show Context Actions > Create Ohos Test, or use the shortcut key Alt+enter > Create Ohos Test to create a test class. For more guidance, refer to the guidance in DevEco Studio.
4. Write Unit Test Script
Note
This section mainly describes the capabilities supported by the unit test framework and how to use them.
In the unit test framework, test scripts need to include the following basic elements:
Import dependency packages to use the testing interfaces.
Write test code, mainly writing relevant logic for test code, such as interface calls, etc.
Assert interface calls, set checkpoints in the test code. Without checkpoints, it cannot be considered a complete test script.
The scenario implemented by the following example code is: start the test page and check if the currently displayed page on the device is the expected page.

5. Write UI Test Script
This section mainly introduces the capabilities supported by the UI test framework and how to use the corresponding capability APIs. UI testing is based on unit testing. UI test scripts add calls to UiTest interfaces (provide links) on unit test scripts to complete corresponding testing activities. The following example code is incrementally written on the above unit test script. It implements clicking operations on the started application page and then detecting whether the current page change is the expected change.
Add dependency imports: import { Driver, ON } from '@ohos.UiTest'
Write index.ets page code:

- Write specific test code:


- Execute test script
DevEco Studio execution is through clicking buttons. Currently, it supports the following execution methods:
Test package level execution, i.e., execute all test cases in the test package.
Test suite level execution, i.e., execute all test cases defined in the describe method.
Test method level execution, i.e., execute the specified it method, which is a single test case.

View test results
After test execution is complete, you can view the test results directly in DevEco Studio. An example is shown in the following figure:

6. Common Questions
Common Questions for Unit Test Cases
Error 1
Printed logs in the case appear after the case result.
Problem Description
Log print information added in the case does not appear during case execution, but appears after the case execution ends.
Possible Causes
This situation only exists when there are asynchronous interface calls in the case. In principle, all log information in the case should be printed before the case execution ends.
Solution
When there is more than one asynchronous interface being called, it is recommended to encapsulate the interface calls in Promise manner.
Error 2
Error reported when executing case: fail to start ability
Problem Description
When executing the test case, the case execution fails, and the console returns the error: fail to start ability.
Possible Causes
There is a problem during the test package packaging process, and the test framework dependency files are not packaged in the test package.
Solution
Check if the test package contains the OpenHarmonyTestRunner.abc file. If not, recompile and package, then execute the test again.
Error 3
Case timeout error reported when executing case.
Problem Description
Case execution ends, and the console prompts "execute time XXms" error, meaning case execution timed out.
Possible Causes
- The case executes asynchronous interfaces, but the done function is not executed during execution, causing the case execution to not end until timeout.
- The function called by the case takes too long, exceeding the timeout setting for case execution.
- An assertion fails in the function called by the case, throwing a failure exception, causing the case execution to not end until timeout.
Solution
- Check the case code logic to ensure that even in assertion failure scenarios, the code can reach the done function to ensure case execution ends.
- You can modify the case execution timeout configuration parameter in Run/Debug Configurations in the IDE to avoid case execution timeout.
- Check the case code logic and assertion results to ensure assertions pass.
Common Questions for UI Test Cases
Error 1
Failed log has "Get windows failed/GetRootByWindow failed" error information.
Problem Description
UI test case execution fails. Check the hilog log and find "Get windows failed/GetRootByWindow failed" error information in the log.
Possible Causes
The system ArkUI switch is not turned on, causing the control tree information of the tested interface not to be generated.
Solution
Execute the following commands and restart the device, then execute the case again.
hdc shell param set persist.ace.testmode.enabled 1
shell
hdc shell param set persist.ace.testmode.enabled 1Error 2
Failed log has "uitest-api does not allow calling concurrently" error information.
Problem Description
UI test case execution fails. Check the hilog log and find "uitest-api does not allow calling concurrently" error information in the log.
Possible Causes
- The asynchronous interface provided by the UI test framework in the case is not called with await syntax sugar.
- Multi-process execution of UI test cases causes pulling up multiple UITest processes, which the framework does not support for multi-process calls.
Solution
- Check the case implementation and add await syntax sugar for asynchronous interface calls.
- Avoid multi-process execution of UI test cases.
Error 3
Failed log has "does not exist on current UI! Check if the UI has changed after you got the widget object" error information.
Problem Description
UI test case execution fails. Check the hilog log and find "does not exist on current UI! Check if the UI has changed after you got the widget object" error information in the log.
Possible Causes
After finding the target control in the case code, the device interface changes, causing the found control to be lost, and the next step of simulation operation cannot be performed.
Solution
Re-execute the UI test case.
