• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# arkXtest User Guide
2
3
4## Overview
5
6To accelerate test automation of OpenHarmony, arkXtest — an automated test framework that supports both the JavaScript (JS) and TypeScript (TS) programming languages — is provided.
7
8In this document you will learn about the key functions of arkXtest and how to use it to perform unit testing on application or system APIs and to write UI automated test scripts.
9
10
11### Introduction
12
13arkXtest is part of the OpenHarmony toolkit and provides basic capabilities of writing and running OpenHarmony automated test scripts. In terms of test script writing, arkXtest offers a wide range of APIs, including basic process APIs, assertion APIs, and APIs related to UI operations. In terms of test script running, arkXtest offers such features as identifying, scheduling, and executing test scripts, as well as summarizing test script execution results.
14
15
16### Principles
17
18arkXtest is divided into two parts: unit test framework and UI test framework.
19
20- Unit Test Framework
21
22  As the backbone of arkXtest, the unit test framework offers such features as identifying, scheduling, and executing test scripts, as well as summarizing test script execution results. The figure below shows the main functions of the unit test framework.
23
24  ![](figures/UnitTest.PNG)
25
26  The following figure shows the basic unit test process. To start the unit test framework, run the **aa test** command.
27
28  ![](figures/TestFlow.PNG)
29
30- UI Testing Framework
31
32  The UI test framework provides [UiTest APIs](../reference/apis/js-apis-uitest.md) for you to call in different test scenarios. The test scripts are executed on top of the unit test framework mentioned above.
33
34  The figure below shows the main functions of the UI test framework.
35
36  ![](figures/Uitest.PNG)
37
38
39### Limitations and Constraints
40
41- The features of the UI test framework are available only in OpenHarmony 3.1 and later versions.
42- The feature availability of the unit test framework varies by version. For details about the mappings between the features and versions, see [arkXtest](https://gitee.com/openharmony/testfwk_arkxtest/blob/master/README_en.md).
43
44
45## Environment preparations
46
47### Environment Requirements
48
49Software for writing test scripts: DevEco Studio 3.0 or later
50
51Hardware for running test scripts: PC connected to a OpenHarmony device, such as the RK3568 development board
52
53### Setting Up the Environment
54
55[Download DevEco Studio](https://developer.harmonyos.com/cn/develop/deveco-studio#download) and set it up as instructed on the official website.
56
57
58## Creating a Test Script
59
601. Open DevEco Studio and create a project, where the **ohos** directory is where the test script is located.
612. Open the .ets file of the module to be tested in the project directory. Move the cursor to any position in the code, and then right-click and choose **Show Context Actions** > **Create Ohos Test** or press **Alt+Enter** and choose **Create Ohos Test** to create a test class.
62
63## Writing a Unit Test Script
64
65```TS
66import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
67import abilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
68
69const delegator = abilityDelegatorRegistry.getAbilityDelegator()
70export default function abilityTest() {
71  describe('ActsAbilityTest', function () {
72    it('testUiExample',0, async function (done) {
73      console.info("uitest: TestUiExample begin");
74      //start tested ability
75      await delegator.executeShellCommand('aa start -b com.ohos.uitest -a MainAbility').then(result =>{
76        console.info('Uitest, start ability finished:' + result)
77      }).catch(err => {
78        console.info('Uitest, start ability failed: ' + err)
79      })
80      await sleep(1000);
81      //check top display ability
82      await delegator.getCurrentTopAbility().then((Ability)=>{
83        console.info("get top ability");
84        expect(Ability.context.abilityInfo.name).assertEqual('MainAbility');
85      })
86      done();
87    })
88
89    function sleep(time) {
90      return new Promise((resolve) => setTimeout(resolve, time));
91    }
92  })
93}
94```
95
96The unit test script must contain the following basic elements:
97
981. Import of the dependency package so that the dependent test APIs can be used.
99
1002. Test code, mainly about the related logic, such as API invoking.
101
1023. Invoking of the assertion APIs and setting of checkpoints. If there is no checkpoint, the test script is considered as incomplete.
103
104## Writing a UI Test Script
105
106You write a UI test script based on the unit test framework, adding the invoking of APIs provided by the UI test framework to implement the corresponding test logic.
107
108In this example, the UI test script is written based on the preceding unit test script. First, add the dependency package, as shown below:
109
110```js
111import {Driver,ON,Component,MatchPattern} from '@ohos.uitest'
112```
113
114Then, write specific test code. Specifically, implement the click action on the started application page and add checkpoint check cases.
115
116```js
117export default function abilityTest() {
118  describe('ActsAbilityTest', function () {
119    it('testUiExample',0, async function (done) {
120      console.info("uitest: TestUiExample begin");
121      //start tested ability
122      await delegator.executeShellCommand('aa start -b com.ohos.uitest -a MainAbility').then(result =>{
123        console.info('Uitest, start ability finished:' + result)
124      }).catch(err => {
125        console.info('Uitest, start ability failed: ' + err)
126      })
127      await sleep(1000);
128      //check top display ability
129      await delegator.getCurrentTopAbility().then((Ability)=>{
130        console.info("get top ability");
131        expect(Ability.context.abilityInfo.name).assertEqual('MainAbility');
132      })
133      //ui test code
134      //init driver
135      var driver = await Driver.create();
136      await driver.delayMs(1000);
137      //find button by text 'Next'
138      var button = await driver.findComponent(ON.text('Next'));
139      //click button
140      await button.click();
141      await driver.delayMs(1000);
142      //check text
143      await driver.assertComponentExist(ON.text('after click'));
144      await driver.pressBack();
145      done();
146    })
147
148    function sleep(time) {
149      return new Promise((resolve) => setTimeout(resolve, time));
150    }
151  })
152}
153```
154
155## Running the Test Script
156
157You can run a test script in DevEco Studio in any of the following modes:
158
159- Test package level: All test cases in the test package are executed.
160- Test suite level: All test cases defined in the **describe** method are executed.
161- Test method level: The specified **it** method, that is, a single test case, is executed.
162
163![](figures/Execute.PNG)
164
165## Viewing the Test Result
166
167After the test is complete, you can view the test result in DevEco Studio, as shown in the following figure.
168
169![](figures/TestResult.PNG)
170
171## FAQs
172
173### FAQs About Unit Test Cases
174
175#### The logs in the test case are printed after the test case result
176
177**Problem**
178
179The logs added to the test case are displayed after the test case execution, rather than during the test case execution.
180
181**Possible Causes**
182
183More than one asynchronous interface is called in the test case.<br>In principle, logs in the test case are printed before the test case execution is complete.
184
185 **Solution**
186
187If more than one asynchronous interface is called, you are advised to encapsulate the interface invoking into the promise mode
188
189#### Error "fail to start ability" is reported during test case execution
190
191**Problem**
192
193When a test case is executed, the console returns the error message "fail to start ability".
194
195**Possible Causes**
196
197An error occurs during the packaging of the test package, and the test framework dependency file is not included in the test package.
198
199**Solution**
200
201Check whether the test package contains the **OpenHarmonyTestRunner.abc** file. If the file does not exist, rebuild and pack the file and perform the test again.
202
203#### Test case execution timeout
204
205**Problem**
206
207After the test case execution is complete, the console displays the error message "execute time XXms", indicating that the case execution times out.
208
209**Possible Causes**
210
2111. The test case is executed through an asynchronous interface, but the **done** function is not executed during the execution. As a result, the test case execution does not end until it times out.
2122. The time taken for API invocation is longer than the timeout interval set for test case execution.
213
214**Solution**
215
2161. Check the code logic of the test case to ensure that the **done** function is executed even if the assertion fails.
217
2182. Modify the case execution timeout settings under **Run/Debug Configurations** in DevEco Studio.
219
220### FAQs About UI Test Cases
221
222#### The failure log contains "Get windows failed/GetRootByWindow failed"
223
224**Problem**
225
226The UI test case fails to be executed. The HiLog file contains the error message "Get windows failed/GetRootByWindow failed".
227
228**Possible Causes**
229
230The ArkUI feature is disabled. As a result, the control tree information on the test page is not generated.
231
232**Solution**
233
234Run the following command, restart the device, and execute the test case again:
235
236```shell
237hdc shell param set persist.ace.testmode.enabled 1
238```
239
240#### The failure log contains "uitest-api dose not allow calling concurrently"
241
242**Problem**
243
244The UI test case fails to be executed. The HiLog file contains the error message "uitest-api dose not allow calling concurrently".
245
246**Possible Causes**
247
2481. In the test case, the **await** operator is not added to the asynchronous interface provided by the UI test framework.
249
2502. The UI test case is executed in multiple processes. As a result, multiple UI test processes are started. The framework does not support multi-process invoking.
251
252**Solution**
253
2541. Check the case implementation and add the **await** operator to the asynchronous interface invoking.
255
2562. Do not execute UI test cases in multiple processes.
257
258#### The failure log contains "dose not exist on current UI! Check if the UI has changed after you got the widget object"
259
260**Problem**
261
262The UI test case fails to be executed. The HiLog file contains the error message "dose not exist on current UI! Check if the UI has changed after you got the widget object".
263
264**Possible Causes**
265
266After the target component is found in the code of the test case, the device UI changes. As a result, the found component is lost and the emulation operation cannot be performed.
267
268**Solution**
269
270Run the UI test case again.
271