|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| coverage_signal_handler/ | | 22-Mar-2025 | - | 73 | 51 |
| hamock/ | | 22-Mar-2025 | - | 1,428 | 1,110 |
| jsunit/ | | 22-Mar-2025 | - | 5,317 | 4,058 |
| testserver/ | | 22-Mar-2025 | - | 1,942 | 1,424 |
| uitest/ | | 22-Mar-2025 | - | 20,785 | 17,213 |
| CODEOWNERS | D | 22-Mar-2025 | 602 | 14 | 13 |
| LICENSE | D | 22-Mar-2025 | 9.9 KiB | 177 | 150 |
| OAT.xml | D | 22-Mar-2025 | 3.8 KiB | 61 | 12 |
| README_en.md | D | 22-Mar-2025 | 48.1 KiB | 1,110 | 866 |
| README_zh.md | D | 22-Mar-2025 | 69.8 KiB | 1,597 | 1,328 |
| bundle.json | D | 22-Mar-2025 | 2.4 KiB | 87 | 86 |
README_en.md
1# arkXtest
2
3## Introduction
4arkXtest, the automated test framework of OpenHarmony, consists of the JS unit test framework (JsUnit) and UI test framework (UiTest).
5
6JsUnit provides APIs for writing test cases for system or application interfaces, executing unit test cases, and generating test reports.
7
8UiTest provides simple and easy-to-use APIs for locating and operating UI components, helping users to develop automatic test scripts based on UI operations.
9
10## Directory Structure
11
12```
13arkXtest
14 |-----jsunit Unit test framework
15 |-----uitest UI test framework
16```
17## Constraints
18The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
19
20## JsUnit Features
21
22| No. | Feature | Description |
23| ---- | -------- | ------------------------------------------------------------ |
24| 1 | Basic process support| Provides APIs for writing and executing test cases. |
25| 2 | Assertion library | Provides APIs for checking whether the actual value of a test case is the same as the expected value. |
26| 3 | Mock| Provides APIs for mocking functions to return the specified value or perform the specified action.|
27| 4 | Data driving| Provides APIs for reusing a test script with different input data.|
28
29### How to Use
30
31#### Basic Process Support
32
33Test cases use the common syntax in the industry. **describe** defines a test suite, and **it** specifies a test case.
34
35| No. | API | Description |
36| ---- | ---------- | ------------------------------------------------------------ |
37| 1 | describe | Defines a test suite. This API supports two parameters: test suite name and test suite function. |
38| 2 | beforeAll | Presets an action, which is performed only once before all test cases of the test suite start. This API supports only one parameter: preset action function.|
39| 3 | beforeEach | Presets an action, which is performed before each unit test case starts. The number of execution times is the same as the number of test cases defined by **it**. This API supports only one parameter: preset action function.|
40| 4 | afterEach | Presets a clear action, which is performed after each unit test case ends. The number of execution times is the same as the number of test cases defined by **it**. This API supports only one parameter: clear action function.|
41| 5 | afterAll | Presets a clear action, which is performed after all test cases of the test suite end. This API supports only one parameter: clear action function.|
42| 6 | it | Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.|
43| 7 | expect | Defines a variety of assertion methods, which are used to declare expected Boolean conditions. |
44
45The sample code is as follows:
46
47```javascript
48import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
49import demo from '@ohos.bundle'
50
51export default async function abilityTest() {
52 describe('ActsAbilityTest', function () {
53 it('String_assertContain_success', 0, function () {
54 let a = 'abc'
55 let b = 'b'
56 expect(a).assertContain(b)
57 expect(a).assertEqual(a)
58 })
59 it('getBundleInfo_0100', 0, async function () {
60 const NAME1 = "com.example.MyApplicationStage"
61 await demo.getBundleInfo(NAME1,
62 demo.BundleFlag.GET_BUNDLE_WITH_ABILITIES | demo.BundleFlag.GET_BUNDLE_WITH_REQUESTED_PERMISSION)
63 .then((value) => {
64 console.info(value.appId)
65 })
66 .catch((err) => {
67 console.info(err.code)
68 })
69 })
70 })
71}
72```
73
74
75
76#### Assertion Library
77
78Available APIs:
79
80
81| No. | API | Description |
82| :--- | :--------------- | ------------------------------------------------------------ |
83| 1 | assertClose | Checks whether the proximity between the actual value and the expected value (0) is the expected value (1). |
84| 2 | assertContain | Checks whether the actual value contains the expected value. |
85| 3 | assertEqual | Checks whether the actual value is equal to the expected value. |
86| 4 | assertFail | Throws an error. |
87| 5 | assertFalse | Check whether the actual value is **false**. |
88| 6 | assertTrue | Checks whether the actual value is **true**. |
89| 7 | assertInstanceOf | Checks whether the actual value is of the type specified by the expected value,basic types are supported. |
90| 8 | assertLarger | Checks whether the actual value is greater than the expected value. |
91| 9 | assertLess | Checks whether the actual value is less than the expected value. |
92| 10 | assertNull | Checks whether the actual value is null. |
93| 11 | assertThrowError | Checks whether the error thrown by the actual value is the expected value. |
94| 12 | assertUndefined | Checks whether the actual value is **undefined**. |
95
96The sample code is as follows:
97
98```javascript
99import { describe, it, expect } from '@ohos/hypium'
100export default async function abilityTest() {
101 describe('assertClose', function () {
102 it('assertBeClose_success', 0, function () {
103 let a = 100
104 let b = 0.1
105 expect(a).assertClose(99, b)
106 })
107 it('assertBeClose_fail', 0, function () {
108 let a = 100
109 let b = 0.1
110 expect(a).assertClose(1, b)
111 })
112 it('assertBeClose_fail', 0, function () {
113 let a = 100
114 let b = 0.1
115 expect(a).assertClose(null, b)
116 })
117 it('assertBeClose_fail', 0, function () {
118 expect(null).assertClose(null, 0)
119 })
120 it('assertInstanceOf_success', 0, function () {
121 let a = 'strTest'
122 expect(a).assertInstanceOf('String')
123 })
124 })
125}
126```
127
128
129#### Mock
130
131##### Constraints
132
133JsUnit provides the mock capability since npm [1.0.1](https://repo.harmonyos.com/#/en/application/atomService/@ohos%2Fhypium). You must modify the npm version in **package.info** of the source code project before using the mock capability.
134
135- **Available APIs**
136
137| No. | API | Description|
138| --- | --- | --- |
139| 1 | mockFunc(obj: object, f: function())| Mocks a function in the object of a class. The parameters **obj** and **f** must be passed in. This API supports asynchronous functions. <br>**NOTE**: This API does not focus on the implementation of the original function. Therefore, it does not matter whether the original function is implemented synchronously or asynchronously.|
140| 2 | when(mockedfunc: function)| Checks whether the input function is mocked and marked. A function declaration is returned.|
141| 3 | afterReturn(x: value)| Sets an expected return value, for example, a string or promise.|
142| 4 | afterReturnNothing() | Sets the expected return value to **undefined**, that is, no value will be returned.|
143| 5 | afterAction(x: action)| Sets the expected return value to be an action executed by a function.|
144| 6 | afterThrow(x: msg)| Sets an exception to throw and the error message.|
145| 7 | clear() | Restores the mocked object after the test case is complete (restores the original features of the object).|
146| 8 | any | Returns the expected value if a parameter of any type (except **undefined** and **null**) is passed in. This API must be called by **ArgumentMatchers.any**.|
147| 9 | anyString | Returns the expected value if a string is passed in. This API must be called by **ArgumentMatchers.anyString**.|
148| 10 | anyBoolean | Returns the expected value if a Boolean value is passed in. This API must be called by **ArgumentMatchers.anyBoolean**.|
149| 11 | anyFunction | Returns the expected value if a function is passed in. This API must be called by **ArgumentMatchers.anyFunction**.|
150| 12 | anyNumber | Returns the expected value if a number is passed in. This API must be called by **ArgumentMatchers.anyNumber**.|
151| 13 | anyObj | Returns the expected value if an object is passed in. This API must be called by **ArgumentMatchers.anyObj**.|
152| 14 | matchRegexs(Regex) | Returns the expected value if a regular expression is passed in. This API must be called by **ArgumentMatchers.matchRegexs(Regex)**.|
153| 15 | verify(methodName, argsArray) | Verifies whether a function and its parameters are processed as expected. This API returns a **VerificationMode**, which is a class that provides the verification mode. This class provides functions such as **times(count)**, **once()**, **atLeast(x)**, **atMost(x)**, and **never()**.|
154| 16 | times(count) | Verifies whether the function was executed the specified number of times.|
155| 17 | once() | Verifies whether the function was executed only once.|
156| 18 | atLeast(count) | Verifies whether the function was executed at least **count** times.|
157| 19 | atMost(count) | Verifies whether the function was executed at most **count** times.|
158| 20 | never | Verifies whether the function has never been executed.|
159| 21 | ignoreMock(obj, method) | Restores the mocked function in the object. This API is valid for mocked functions.|
160| 22 | clearAll() | Clears all data and memory after the test cases are complete.|
161
162- **Examples**
163
164You can refer to the following examples to import the mock module and write test cases.
165
166- **NOTICE**<br>
167You must import **MockKit** and **when**. You can import other assertion APIs based on the test cases.
168Example: `import {describe, expect, it, MockKit, when} from '@ohos/hypium'`
169
170Example 1: Use **afterReturn**.
171
172```javascript
173import {describe, expect, it, MockKit, when} from '@ohos/hypium';
174
175export default function ActsAbilityTest() {
176 describe('ActsAbilityTest', function () {
177 it('testMockfunc', 0, function () {
178 console.info("it1 begin");
179
180 // 1. Create a MockKit object.
181 let mocker = new MockKit();
182
183 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
184 class ClassName {
185 constructor() {
186 }
187
188 method_1(arg) {
189 return '888888';
190 }
191
192 method_2(arg) {
193 return '999999';
194 }
195 }
196
197 let claser = new ClassName();
198
199 // 3. Mock method_1 of the ClassName class.
200 let mockfunc = mocker.mockFunc(claser, claser.method_1);
201 when(mockfunc)('test').afterReturn('1');
202
203 // 4. Assert whether the mocked function is implemented as expected.
204 // The operation is successful if 'test' is passed in.
205 expect(claser.method_1('test')).assertEqual('1'); // The operation is successful.
206
207 // The operation fails if 'abc' is passed in.
208 //expect(claser.method_1('abc')).assertEqual('1'); // The operation fails.
209 });
210 });
211}
212```
213- **NOTICE**<br>
214In `when(mockfunc)('test').afterReturn('1');`, `('test')` is the value to pass in the mocked function. Currently, only one parameter is supported. `afterReturn('1')` is the expected return value. The expected value is returned only when `('test')` is passed in.
215
216Example 2: Use **afterReturnNothing**.
217
218```javascript
219import {describe, expect, it, MockKit, when} from '@ohos/hypium';
220
221export default function ActsAbilityTest() {
222 describe('ActsAbilityTest', function () {
223 it('testMockfunc', 0, function () {
224 console.info("it1 begin");
225
226 // 1. Create a MockKit object.
227 let mocker = new MockKit();
228
229 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
230 class ClassName {
231 constructor() {
232 }
233
234 method_1(arg) {
235 return '888888';
236 }
237
238 method_2(arg) {
239 return '999999';
240 }
241 }
242
243 let claser = new ClassName();
244
245 // 3. Mock method_1 of the ClassName class.
246 let mockfunc = mocker.mockFunc(claser, claser.method_1);
247
248 // 4. Set the action to be performed when the test case ends. For example, set it to afterReturnNothing(), which returns no value.
249 when(mockfunc)('test').afterReturnNothing();
250
251 // 5. Assert whether the mocked function is implemented as expected. Use the assertion APIs corresponding to Step 4.
252 // The operation is successful if 'test' is passed in.
253 // The mocked claser.method_1 does not return '888888'. Instead, afterReturnNothing() takes effect, that is, no value is returned.
254 expect(claser.method_1('test')).assertUndefined(); // The operation is successful.
255
256 // The operation fails if '123' is passed in.
257 // expect(method_1(123)).assertUndefined();// The operation fails.
258 });
259 });
260}
261```
262
263Example 3: Set the parameter type to **any**, that is, allow any parameter value except **undefine** and **null**.
264
265
266- **NOTICE**<br>
267The **ArgumentMatchers** class, for example, **ArgumentMatchers.any**, must be imported.
268
269```javascript
270import {describe, expect, it, MockKit, when, ArgumentMatchers} from '@ohos/hypium';
271
272export default function ActsAbilityTest() {
273 describe('ActsAbilityTest', function () {
274 it('testMockfunc', 0, function () {
275 console.info("it1 begin");
276
277 // 1. Create a MockKit object.
278 let mocker = new MockKit();
279
280 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
281 class ClassName {
282 constructor() {
283 }
284
285 method_1(arg) {
286 return '888888';
287 }
288
289 method_2(arg) {
290 return '999999';
291 }
292 }
293
294 let claser = new ClassName();
295
296 // 3. Mock method_1 of the ClassName class.
297 let mockfunc = mocker.mockFunc(claser, claser.method_1);
298 // Set the parameter matcher and expected return value as required.
299 when(mockfunc)(ArgumentMatchers.any).afterReturn('1');
300
301 // 4. Assert whether the mocked function is implemented as expected.
302 // The operation is successful if a string is passed in.
303 expect(claser.method_1('test')).assertEqual('1'); // The operation is successful.
304 // The operation is successful if a number (for example '123') is passed in.
305 expect(claser.method_1(123)).assertEqual('1'); // The operation is successful.
306 // The operation is successful if a Boolean value (for example 'true') is passed in.
307 expect(claser.method_1(true)).assertEqual('1');// The operation is successful.
308
309 // The operation fails if an empty value is passed in.
310 //expect(claser.method_1()).assertEqual('1');// The operation fails.
311 });
312 });
313}
314```
315
316Example 4: Set the parameter type to **anyString**.
317
318```javascript
319import {describe, expect, it, MockKit, when, ArgumentMatchers} from '@ohos/hypium';
320
321export default function ActsAbilityTest() {
322 describe('ActsAbilityTest', function () {
323 it('testMockfunc', 0, function () {
324 console.info("it1 begin");
325
326 // 1. Create a MockKit object.
327 let mocker = new MockKit();
328
329 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
330 class ClassName {
331 constructor() {
332 }
333
334 method_1(arg) {
335 return '888888';
336 }
337
338 method_2(arg) {
339 return '999999';
340 }
341 }
342
343 let claser = new ClassName();
344
345 // 3. Mock method_1 of the ClassName class.
346 let mockfunc = mocker.mockFunc(claser, claser.method_1);
347 // Set the following parameters as required.
348 when(mockfunc)(ArgumentMatchers.anyString).afterReturn('1');
349
350 // 4. Assert whether the mocked function is implemented as expected.
351 // The operation is successful if a string is passed in.
352 expect(claser.method_1('test')).assertEqual('1'); // The operation is successful.
353 expect(claser.method_1('abc')).assertEqual('1'); // The operation is successful.
354
355 // The operation fails if a number or a Boolean value is passed in.
356 //expect(claser.method_1(123)).assertEqual('1'); // The operation fails.
357 //expect(claser.method_1(true)).assertEqual('1'); // The operation fails.
358 });
359 });
360}
361```
362
363Example 5: Set the parameter type to **matchRegexs (Regex)**.
364```javascript
365import {describe, expect, it, MockKit, when, ArgumentMatchers} from '@ohos/hypium';
366
367export default function ActsAbilityTest() {
368 describe('ActsAbilityTest', function () {
369 it('testMockfunc', 0, function () {
370 console.info("it1 begin");
371
372 // 1. Create a MockKit object.
373 let mocker = new MockKit();
374
375 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
376 class ClassName {
377 constructor() {
378 }
379
380 method_1(arg) {
381 return '888888';
382 }
383
384 method_2(arg) {
385 return '999999';
386 }
387 }
388
389 let claser = new ClassName();
390
391 // 3. Mock method_1 of the ClassName class.
392 let mockfunc = mocker.mockFunc(claser, claser.method_1);
393 // Set a regular expression to match, for example, /123456/.
394 when(mockfunc)(ArgumentMatchers.matchRegexs(/123456/)).afterReturn('1');
395
396 // 4. Assert whether the mocked function is implemented as expected.
397 // The operation is successful if a string, for example, '1234567898', is passed in.
398 expect(claser.method_1('1234567898')).assertEqual('1'); // The operation is successful.
399 // The string '1234567898' matches the regular expression /123456/.
400
401 // The operation fails if '1234' is passed in.
402 //expect(claser.method_1('1234').assertEqual('1'); // The operation fails because '1234' does not match the regular expression /123456/.
403 });
404 });
405}
406```
407
408Example 6: Use **verify()**.
409```javascript
410import {describe, expect, it, MockKit, when} from '@ohos/hypium';
411
412export default function ActsAbilityTest() {
413 describe('ActsAbilityTest', function () {
414 it('testMockfunc', 0, function () {
415 console.info("it1 begin");
416
417 // 1. Create a MockKit object.
418 let mocker = new MockKit();
419
420 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
421 class ClassName {
422 constructor() {
423 }
424
425 method_1(...arg) {
426 return '888888';
427 }
428
429 method_2(...arg) {
430 return '999999';
431 }
432 }
433
434 let claser = new ClassName();
435
436 // 3. Mock method_1 and method_2 of the ClassName class.
437 mocker.mockFunc(claser, claser.method_1);
438 mocker.mockFunc(claser, claser.method_2);
439
440 // 4. Call the following methods.
441 claser.method_1('abc', 'ppp');
442 claser.method_1('abc');
443 claser.method_1('xyz');
444 claser.method_1();
445 claser.method_1('abc', 'xxx', 'yyy');
446 claser.method_1();
447 claser.method_2('111');
448 claser.method_2('111', '222');
449
450 //5. Verify the mocked functions.
451 mocker.verify('method_1', []).atLeast(3); // The result is "failed".
452 // Verify whether 'method_1' with an empty parameter list was executed at least three times.
453 // The result is "failed" because 'method_1' with an empty parameter list was executed only twice in Step 4.
454 //mocker.verify('method_2',['111']).once(); // The result is "success".
455 //mocker.verify('method_2',['111',,'222']).once(); // The result is "success".
456 });
457 });
458}
459```
460
461Example 7: Use **ignoreMock(obj, method)**.
462```javascript
463import {describe, expect, it, MockKit, when, ArgumentMatchers} from '@ohos/hypium';
464
465export default function ActsAbilityTest() {
466 describe('ActsAbilityTest', function () {
467 it('testMockfunc', 0, function () {
468 console.info("it1 begin");
469
470 // 1. Create a MockKit object.
471 let mocker = new MockKit();
472
473 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
474 class ClassName {
475 constructor() {
476 }
477
478 method_1(...arg) {
479 return '888888';
480 }
481
482 method_2(...arg) {
483 return '999999';
484 }
485 }
486
487 let claser = new ClassName();
488
489 // 3. Mock method_1 and method_2 of the ClassName class.
490 let func_1 = mocker.mockFunc(claser, claser.method_1);
491 let func_2 = mocker.mockFunc(claser, claser.method_2);
492
493 // 4. Modify the mocked functions.
494 when(func_1)(ArgumentMatchers.anyNumber).afterReturn('4');
495 when(func_2)(ArgumentMatchers.anyNumber).afterReturn('5');
496
497 // 5. Call the following methods.
498 console.log(claser.method_1(123)); // The return value is 4, which is the same as the expected value in Step 4.
499 console.log(claser.method_2(456)); // The return value is 5, which is the same as the expected value in Step 4.
500
501 // 6. Restore method_1 using ignoreMock().
502 mocker.ignoreMock(claser, claser.method_1);
503 // Call claser.method_1 and check the execution result.
504 console.log(claser.method_1(123)); // The return value is 888888, which is the same as that returned by the original function.
505 // Test with assertions.
506 expect(claser.method_1(123)).assertEqual('4'); // The return value is "failed", which meets the expected value of ignoreMock().
507 claser.method_2(456); // The return value is 5, which is the same as the expected value in Step 4 because method_2 is not restored.
508 });
509 });
510}
511```
512
513Example 8: Use **clear()**.
514
515```javascript
516import {describe, expect, it, MockKit, when, ArgumentMatchers} from '@ohos/hypium';
517
518export default function ActsAbilityTest() {
519 describe('ActsAbilityTest', function () {
520 it('testMockfunc', 0, function () {
521 console.info("it1 begin");
522
523 // 1. Create a MockKit object.
524 let mocker = new MockKit();
525
526 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
527 class ClassName {
528 constructor() {
529 }
530
531 method_1(...arg) {
532 return '888888';
533 }
534
535 method_2(...arg) {
536 return '999999';
537 }
538 }
539 let claser = new ClassName();
540
541 // 3. Mock method_1 and method_2 of the ClassName class.
542 let func_1 = mocker.mockFunc(claser, claser.method_1);
543 let func_2 = mocker.mockFunc(claser, claser.method_2);
544
545 // 4. Modify the mocked functions.
546 when(func_1)(ArgumentMatchers.anyNumber).afterReturn('4');
547 when(func_2)(ArgumentMatchers.anyNumber).afterReturn('5');
548
549 // 5. Call the following methods.
550 //expect(claser.method_1(123)).assertEqual('4'); // The return value is the same as the expected value.
551 //expect(claser.method_2(456)).assertEqual('5'); // The return value is the same as the expected value.
552
553 // 6. Clear the mock operation.
554 mocker.clear(claser);
555 // Call claser.method_1 and check the execution result.
556 expect(claser.method_1(123)).assertEqual('4'); // The return value is "failed", which meets the expectation.
557 expect(claser.method_2(456)).assertEqual('5'); // The return value is "failed", which meets the expectation.
558 });
559 });
560}
561```
562
563
564Example 9: Use **afterThrow(msg)**.
565
566```javascript
567import {describe, expect, it, MockKit, when} from '@ohos/hypium';
568
569export default function ActsAbilityTest() {
570 describe('ActsAbilityTest', function () {
571 it('testMockfunc', 0, function () {
572 console.info("it1 begin");
573
574 // 1. Create a MockKit object.
575 let mocker = new MockKit();
576
577 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
578 class ClassName {
579 constructor() {
580 }
581
582 method_1(arg) {
583 return '888888';
584 }
585 }
586
587 let claser = new ClassName();
588
589 // 3. Mock method_1 of the ClassName class.
590 let mockfunc = mocker.mockFunc(claser, claser.method_1);
591
592 // 4. Set the action to be performed when the test case ends. For example, set it to afterReturnNothing(), which returns no value.
593 when(mockfunc)('test').afterThrow('error xxx');
594
595 // 5. Execute the mocked function, capture the exception, and use assertEqual() to check whether message meets the expectation.
596 try {
597 claser.method_1('test');
598 } catch (e) {
599 expect(e).assertEqual('error xxx'); // The execution is successful.
600 }
601 });
602 });
603}
604```
605
606Example 10: Mock asynchronous functions.
607
608```javascript
609import {describe, expect, it, MockKit, when} from '@ohos/hypium';
610
611export default function ActsAbilityTest() {
612 describe('ActsAbilityTest', function () {
613 it('testMockfunc', 0, function () {
614 console.info("it1 begin");
615
616 // 1. Create a MockKit object.
617 let mocker = new MockKit();
618
619 // 2. Define the ClassName class, which contains two functions, and then create a claser object.
620 class ClassName {
621 constructor() {
622 }
623
624 async method_1(arg) {
625 return new Promise((res, rej) => {
626 // Perform asynchronous operations.
627 setTimeout(function () {
628 console.log ('Execute');
629 res('Pass data');
630 }, 2000);
631 });
632 }
633 }
634
635 let claser = new ClassName();
636
637 // 3. Mock method_1 of the ClassName class.
638 let mockfunc = mocker.mockFunc(claser, claser.method_1);
639
640 // 4. Set the action to be performed after the test case ends. For example, set it to afterRetrun(), which returns a custom promise.
641 when(mockfunc)('test').afterReturn(new Promise((res, rej) => {
642 console.log("do something");
643 res('success something');
644 }));
645
646 // 5. Execute the mocked function, that is, execute the promise.
647 claser.method_1('test').then(function (data) {
648 // Code for data processing
649 console.log('result : ' + data);
650 });
651 });
652 });
653}
654```
655
656Example 11: Mock a system function.
657
658```javascript
659export default function ActsAbilityTest() {
660 describe('ActsAbilityTest', function () {
661 it('test_systemApi', 0, function () {
662 // 1. Create a MockKit object.
663 let mocker = new MockKit();
664 // 2. Mock the app.getInfo() function.
665 let mockf = mocker.mockFunc(app, app.getInfo);
666 when(mockf)('test').afterReturn('1');
667 // The operation is successful.
668 expect(app.getInfo('test')).assertEqual('1');
669 });
670 });
671}
672```
673
674
675Example 12: Verify **times(count)**.
676
677```javascript
678import { describe, expect, it, MockKit, when } from '@ohos/hypium'
679
680export default function ActsAbilityTest() {
681 describe('ActsAbilityTest', function () {
682 it('test_verify_times', 0, function () {
683 // 1. Create a MockKit object.
684 let mocker = new MockKit();
685 // 2. Define the class to be mocked.
686 class ClassName {
687 constructor() {
688 }
689
690 method_1(...arg) {
691 return '888888';
692 }
693 }
694 // 3. Create an object of the class.
695 let claser = new ClassName();
696 // 4. Mock a function, for example, method_1, of the object.
697 let func_1 = mocker.mockFunc(claser, claser.method_1);
698 // 5. Set the expected value to be returned by the mocked function.
699 when(func_1)('123').afterReturn('4');
700
701 // 6. Execute the function several times, with parameters set as follows:
702 claser.method_1('123', 'ppp');
703 claser.method_1('abc');
704 claser.method_1('xyz');
705 claser.method_1();
706 claser.method_1('abc', 'xxx', 'yyy');
707 claser.method_1('abc');
708 claser.method_1();
709 // 7. Check whether method_1 with the parameter of 'abc' was executed twice.
710 mocker.verify('method_1', ['abc']).times(2);
711 });
712 });
713}
714```
715
716
717Example 13: Verify **atLeast(count)**.
718
719```javascript
720import { describe, expect, it, MockKit, when } from '@ohos/hypium'
721
722export default function ActsAbilityTest() {
723 describe('ActsAbilityTest', function () {
724 it('test_verify_atLeast', 0, function () {
725 // 1. Create a MockKit object.
726 let mocker = new MockKit();
727 // 2. Define the class to be mocked.
728 class ClassName {
729 constructor() {
730 }
731
732 method_1(...arg) {
733 return '888888';
734 }
735 }
736
737 // 3. Create an object of the class.
738 let claser = new ClassName();
739 // 4. Mock a function, for example, method_1, of the object.
740 let func_1 = mocker.mockFunc(claser, claser.method_1);
741 // 5. Set the expected value to be returned by the mocked function.
742 when(func_1)('123').afterReturn('4');
743 // 6. Execute the function several times, with parameters set as follows:
744 claser.method_1('123', 'ppp');
745 claser.method_1('abc');
746 claser.method_1('xyz');
747 claser.method_1();
748 claser.method_1('abc', 'xxx', 'yyy');
749 claser.method_1();
750 // 7. Check whether method_1 with an empty value was executed at least twice.
751 mocker.verify('method_1', []).atLeast(2);
752 });
753 });
754}
755```
756
757#### Data Driving
758
759##### Constraints
760
761JsUnit provides the following data driving capability since [Hypium 1.0.2](https://repo.harmonyos.com/#/en/application/atomService/@ohos%2Fhypium):
762
763- Passes parameters for the specified test suite and test case.
764- Specifies the number of times that the test suite and test case are executed.
765
766The execution times of test cases and the parameters passed in each time are determined by the settings in **data.json**. The file content is as follows:
767
768>**NOTE**<br>The **data.json** file is in the same directory as the **.test.js** or **.test.ets** file.
769
770```json
771{
772 "suites": [{
773 "describe": ["actsAbilityTest"],
774 "stress": 2,
775 "params": {
776 "suiteParams1": "suiteParams001",
777 "suiteParams2": "suiteParams002"
778 },
779 "items": [{
780 "it": "testDataDriverAsync",
781 "stress": 2,
782 "params": [{
783 "name": "tom",
784 "value": 5
785 }, {
786 "name": "jerry",
787 "value": 4
788 }]
789 }, {
790 "it": "testDataDriver",
791 "stress": 3
792 }]
793 }]
794}
795```
796
797Parameter description:
798
799| | Name| Description | Mandatory|
800| :--- | :--------- | :------------------------------------ | ---- |
801| 1 | "suite" | Test suite configuration. | Yes |
802| 2 | "items" | Test case configuration. | Yes |
803| 3 | "describe" | Test suite name. | Yes |
804| 4 | "it" | Test case name. | Yes |
805| 5 | "params" | Parameters to be passed to the test suite or test case.| No |
806| 6 | "stress" | Number of times that the test suite or test case is executed. | No |
807
808The sample code is as follows:
809
810Import the **data.json** file to the **app.js** or **app.ets** file in the **TestAbility** directory, and set parameters before executing the **Hypium.hypiumTest()** method.
811
812```javascript
813import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
814import { Hypium } from '@ohos/hypium'
815import testsuite from '../test/List.test'
816import data from '../test/data.json';
817
818...
819Hypium.setData(data);
820Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite)
821...
822```
823
824```javascript
825import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium';
826
827export default function abilityTest() {
828 describe('actsAbilityTest', function () {
829 it('testDataDriverAsync', 0, async function (done, data) {
830 console.info('name: ' + data.name);
831 console.info('value: ' + data.value);
832 done();
833 });
834
835 it('testDataDriver', 0, function () {
836 console.info('stress test');
837 });
838 });
839}
840```
841
842### How to Use
843
844JsUnit is released as an npm (hypium) package at the [service component official website](https://repo.harmonyos.com/#/en/application/atomService/@ohos%2Fhypium). You can download Deveco Studio, configure dependencies in the application project, and use JsUnit. For details about how to create a test project and execute test scripts, see the [OpenHarmony Test Framework](https://developer.harmonyos.com/en/docs/documentation/doc-guides/ohos-openharmony-test-framework-0000001267284568).
845
846## UiTest Features
847
848| No. | Feature | Description |
849| ---- | ----------- | ------------------------------------------------------------ |
850| 1 | UiDriver | Provides the UI test entry. It provides APIs for locating a component, checking whether a component exists, and injecting a key.|
851| 2 | By | Describes the attributes (such as text, ID, and type) of UI components. `UiDriver` locates the component based on the attributes described by `By`.|
852| 3 | UiComponent | Provides the UI component object, which provides APIs for obtaining component attributes, clicking a component, and scrolling to search for a component.|
853| 4 | UiWindow | Provides the window object, which provides APIs for obtaining window attributes, dragging a window, and adjusting the window size.|
854
855Import the following to the test script:
856
857```typescript
858import {UiDriver,BY,UiComponent,Uiwindow,MatchPattern} from '@ohos.uitest'
859```
860
861> **NOTICE**<br>
862> - All APIs provided by the `By` class are synchronous. You can use `builder` to call the APIs in chain mode to construct component filtering conditions.
863> - All the APIs provided by the `UiDriver` and `UiComponent` classes are asynchronous (in `Promise` mode), and `await` must be used.
864> - All UI test cases must be written in the asynchronous syntax and comply with the asynchronous test case specifications of JsUnit.
865
866
867
868Import the `By`, `UiDriver`, and `UiComponent` classes to the test case file, and then call the APIs to write test cases.
869
870```javascript
871import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
872import {BY, UiDriver, UiComponent, MatchPattern} from '@ohos.uitest'
873
874export default async function abilityTest() {
875 describe('uiTestDemo', function() {
876 it('uitest_demo0', 0, async function() {
877 // create UiDriver
878 let driver = await UiDriver.create()
879 // find component by text
880 let button = await driver.findComponent(BY.text('hello').enabled(true))
881 // click component
882 await button.click()
883 // get and assert component text
884 let content = await button.getText()
885 expect(content).assertEquals('clicked!')
886 })
887 })
888}
889```
890
891### Using UiDriver
892
893As the main entry to UiTest, the `UiDriver` class provides APIs for component matching/search, key injection, coordinate clicking/swiping, and screenshot.
894
895| No. | API | Description |
896| ---- | ------------------------------------------------------------ | ------------------------ |
897| 1 | create():Promise<UiDriver> | Creates a **UiDriver** object. This API is a static method.|
898| 2 | findComponent(b:By):Promise<UiComponent> | Searches for a component. |
899| 3 | pressBack():Promise<void> | Presses the BACK button. |
900| 4 | click(x:number, y:number):Promise<void> | Clicks a specific point based on the given coordinates. |
901| 5 | swipe(x1:number, y1:number, x2:number, y2:number):Promise<void> | Swipes based on the given coordinates. |
902| 6 | assertComponentExist(b:By):Promise<void> | Asserts that the component matching the given attributes exists on the current page. |
903| 7 | delayMs(t:number):Promise<void> | Delays this **UiDriver** object for the specified duration. |
904| 8 | screenCap(s:path):Promise<void> | Captures the current screen. |
905| 9 | findWindow(filter: WindowFilter): Promise<UiWindow> | Searches for a window. |
906
907**assertComponentExist()** is an assertion API, which is used to assert that the target component exists on the current page. If the component does not exist, a JS exception will be thrown, causing the test case to fail.
908
909```javascript
910import {BY,UiDriver,UiComponent} from '@ohos.uitest'
911
912export default async function abilityTest() {
913 describe('UiTestDemo', function() {
914 it('Uitest_demo0', 0, async function(done) {
915 try{
916 // create UiDriver
917 let driver = await UiDriver.create()
918 // assert text 'hello' exists on current Ui
919 await assertComponentExist(BY.text('hello'))
920 } finally {
921 done()
922 }
923 })
924 })
925}
926```
927
928For details about the APIs of `UiDriver`, see [@ohos.uitest.d.ts](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts) and [UiDriver](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-uitest.md#uidriver).
929
930### Using By
931
932UiTest provides a wide range of UI component feature description APIs in the `By` class to filter and match components. The APIs provided by `By` exhibit the following features:
933
934- Allow one or more attributes as the match conditions. For example, you can specify both the text and id attributes to find a component.
935- Provide a variety of match patterns (`EQUALS`, `CONTAINS`, `STARTS_WITH`, and `ENDS_WITH`) for component attributes.
936- Support relative positioning for components. APIs such as `isBefore` and `isAfter` can be used to specify the features of adjacent components to assist positioning.
937
938| No. | API | Description |
939| ---- | ---------------------------------- | ------------------------------------------------ |
940| 1 | id(i:number):By | Specifies the component ID. |
941| 2 | text(t:string, p?:MatchPattern):By | Specifies the component text. You can specify the match pattern. |
942| 3 | type(t:string)):By | Specifies the component type. |
943| 4 | enabled(e:bool):By | Specifies the component state, which can be enabled or disabled. |
944| 5 | clickable(c:bool):By | Specifies the clickable status of the component. |
945| 6 | focused(f:bool):By | Specifies the focused status of the component. |
946| 7 | scrollable(s:bool):By | Specifies the scrollable status of the component. |
947| 8 | selected(s:bool):By | Specifies the selected status of the component. |
948| 9 | isBefore(b:By):By | Specifies the attributes of the component that locates before the target component.|
949| 10 | isAfter(b:By):By | Specifies the attributes of the component that locates after the target component.|
950
951The `text` attribute supports match patterns `MatchPattern.EQUALS`, `MatchPattern.CONTAINS`, `MatchPattern.STARTS_WITH`, and `MatchPattern.ENDS_WITH`. The default match pattern is `MatchPattern.EQUALS`.
952
953For details about the APIs of `By`, see [@ohos.uitest.d.ts](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts) and [By](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-uitest.md#by).
954
955#### Absolute Positioning of a Component
956
957Example 1: Search for component `Id_button`.
958
959```javascript
960let button = await driver.findComponent(BY.id(Id_button))
961```
962
963 Example 2: Search for component `Id_button` in the `enabled` state. Use this API when the component cannot be located based on a single attribute.
964
965```javascript
966let button = await driver.findComponent(BY.id(Id_button).enabled(true))
967```
968
969Use `By.id(x).enabled(y)` to specify multiple attributes of the target component.
970
971Example 3: Search for the component whose text contains `hello`. Use this API when the component attribute values cannot be completely determined.
972
973```javascript
974let txt = await driver.findComponent(BY.text("hello", MatchPattern.CONTAINS))
975```
976
977`By.text()` passes the second parameter `MatchPattern.CONTAINS` to specify the matching rule. The default rule is `MatchPattern.EQUALS`, that is, the text attribute of the target component must be equal to the specified value.
978
979#### Relative Positioning of a Component
980
981Example 1: Search for the switch component `ResourceTable.Id_switch` following the text component `Item3_3`.
982
983```javascript
984let switch = await driver.findComponent(BY.id(Id_switch).isAfter(BY.text("Item3_3")))
985```
986
987Use `By.isAfter` to specify the attributes of the feature component located before the target component for relative positioning. Generally, a feature component is a component that has a globally unique feature (for example, a unique ID or a unique text).
988
989Similarly, you can use `By.isBefore` to specify the attributes of the feature component located after the target component to implement relative positioning.
990
991### Using UiComponent
992
993The `UiComponent` class represents a UI component, which can be located by using `UiDriver.findComponent(by)`. It provides APIs for obtaining component attributes, clicking a component, scrolling to search for a component, and text injection.
994
995`UiComponent` provides the following APIs:
996
997| No. | API | Description |
998| ---- | --------------------------------- | ---------------------------------------------- |
999| 1 | click():Promise<void> | Clicks the component. |
1000| 2 | inputText(t:string):Promise<void> | Inputs text into the component. This API is applicable to text box components. |
1001| 3 | scrollSearch(s:By):Promise<bool> | Scrolls on this component to search for the target component. This API is applicable to the **List** components.|
1002| 4 | getText():Promise<string> | Obtains the component text. |
1003| 5 | getId():Promise<number> | Obtains the component ID. |
1004| 6 | getType():Promise<string> | Obtains the component type. |
1005| 7 | isEnabled():Promise<bool> | Obtains the component state, which can be enabled or disabled. |
1006
1007For details about the APIs of `UiComponent`, see [@ohos.uitest.d.ts](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts) and [UiComponent](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-uitest.md#uicomponent).
1008
1009Example 1: Click a component.
1010
1011```javascript
1012let button = await driver.findComponent(BY.id(Id_button))
1013await button.click()
1014```
1015
1016Example 2: After obtaining component attributes, use **assert()** to make assertion.
1017
1018```javascript
1019let component = await driver.findComponent(BY.id(Id_title))
1020expect(component !== null).assertTrue()
1021```
1022
1023Example 3: Scroll on the **List** component to locate the child component with text `Item3_3`.
1024
1025```javascript
1026let list = await driver.findComponent(BY.id(Id_list))
1027let found = await list.scrollSearch(BY.text("Item3_3"))
1028expect(found).assertTrue()
1029```
1030
1031Example 4: Input text in a text box.
1032
1033```javascript
1034let editText = await driver.findComponent(BY.type('InputText'))
1035await editText.inputText("user_name")
1036```
1037### Using UiWindow
1038
1039The `UiWindow` class represents a UI window, which can be located by using `UiDriver.findWindow(by)`. You can use the instance provided by this class to obtain window attributes, drag a window, and adjust the window size.
1040
1041`UiWindow` provides the following APIs:
1042
1043| No. | API | Description |
1044| ---- | ------------------------------------------------------------ | -------------------------------------------------- |
1045| 1 | getBundleName(): Promise<string> | Obtains the bundle name of the window. |
1046| 2 | getTitle(): Promise<string> | Obtains the window title. |
1047| 3 | focus(): Promise<bool> | Gives focus to the current window. |
1048| 4 | moveTo(x: number, y: number): Promise<bool>; | Moves the current window to the specified position. This API is applicable to the windows that can be moved.|
1049| 5 | resize(wide: number, height: number, direction: ResizeDirection): Promise<bool> | Adjusts the window size. This API is applicable to the windows that can be resized. |
1050| 6 | split(): Promise<bool> | Splits the current window. This API is applicable to the windows that support split-screen mode. |
1051| 7 | close(): Promise<bool> | Closes the current window. |
1052
1053For details about the APIs of `UiWindow`, see [@ohos.uitest.d.ts](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts) and [UiWindow](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-uitest.md#uiwindow9).
1054
1055Example 1: Obtain the window attributes.
1056
1057```javascript
1058let window = await driver.findWindow({actived: true})
1059let bundelName = await window.getBundleName()
1060```
1061
1062Example 2: Move the window.
1063
1064```javascript
1065let window = await driver.findWindow({actived: true})
1066await window.moveTo(500,500)
1067```
1068
1069Example 3: Close the window.
1070
1071```javascript
1072let window = await driver.findWindow({actived: true})
1073await window.close()
1074```
1075
1076### How to Use
1077
1078 Download Deveco Studio, create a test project, and call the APIs provided by UiTest to perform related tests. For details about how to create a test project and execute test scripts, see [OpenHarmony Test Framework](https://developer.harmonyos.com/en/docs/documentation/doc-guides/ohos-openharmony-test-framework-0000001267284568).
1079 Run the following command to enable UiTest:
1080
1081>```shell
1082> hdc_std shell param set persist.ace.testmode.enabled 1
1083>```
1084### Building UiTest
1085
1086> UiTest is not built with OpenHarmony 3.1 Release and needs to be manually built.
1087
1088If you want to modify UiTest code and verify the modification, use the following commands.
1089
1090#### Building UiTest
1091
1092```shell
1093./build.sh --product-name rk3568 --build-target uitestkit
1094```
1095#### Sending UiTest
1096
1097```shell
1098hdc_std target mount
1099hdc_std shell mount -o rw,remount /
1100hdc_std file send uitest /system/bin/uitest
1101hdc_std file send libuitest.z.so /system/lib/module/libuitest.z.so
1102hdc_std shell chmod +x /system/bin/uitest
1103```
1104
1105### Version Information
1106
1107| Version | Description |
1108| :------ | :----------------------------------------------------------- |
1109| 3.2.2.1 | 1. Added the APIs for obtaining and setting the screen orientation and flinging.<br>2. Added the window processing logic for unsupported scenarios. |
1110
README_zh.md
1# 自动化测试框架使用介绍
2
3## 简介
4 OpenHarmony自动化测试框架代码部件仓arkXtest,包含单元测试框架(JsUnit)和Ui测试框架(UiTest)。
5
6 单元测试框架(JsUnit)提供单元测试用例执行能力,提供用例编写基础接口,生成对应报告,用于测试系统或应用接口。
7
8 Ui测试框架(UiTest)通过简洁易用的API提供查找和操作界面控件能力,支持用户开发基于界面操作的自动化测试脚本。
9
10## 目录
11
12```
13arkXtest
14 |-----jsunit 单元测试框架
15 |-----uitest Ui测试框架
16```
17## 约束限制
18本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
19
20## 单元测试框架功能特性
21
22| No. | 特性 | 功能说明 |
23| ---- | -------- | ------------------------------------------------------------ |
24| 1 | 基础流程 | 支持编写及异步执行基础用例。 |
25| 2 | 断言库 | 判断用例实际期望值与预期值是否相符。 |
26| 3 | Mock能力 | 支持函数级mock能力,对定义的函数进行mock后修改函数的行为,使其返回指定的值或者执行某种动作。 |
27| 4 | 数据驱动 | 提供数据驱动能力,支持复用同一个测试脚本,使用不同输入数据驱动执行。 |
28| 5 | 专项能力 | 支持测试套与用例筛选、随机执行、压力测试、超时设置、遇错即停模式,跳过,支持测试套嵌套等。 |
29
30### 使用说明
31
32#### 基础流程
33
34测试用例采用业内通用语法,describe代表一个测试套, it代表一条用例。
35
36| No. | API | 功能说明 |
37|-----| ----------------- |------------------------------------------------------------------------|
38| 1 | describe | 定义一个测试套,支持两个参数:测试套名称和测试套函数。 |
39| 2 | beforeAll | 在测试套内定义一个预置条件,在所有测试用例开始前执行且仅执行一次,支持一个参数:预置动作函数。 |
40| 3 | beforeEach | 在测试套内定义一个单元预置条件,在每条测试用例开始前执行,执行次数与it定义的测试用例数一致,支持一个参数:预置动作函数。 |
41| 4 | afterEach | 在测试套内定义一个单元清理条件,在每条测试用例结束后执行,执行次数与it定义的测试用例数一致,支持一个参数:清理动作函数。 |
42| 5 | afterAll | 在测试套内定义一个清理条件,在所有测试用例结束后执行且仅执行一次,支持一个参数:清理动作函数。 |
43| 6 | beforeItSpecified | @since1.0.15在测试套内定义一个单元预置条件,仅在指定测试用例开始前执行,支持两个参数:单个用例名称或用例名称数组、预置动作函数。 |
44| 7 | afterItSpecified | @since1.0.15在测试套内定义一个单元清理条件,仅在指定测试用例结束后执行,支持两个参数:单个用例名称或用例名称数组、清理动作函数 |
45| 8 | it | 定义一条测试用例,支持三个参数:用例名称,过滤参数和用例函数。 |
46| 9 | expect | 支持bool类型判断等多种断言方法。 |
47| 10 | xdescribe | @since1.0.17定义一个跳过的测试套,支持两个参数:测试套名称和测试套函数。 |
48| 11 | xit | @since1.0.17定义一条跳过的测试用例,支持三个参数:用例名称,过滤参数和用例函数。 |
49
50
51beforeItSpecified, afterItSpecified 示例代码:
52
53```javascript
54import { describe, it, expect, beforeItSpecified, afterItSpecified } from '@ohos/hypium';
55export default function beforeItSpecifiedTest() {
56 describe('beforeItSpecifiedTest', () => {
57 beforeItSpecified(['String_assertContain_success'], () => {
58 const num:number = 1;
59 expect(num).assertEqual(1);
60 })
61 afterItSpecified(['String_assertContain_success'], async (done: Function) => {
62 const str:string = 'abc';
63 setTimeout(()=>{
64 try {
65 expect(str).assertContain('b');
66 } catch (error) {
67 console.error(`error message ${JSON.stringify(error)}`);
68 }
69 done();
70 }, 1000)
71 })
72 it('String_assertContain_success', 0, () => {
73 let a: string = 'abc';
74 let b: string = 'b';
75 expect(a).assertContain(b);
76 expect(a).assertEqual(a);
77 })
78 })
79}
80```
81
82#### 断言库
83
84##### 断言功能列表
85
86
87| No. | API | 功能说明 |
88| :--- | :------------------| ------------------------------------------------------------ |
89| 1 | assertClose | 检验actualvalue和expectvalue(0)的接近程度是否是expectValue(1)。 |
90| 2 | assertContain | 检验actualvalue中是否包含expectvalue。 |
91| 3 | assertEqual | 检验actualvalue是否等于expectvalue[0]。 |
92| 4 | assertFail | 抛出一个错误。 |
93| 5 | assertFalse | 检验actualvalue是否是false。 |
94| 6 | assertTrue | 检验actualvalue是否是true。 |
95| 7 | assertInstanceOf | 检验actualvalue是否是expectvalue类型,支持基础类型。 |
96| 8 | assertLarger | 检验actualvalue是否大于expectvalue。 |
97| 9 | assertLess | 检验actualvalue是否小于expectvalue。 |
98| 10 | assertNull | 检验actualvalue是否是null。 |
99| 11 | assertThrowError | 检验actualvalue抛出Error内容是否是expectValue。 |
100| 12 | assertUndefined | 检验actualvalue是否是undefined。 |
101| 13 | assertNaN | @since1.0.4 检验actualvalue是否是一个NAN |
102| 14 | assertNegUnlimited | @since1.0.4 检验actualvalue是否等于Number.NEGATIVE_INFINITY |
103| 15 | assertPosUnlimited | @since1.0.4 检验actualvalue是否等于Number.POSITIVE_INFINITY |
104| 16 | assertDeepEquals | @since1.0.4 检验actualvalue和expectvalue是否完全相等 |
105| 17 | assertPromiseIsPending | @since1.0.4 判断promise是否处于Pending状态。 |
106| 18 | assertPromiseIsRejected | @since1.0.4 判断promise是否处于Rejected状态。 |
107| 19 | assertPromiseIsRejectedWith | @since1.0.4 判断promise是否处于Rejected状态,并且比较执行的结果值。 |
108| 20 | assertPromiseIsRejectedWithError | @since1.0.4 判断promise是否处于Rejected状态并有异常,同时比较异常的类型和message值。 |
109| 21 | assertPromiseIsResolved | @since1.0.4 判断promise是否处于Resolved状态。 |
110| 22 | assertPromiseIsResolvedWith | @since1.0.4 判断promise是否处于Resolved状态,并且比较执行的结果值。 |
111| 23 | not | @since1.0.4 断言取反,支持上面所有的断言功能 |
112| 24 | message | @since1.0.17自定义断言异常信息 |
113
114expect断言示例代码:
115
116```javascript
117import { describe, it, expect } from '@ohos/hypium';
118
119export default function expectTest() {
120 describe('expectTest', () => {
121 it('assertBeClose_success', 0, () => {
122 let a: number = 100;
123 let b: number = 0.1;
124 expect(a).assertClose(99, b);
125 })
126 it('assertInstanceOf_success', 0, () => {
127 let a: string = 'strTest';
128 expect(a).assertInstanceOf('String');
129 })
130 it('assertNaN_success', 0, () => {
131 expect(Number.NaN).assertNaN(); // true
132 })
133 it('assertNegUnlimited_success', 0, () => {
134 expect(Number.NEGATIVE_INFINITY).assertNegUnlimited(); // true
135 })
136 it('assertPosUnlimited_success', 0, () => {
137 expect(Number.POSITIVE_INFINITY).assertPosUnlimited(); // true
138 })
139 it('not_number_true', 0, () => {
140 expect(1).not().assertLargerOrEqual(2);
141 })
142 it('not_number_true_1', 0, () => {
143 expect(3).not().assertLessOrEqual(2);
144 })
145 it('not_NaN_true', 0, () => {
146 expect(3).not().assertNaN();
147 })
148 it('not_contain_true', 0, () => {
149 let a: string = "abc";
150 let b: string = "cdf";
151 expect(a).not().assertContain(b);
152 })
153 it('not_large_true', 0, () => {
154 expect(3).not().assertLarger(4);
155 })
156 it('not_less_true', 0, () => {
157 expect(3).not().assertLess(2);
158 })
159 it('not_undefined_true', 0, () => {
160 expect(3).not().assertUndefined();
161 })
162 it('deepEquals_null_true', 0, () => {
163 // Defines a variety of assertion methods, which are used to declare expected boolean conditions.
164 expect(null).assertDeepEquals(null);
165 })
166 it('deepEquals_array_not_have_true', 0, () => {
167 // Defines a variety of assertion methods, which are used to declare expected boolean conditions.
168 const a: Array<number> = [];
169 const b: Array<number> = [];
170 expect(a).assertDeepEquals(b);
171 })
172 it('deepEquals_map_equal_length_success', 0, () => {
173 // Defines a variety of assertion methods, which are used to declare expected boolean conditions.
174 const a: Map<number, number> = new Map();
175 const b: Map<number, number> = new Map();
176 a.set(1, 100);
177 a.set(2, 200);
178 b.set(1, 100);
179 b.set(2, 200);
180 expect(a).assertDeepEquals(b);
181 })
182 it("deepEquals_obj_success_1", 0, () => {
183 const a: SampleTest = {x: 1};
184 const b: SampleTest = {x: 1};
185 expect(a).assertDeepEquals(b);
186 })
187 it("deepEquals_regExp_success_0", 0, () => {
188 const a: RegExp = new RegExp("/test/");
189 const b: RegExp = new RegExp("/test/");
190 expect(a).assertDeepEquals(b);
191 })
192 it('test_isPending_pass_1', 0, () => {
193 let p: Promise<void> = new Promise<void>(() => {});
194 expect(p).assertPromiseIsPending();
195 })
196 it('test_isRejected_pass_1', 0, () => {
197 let info: PromiseInfo = {res: "no"};
198 let p: Promise<PromiseInfo> = Promise.reject(info);
199 expect(p).assertPromiseIsRejected();
200 })
201 it('test_isRejectedWith_pass_1', 0, () => {
202 let info: PromiseInfo = {res: "reject value"};
203 let p: Promise<PromiseInfo> = Promise.reject(info);
204 expect(p).assertPromiseIsRejectedWith(info);
205 })
206 it('test_isRejectedWithError_pass_1', 0, () => {
207 let p1: Promise<TypeError> = Promise.reject(new TypeError('number'));
208 expect(p1).assertPromiseIsRejectedWithError(TypeError);
209 })
210 it('test_isResolved_pass_1', 0, () => {
211 let info: PromiseInfo = {res: "result value"};
212 let p: Promise<PromiseInfo> = Promise.resolve(info);
213 expect(p).assertPromiseIsResolved();
214 })
215 it('test_isResolvedTo_pass_1', 0, () => {
216 let info: PromiseInfo = {res: "result value"};
217 let p: Promise<PromiseInfo> = Promise.resolve(info);
218 expect(p).assertPromiseIsResolvedWith(info);
219 })
220 it("test_message", 0, () => {
221 expect(1).message('1 is not equal 2!').assertEqual(2); // fail
222 })
223 })
224}
225
226interface SampleTest {
227 x: number;
228}
229
230interface PromiseInfo {
231 res: string;
232}
233```
234
235##### 自定义断言@since1.0.18
236
237示例代码:
238
239```javascript
240import { describe, Assert, beforeAll, expect, Hypium, it } from '@ohos/hypium';
241
242// custom.ets
243interface customAssert extends Assert {
244 // 自定义断言声明
245 myAssertEqual(expectValue: boolean): void;
246}
247
248//自定义断言实现
249let myAssertEqual = (actualValue: boolean, expectValue: boolean) => {
250 interface R {
251 pass: boolean,
252 message: string
253}
254
255let result: R = {
256 pass: true,
257 message: 'just is a msg'
258}
259
260let compare = () => {
261 if (expectValue === actualValue) {
262 result.pass = true;
263 result.message = '';
264 } else {
265 result.pass = false;
266 result.message = 'expectValue !== actualValue!';
267 }
268 return result;
269}
270result = compare();
271return result;
272}
273
274export default function customAssertTest() {
275 describe('customAssertTest', () => {
276 beforeAll(() => {
277 //注册自定义断言,只有先注册才可以使用
278 Hypium.registerAssert(myAssertEqual);
279 })
280 it('assertContain1', 0, () => {
281 let a = true;
282 let b = true;
283 (expect(a) as customAssert).myAssertEqual(b);
284 Hypium.unregisterAssert(myAssertEqual);
285 })
286 it('assertContain2', 0, () => {
287 Hypium.registerAssert(myAssertEqual);
288 let a = true;
289 let b = true;
290 (expect(a) as customAssert).myAssertEqual(b);
291 // 注销自定义断言,注销以后就无法使用
292 Hypium.unregisterAssert(myAssertEqual);
293 try {
294 (expect(a) as customAssert).myAssertEqual(b);
295 }catch(e) {
296 expect(e.message).assertEqual("myAssertEqual is unregistered");
297 }
298 })
299 })
300}
301```
302
303#### Mock能力
304
305##### 约束限制
306
307单元测试框架Mock能力从npm包[1.0.1版本](https://ohpm.openharmony.cn/#/cn/detail/@ohos%2Fhypium)开始支持,需修改源码工程中package.info中配置依赖npm包版本号后使用。
308> - 仅支持mock自定义对象,不支持mock系统API对象。
309> - 不支持mock对象的私有函数。
310- **接口列表:**
311
312| No. | API | 功能说明 |
313| --- | --- | --- |
314| 1 | mockFunc(obj: object, f:function()) | mock某个类的对象obj的函数f,那么需要传两个参数:obj和f,支持使用异步函数(说明:对mock而言原函数实现是同步或异步没太多区别,因为mock并不关注原函数的实现)。 |
315| 2 | when(mockedfunc:function) | 对传入后方法做检查,检查是否被mock并标记过,返回的是一个方法声明。 |
316| 3 | afterReturn(x:value) | 设定预期返回一个自定义的值value,比如某个字符串或者一个promise。 |
317| 4 | afterReturnNothing() | 设定预期没有返回值,即 undefined。 |
318| 5 | afterAction(x:action) | 设定预期返回一个函数执行的操作。 |
319| 6 | afterThrow(x:msg) | 设定预期抛出异常,并指定异常msg。 |
320| 7 | clear() | 用例执行完毕后,进行数据mocker对象的还原处理(还原之后对象恢复被mock之前的功能)。 |
321| 8 | any | 设定用户传任何类型参数(undefined和null除外),执行的结果都是预期的值,使用ArgumentMatchers.any方式调用。 |
322| 9 | anyString | 设定用户传任何字符串参数,执行的结果都是预期的值,使用ArgumentMatchers.anyString方式调用。 |
323| 10 | anyBoolean | 设定用户传任何boolean类型参数,执行的结果都是预期的值,使用ArgumentMatchers.anyBoolean方式调用。 |
324| 11 | anyFunction | 设定用户传任何function类型参数,执行的结果都是预期的值,使用ArgumentMatchers.anyFunction方式调用。 |
325| 12 | anyNumber | 设定用户传任何数字类型参数,执行的结果都是预期的值,使用ArgumentMatchers.anyNumber方式调用。 |
326| 13 | anyObj | 设定用户传任何对象类型参数,执行的结果都是预期的值,使用ArgumentMatchers.anyObj方式调用。 |
327| 14 | matchRegexs(Regex) | 设定用户传任何正则表达式类型参数Regex,执行的结果都是预期的值,使用ArgumentMatchers.matchRegexs(Regex)方式调用。 |
328| 15 | verify(methodName, argsArray) | 验证methodName(函数名字符串)所对应的函数和其参数列表argsArray的执行行为是否符合预期,返回一个VerificationMode:一个提供验证模式的类,它有times(count)、once()、atLeast(x)、atMost(x)、never()等函数可供选择。 |
329| 16 | times(count) | 验证行为调用过count次。 |
330| 17 | once() | 验证行为调用过一次。 |
331| 18 | atLeast(count) | 验证行为至少调用过count次。 |
332| 19 | atMost(count) | 验证行为至多调用过count次。 |
333| 20 | never | 验证行为从未发生过。 |
334| 21 | ignoreMock(obj, method) | 使用ignoreMock可以还原obj对象中被mock后的函数,对被mock后的函数有效。 |
335| 22 | clearAll() | 用例执行完毕后,进行数据和内存清理。 |
336
337- **使用示例:**
338
339用户可以通过以下方式进行引入mock模块进行测试用例编写:
340
341- **须知:**
342使用时候必须引入的mock能力模块: MockKit,when
343根据自己用例需要引入断言能力api
344例如:`import { describe, expect, it, MockKit, when} from '@ohos/hypium'`
345
346**示例1: afterReturn 的使用**
347
348```javascript
349import { describe, expect, it, MockKit, when } from '@ohos/hypium';
350
351class ClassName {
352 constructor() {
353 }
354
355 method_1(arg: string) {
356 return '888888';
357 }
358
359 method_2(arg: string) {
360 return '999999';
361 }
362}
363export default function afterReturnTest() {
364 describe('afterReturnTest', () => {
365 it('afterReturnTest', 0, () => {
366 console.info("it1 begin");
367 // 1.创建一个mock能力的对象MockKit
368 let mocker: MockKit = new MockKit();
369 // 2.定类ClassName,里面两个函数,然后创建一个对象claser
370 let claser: ClassName = new ClassName();
371 // 3.进行mock操作,比如需要对ClassName类的method_1函数进行mock
372 let mockfunc: Function = mocker.mockFunc(claser, claser.method_1);
373 // 4.期望claser.method_1函数被mock后, 以'test'为入参时调用函数返回结果'1'
374 when(mockfunc)('test').afterReturn('1');
375 // 5.对mock后的函数进行断言,看是否符合预期
376 // 执行成功案例,参数为'test'
377 expect(claser.method_1('test')).assertEqual('1'); // 执行通过
378 })
379 })
380}
381```
382- **须知:**
383`when(mockfunc)('test').afterReturn('1');`
384这句代码中的`('test')`是mock后的函数需要传递的匹配参数,目前支持一个参数
385`afterReturn('1')`是用户需要预期返回的结果。
386有且只有在参数是`('test')`的时候,执行的结果才是用户自定义的预期结果。
387
388**示例2: afterReturnNothing 的使用**
389
390```javascript
391import { describe, expect, it, MockKit, when } from '@ohos/hypium';
392
393class ClassName {
394 constructor() {
395 }
396
397 method_1(arg: string) {
398 return '888888';
399 }
400
401 method_2(arg: string) {
402 return '999999';
403 }
404}
405export default function afterReturnNothingTest() {
406 describe('afterReturnNothingTest', () => {
407 it('testMockfunc', 0, () => {
408 console.info("it1 begin");
409 // 1.创建一个mock能力的对象MockKit
410 let mocker: MockKit = new MockKit();
411 // 2.定类ClassName,里面两个函数,然后创建一个对象claser
412 let claser: ClassName = new ClassName();
413 // 3.进行mock操作,比如需要对ClassName类的method_1函数进行mock
414 let mockfunc: Function = mocker.mockFunc(claser, claser.method_1);
415 // 4.期望claser.method_1函数被mock后, 以'test'为入参时调用函数返回结果undefined
416 when(mockfunc)('test').afterReturnNothing();
417 // 5.对mock后的函数进行断言,看是否符合预期,注意选择跟第4步中对应的断言方法
418 // 执行成功案例,参数为'test',这时候执行原对象claser.method_1的方法,会发生变化
419 // 这时候执行的claser.method_1不会再返回'888888',而是设定的afterReturnNothing()生效// 不返回任何值;
420 expect(claser.method_1('test')).assertUndefined(); // 执行通过
421 })
422 })
423}
424```
425
426**示例3: 设定参数类型为any ,即接受任何参数(undefine和null除外)的使用**
427
428
429- **须知:**
430需要引入ArgumentMatchers类,即参数匹配器,例如:ArgumentMatchers.any
431
432```javascript
433import { describe, expect, it, MockKit, when, ArgumentMatchers } from '@ohos/hypium';
434
435class ClassName {
436 constructor() {
437 }
438
439 method_1(arg: string) {
440 return '888888';
441 }
442
443 method_2(arg: string) {
444 return '999999';
445 }
446}
447export default function argumentMatchersAnyTest() {
448 describe('argumentMatchersAnyTest', () => {
449 it('testMockfunc', 0, () => {
450 console.info("it1 begin");
451 // 1.创建一个mock能力的对象MockKit
452 let mocker: MockKit = new MockKit();
453 // 2.定类ClassName,里面两个函数,然后创建一个对象claser
454 let claser: ClassName = new ClassName();
455 // 3.进行mock操作,比如需要对ClassName类的method_1函数进行mock
456 let mockfunc: Function = mocker.mockFunc(claser, claser.method_1);
457 // 4.期望claser.method_1函数被mock后, 以任何参数调用函数时返回结果'1'
458 when(mockfunc)(ArgumentMatchers.any).afterReturn('1');
459 // 5.对mock后的函数进行断言,看是否符合预期,注意选择跟第4步中对应的断言方法
460 // 执行成功的案例1,传参为字符串类型
461 expect(claser.method_1('test')).assertEqual('1'); // 用例执行通过。
462 // 执行成功的案例2,传参为数字类型123
463 expect(claser.method_1("123")).assertEqual('1');// 用例执行通过。
464 // 执行成功的案例3,传参为boolean类型true
465 expect(claser.method_1("true")).assertEqual('1');// 用例执行通过。
466 })
467 })
468}
469```
470
471**示例4: 设定参数类型ArgumentMatchers的使用**
472
473```javascript
474import { describe, expect, it, MockKit, when, ArgumentMatchers } from '@ohos/hypium';
475
476class ClassName {
477 constructor() {
478 }
479
480 method_1(arg: string) {
481 return '888888';
482 }
483
484 method_2(arg: string) {
485 return '999999';
486 }
487}
488export default function argumentMatchersTest() {
489 describe('argumentMatchersTest', () => {
490 it('testMockfunc', 0, () => {
491 console.info("it1 begin");
492 // 1.创建一个mock能力的对象MockKit
493 let mocker: MockKit = new MockKit();
494 // 2.定类ClassName,里面两个函数,然后创建一个对象claser
495 let claser: ClassName = new ClassName();
496 // 3.进行mock操作,比如需要对ClassName类的method_1函数进行mock
497 let mockfunc: Function = mocker.mockFunc(claser, claser.method_1);
498 // 4.期望claser.method_1函数被mock后, 以任何string类型为参数调用函数时返回结果'1'
499 when(mockfunc)(ArgumentMatchers.anyString).afterReturn('1');
500 // 4.对mock后的函数进行断言,看是否符合预期,注意选择跟第4步中对应的断言方法
501 // 执行成功的案例,传参为字符串类型
502 expect(claser.method_1('test')).assertEqual('1'); // 用例执行通过。
503 expect(claser.method_1('abc')).assertEqual('1'); // 用例执行通过。
504 })
505 })
506}
507```
508
509**示例5: 设定参数类型为matchRegexs(Regex)等 的使用**
510```javascript
511import { describe, expect, it, MockKit, when, ArgumentMatchers } from '@ohos/hypium';
512
513class ClassName {
514 constructor() {
515 }
516
517 method_1(arg: string) {
518 return '888888';
519 }
520
521 method_2(arg: string) {
522 return '999999';
523 }
524}
525export default function matchRegexsTest() {
526 describe('matchRegexsTest', () => {
527 it('testMockfunc', 0, () => {
528 console.info("it1 begin");
529 // 1.创建一个mock能力的对象MockKit
530 let mocker: MockKit = new MockKit();
531 let claser: ClassName = new ClassName();
532 // 2.进行mock操作,比如需要对ClassName类的method_1函数进行mock
533 let mockfunc: Function = mocker.mockFunc(claser, claser.method_1);
534 // 3.期望claser.method_1函数被mock后, 以"test"为入参调用函数时返回结果'1'
535 when(mockfunc)(ArgumentMatchers.matchRegexs(new RegExp("test"))).afterReturn('1');
536 // 4.对mock后的函数进行断言,看是否符合预期,注意选择跟第4步中对应的断言方法
537 // 执行成功的案例,传参为字符串类型
538 expect(claser.method_1('test')).assertEqual('1'); // 用例执行通过。
539 })
540 })
541}
542```
543
544**示例6: 验证功能 Verify函数的使用**
545```javascript
546import { describe, it, MockKit } from '@ohos/hypium';
547
548class ClassName {
549 constructor() {
550 }
551
552 method_1(...arg: string[]) {
553 return '888888';
554 }
555
556 method_2(...arg: string[]) {
557 return '999999';
558 }
559}
560export default function verifyTest() {
561 describe('verifyTest', () => {
562 it('testMockfunc', 0, () => {
563 console.info("it1 begin");
564 // 1.创建一个mock能力的对象MockKit
565 let mocker: MockKit = new MockKit();
566 // 2.然后创建一个对象claser
567 let claser: ClassName = new ClassName();
568 // 3.进行mock操作,比如需要对ClassName类的method_1和method_2两个函数进行mock
569 mocker.mockFunc(claser, claser.method_1);
570 mocker.mockFunc(claser, claser.method_2);
571 // 4.方法调用如下
572 claser.method_1('abc', 'ppp');
573 claser.method_1('abc');
574 claser.method_1('xyz');
575 claser.method_1();
576 claser.method_1('abc', 'xxx', 'yyy');
577 claser.method_1();
578 claser.method_2('111');
579 claser.method_2('111', '222');
580 // 5.现在对mock后的两个函数进行验证,验证method_2,参数为'111'执行过一次
581 mocker.verify('method_2',['111']).once(); // 执行success
582 })
583 })
584}
585```
586
587**示例7: ignoreMock(obj, method) 忽略函数的使用**
588```javascript
589import { describe, expect, it, MockKit, when, ArgumentMatchers } from '@ohos/hypium';
590
591class ClassName {
592 constructor() {
593 }
594
595 method_1(...arg: number[]) {
596 return '888888';
597 }
598
599 method_2(...arg: number[]) {
600 return '999999';
601 }
602}
603export default function ignoreMockTest() {
604 describe('ignoreMockTest', () => {
605 it('testMockfunc', 0, () => {
606 console.info("it1 begin");
607 // 1.创建一个mock能力的对象MockKit
608 let mocker: MockKit = new MockKit();
609 // 2.创建一个对象claser
610 let claser: ClassName = new ClassName();
611 // 3.进行mock操作,比如需要对ClassName类的method_1和method_2两个函数进行mock
612 let func_1: Function = mocker.mockFunc(claser, claser.method_1);
613 let func_2: Function = mocker.mockFunc(claser, claser.method_2);
614 // 4.期望claser.method_1函数被mock后, 以number类型为入参时调用函数返回结果'4'
615 when(func_1)(ArgumentMatchers.anyNumber).afterReturn('4');
616 // 4.期望claser.method_2函数被mock后, 以number类型为入参时调用函数返回结果'5'
617 when(func_2)(ArgumentMatchers.anyNumber).afterReturn('5');
618 // 5.方法调用如下
619 expect(claser.method_1(123)).assertEqual("4");
620 expect(claser.method_2(456)).assertEqual("5");
621 // 6.现在对mock后的两个函数的其中一个函数method_1进行忽略处理(原理是就是还原)
622 mocker.ignoreMock(claser, claser.method_1);
623 // 7.然后再去调用 claser.method_1函数,用断言测试結果
624 expect(claser.method_1(123)).assertEqual('888888');
625 })
626 })
627}
628```
629
630**示例8: clear(obj)函数的使用**
631
632```javascript
633import { describe, expect, it, MockKit, when, ArgumentMatchers } from '@ohos/hypium';
634
635class ClassName {
636 constructor() {
637 }
638
639 method_1(...arg: number[]) {
640 return '888888';
641 }
642
643 method_2(...arg: number[]) {
644 return '999999';
645 }
646}
647export default function clearTest() {
648 describe('clearTest', () => {
649 it('testMockfunc', 0, () => {
650 console.info("it1 begin");
651 // 1.创建一个mock能力的对象MockKit
652 let mocker: MockKit = new MockKit();
653 // 2.创建一个对象claser
654 let claser: ClassName = new ClassName();
655 // 3.进行mock操作,比如需要对ClassName类的method_1和method_2两个函数进行mock
656 let func_1: Function = mocker.mockFunc(claser, claser.method_1);
657 let func_2: Function = mocker.mockFunc(claser, claser.method_2);
658 // 4.期望claser.method_1函数被mock后, 以任何number类型为参数调用函数时返回结果'4'
659 when(func_1)(ArgumentMatchers.anyNumber).afterReturn('4');
660 // 4.期望claser.method_2函数被mock后, 以任何number类型为参数调用函数时返回结果'5'
661 when(func_2)(ArgumentMatchers.anyNumber).afterReturn('5');
662 // 5.方法调用如下
663 expect(claser.method_1(123)).assertEqual('4');
664 expect(claser.method_2(123)).assertEqual('5');
665 // 6.清除obj上所有的mock能力(原理是就是还原)
666 mocker.clear(claser);
667 // 7.然后再去调用 claser.method_1,claser.method_2 函数,测试结果
668 expect(claser.method_1(123)).assertEqual('888888');
669 expect(claser.method_2(123)).assertEqual('999999');
670 })
671 })
672}
673```
674
675**示例9: afterThrow(msg)函数的使用**
676
677```javascript
678import { describe, expect, it, MockKit, when } from '@ohos/hypium';
679
680class ClassName {
681 constructor() {
682 }
683
684 method_1(arg: string) {
685 return '888888';
686 }
687}
688export default function afterThrowTest() {
689 describe('afterThrowTest', () => {
690 it('testMockfunc', 0, () => {
691 console.info("it1 begin");
692 // 1.创建一个mock能力的对象MockKit
693 let mocker: MockKit = new MockKit();
694 // 2.创建一个对象claser
695 let claser: ClassName = new ClassName();
696 // 3.进行mock操作,比如需要对ClassName类的method_1函数进行mock
697 let mockfunc: Function = mocker.mockFunc(claser, claser.method_1);
698 // 4.期望claser.method_1函数被mock后, 以'test'为参数调用函数时抛出error xxx异常
699 when(mockfunc)('test').afterThrow('error xxx');
700 // 5.执行mock后的函数,捕捉异常并使用assertEqual对比msg否符合预期
701 try {
702 claser.method_1('test');
703 } catch (e) {
704 expect(e).assertEqual('error xxx'); // 执行通过
705 }
706 })
707 })
708}
709```
710
711**示例10: mock异步返回promise对象的使用**
712
713```javascript
714import { describe, expect, it, MockKit, when } from '@ohos/hypium';
715
716class ClassName {
717 constructor() {
718 }
719
720 async method_1(arg: string) {
721 return new Promise<string>((resolve: Function, reject: Function) => {
722 setTimeout(() => {
723 console.log('执行');
724 resolve('数据传递');
725 }, 2000);
726 });
727 }
728}
729export default function mockPromiseTest() {
730 describe('mockPromiseTest', () => {
731 it('testMockfunc', 0, async (done: Function) => {
732 console.info("it1 begin");
733 // 1.创建一个mock能力的对象MockKit
734 let mocker: MockKit = new MockKit();
735 // 2.创建一个对象claser
736 let claser: ClassName = new ClassName();
737 // 3.进行mock操作,比如需要对ClassName类的method_1函数进行mock
738 let mockfunc: Function = mocker.mockFunc(claser, claser.method_1);
739 // 4.期望claser.method_1函数被mock后, 以'test'为参数调用函数时返回一个promise对象
740 when(mockfunc)('test').afterReturn(new Promise<string>((resolve: Function, reject: Function) => {
741 console.log("do something");
742 resolve('success something');
743 }));
744 // 5.执行mock后的函数,即对定义的promise进行后续执行
745 let result = await claser.method_1('test');
746 expect(result).assertEqual("success something");
747 done();
748 })
749 })
750}
751```
752
753**示例11:verify times函数的使用(验证函数调用次数)**
754
755```javascript
756import { describe, it, MockKit, when } from '@ohos/hypium'
757
758class ClassName {
759 constructor() {
760 }
761
762 method_1(...arg: string[]) {
763 return '888888';
764 }
765}
766export default function verifyTimesTest() {
767 describe('verifyTimesTest', () => {
768 it('test_verify_times', 0, () => {
769 // 1.创建MockKit对象
770 let mocker: MockKit = new MockKit();
771 // 2.创建类对象
772 let claser: ClassName = new ClassName();
773 // 3.mock 类ClassName对象的某个方法,比如method_1
774 let func_1: Function = mocker.mockFunc(claser, claser.method_1);
775 // 4.期望被mock后的函数返回结果'4'
776 when(func_1)('123').afterReturn('4');
777 // 5.随机执行几次函数,参数如下
778 claser.method_1('123', 'ppp');
779 claser.method_1('abc');
780 claser.method_1('xyz');
781 claser.method_1();
782 claser.method_1('abc', 'xxx', 'yyy');
783 claser.method_1('abc');
784 claser.method_1();
785 // 6.验证函数method_1且参数为'abc'时,执行过的次数是否为2
786 mocker.verify('method_1', ['abc']).times(2);
787 })
788 })
789}
790```
791
792
793**示例12:verify atLeast函数的使用(验证函数调用次数)**
794
795```javascript
796import { describe, it, MockKit, when } from '@ohos/hypium'
797
798class ClassName {
799 constructor() {
800 }
801
802 method_1(...arg: string[]) {
803 return '888888';
804 }
805}
806export default function verifyAtLeastTest() {
807 describe('verifyAtLeastTest', () => {
808 it('test_verify_atLeast', 0, () => {
809 // 1.创建MockKit对象
810 let mocker: MockKit = new MockKit();
811 // 2.创建类对象
812 let claser: ClassName = new ClassName();
813 // 3.mock 类ClassName对象的某个方法,比如method_1
814 let func_1: Function = mocker.mockFunc(claser, claser.method_1);
815 // 4.期望被mock后的函数返回结果'4'
816 when(func_1)('123').afterReturn('4');
817 // 5.随机执行几次函数,参数如下
818 claser.method_1('123', 'ppp');
819 claser.method_1('abc');
820 claser.method_1('xyz');
821 claser.method_1();
822 claser.method_1('abc', 'xxx', 'yyy');
823 claser.method_1();
824 // 6.验证函数method_1且参数为空时,是否至少执行过2次
825 mocker.verify('method_1', []).atLeast(2);
826 })
827 })
828}
829```
830
831#### 数据驱动
832
833##### 约束限制
834
835单元测试框架数据驱动能力从[框架 1.0.2版本](https://ohpm.openharmony.cn/#/cn/detail/@ohos%2Fhypium)开始支持。
836
837- 数据参数传递 : 为指定测试套、测试用例传递测试输入数据参数。
838- 压力测试 : 为指定测试套、测试用例设置执行次数。
839
840数据驱动可以根据配置参数来驱动测试用例的执行次数和每一次传入的参数,使用时依赖data.json配置文件,文件内容如下:
841
842>说明 : data.json与测试用例*.test.js|ets文件同目录
843
844```json
845{
846 "suites": [{
847 "describe": ["actsAbilityTest"],
848 "stress": 2,
849 "params": {
850 "suiteParams1": "suiteParams001",
851 "suiteParams2": "suiteParams002"
852 },
853 "items": [{
854 "it": "testDataDriverAsync",
855 "stress": 2,
856 "params": [{
857 "name": "tom",
858 "value": 5
859 }, {
860 "name": "jerry",
861 "value": 4
862 }]
863 }, {
864 "it": "testDataDriver",
865 "stress": 3
866 }]
867 }]
868}
869```
870
871配置参数说明:
872
873| | 配置项名称 | 功能 | 必填 |
874| :--- | :--------- | :------------------------------------ | ---- |
875| 1 | "suite" | 测试套配置 。 | 是 |
876| 2 | "items" | 测试用例配置 。 | 是 |
877| 3 | "describe" | 测试套名称 。 | 是 |
878| 4 | "it" | 测试用例名称 。 | 是 |
879| 5 | "params" | 测试套 / 测试用例 可传入使用的参数 。 | 否 |
880| 6 | "stress" | 测试套 / 测试用例 指定执行次数 。 | 否 |
881
882示例代码:
883
884DevEco Studio从V3.0 Release(2022-09-06)版本开始支持
885
886stage模型:
887
888在TestAbility目录下TestAbility.ets文件中导入data.json,并在Hypium.hypiumTest() 方法执行前,设置参数数据
889
890FA模型:
891
892在TestAbility目录下app.js或app.ets文件中导入data.json,并在Hypium.hypiumTest() 方法执行前,设置参数数据
893
894```javascript
895import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
896import { Hypium } from '@ohos/hypium'
897import testsuite from '../test/List.test'
898import data from '../test/data.json';
899
900...
901Hypium.setData(data);
902Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite);
903...
904```
905
906```javascript
907 import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
908
909 export default function abilityTest() {
910 describe('actsAbilityTest', () => {
911 it('testDataDriverAsync', 0, async (done: Function, data: ParmObj) => {
912 console.info('name: ' + data.name);
913 console.info('value: ' + data.value);
914 done();
915 });
916
917 it('testDataDriver', 0, () => {
918 console.info('stress test');
919 })
920 })
921}
922 interface ParmObj {
923 name: string,
924 value: string
925 }
926```
927#### 专项能力
928
929该项能力需通过在cmd窗口中输入aa test命令执行触发,并通过设置执行参数触发不同功能。另外,测试应用模型与编译方式不同,对应的aa test命令也不同,具体可参考[自动化测试框架使用指导](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-test/arkxtest-guidelines.md#cmd%E6%89%A7%E8%A1%8C)
930
931- **筛选能力**
932
933 1、按测试用例属性筛选
934
935 可以利用框架提供的Level、Size、TestType 对象,对测试用例进行标记,以区分测试用例的级别、粒度、测试类型,各字段含义及代码如下:
936
937 | Key | 含义说明 | Value取值范围 |
938 | -------- | ------------ | ------------------------------------------------------------ |
939 | level | 用例级别 | "0","1","2","3","4", 例如:-s level 1 |
940 | size | 用例粒度 | "small","medium","large", 例如:-s size small |
941 | testType | 用例测试类型 | "function","performance","power","reliability","security","global","compatibility","user","standard","safety","resilience", 例如:-s testType function |
942
943 示例代码:
944
945 ```javascript
946 import { describe, it, expect, TestType, Size, Level } from '@ohos/hypium';
947
948 export default function attributeTest() {
949 describe('attributeTest', () => {
950 it("testAttributeIt", TestType.FUNCTION | Size.SMALLTEST | Level.LEVEL0, () => {
951 console.info('Hello Test');
952 })
953 })
954}
955 ```
956
957 示例命令:
958
959 ```shell
960 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s testType function -s size small -s level 0
961 ```
962
963 该命令作用是筛选测试应用中同时满足,用例测试类型是“function”、用例粒度是“small”、用例级别是“0”的三个条件用例执行。
964
965 2、按测试套/测试用例名称筛选
966
967 注意:测试套和测试用例的命名要符合框架规则,即以字母开头,后跟一个或多个字母、数字,不能包含特殊符号。
968
969 框架可以通过指定测试套与测试用例名称,来指定特定用例的执行,测试套与用例名称用“#”号连接,多个用“,”英文逗号分隔
970
971 | Key | 含义说明 | Value取值范围 |
972 | -------- | ----------------------- | ------------------------------------------------------------ |
973 | class | 指定要执行的测试套&用例 | ${describeName}#${itName},${describeName} , 例如:-s class attributeTest#testAttributeIt |
974 | notClass | 指定不执行的测试套&用例 | ${describeName}#${itName},${describeName} , 例如:-s notClass attributeTest#testAttribut |
975
976 示例代码:
977
978 ```javascript
979 import { describe, it, expect, TestType, Size, Level } from '@ohos/hypium';
980
981 export default function attributeTest() {
982 describe('describeTest_000', () => {
983 it("testIt_00", TestType.FUNCTION | Size.SMALLTEST | Level.LEVEL0, () => {
984 console.info('Hello Test');
985 })
986
987 it("testIt_01", TestType.FUNCTION | Size.SMALLTEST | Level.LEVEL0, () => {
988 console.info('Hello Test');
989 })
990 })
991
992 describe('describeTest_001', () => {
993 it("testIt_02", TestType.FUNCTION | Size.SMALLTEST | Level.LEVEL0, () => {
994 console.info('Hello Test');
995 })
996 })
997}
998 ```
999
1000 示例命令1:
1001
1002 ```shell
1003 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s class describeTest_000#testIt_00,describeTest_001
1004 ```
1005
1006 该命令作用是执行“describeTest_001”测试套中所用用例,以及“describeTest_000”测试套中的“testIt_00”用例。
1007
1008 示例命令2:
1009
1010 ```shell
1011 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s notClass describeTest_000#testIt_01
1012 ```
1013
1014 该命令作用是不执行“describeTest_000”测试套中的“testIt_01”用例。
1015
1016- **随机执行**
1017
1018 使测试套与测试用例随机执行,用于稳定性测试。
1019
1020 | Key | 含义说明 | Value取值范围 |
1021 | ------ | ------------------------------------ | ---------------------------------------------- |
1022 | random | @since1.0.3 测试套、测试用例随机执行 | true, 不传参默认为false, 例如:-s random true |
1023
1024 示例命令:
1025
1026 ```shell
1027 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s random true
1028 ```
1029
1030- **压力测试**
1031
1032 指定要执行用例的执行次数,用于压力测试。
1033
1034 | Key | 含义说明 | Value取值范围 |
1035 | ------ | ------------------------------------ | ------------------------------ |
1036 | stress | @since1.0.5 指定要执行用例的执行次数 | 正整数, 例如: -s stress 1000 |
1037
1038 示例命令:
1039
1040 ```shell
1041 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s stress 1000
1042 ```
1043
1044- **用例超时时间设置**
1045
1046 指定测试用例执行的超时时间,用例实际耗时如果大于超时时间,用例会抛出"timeout"异常,用例结果会显示“excute timeout XXX”
1047
1048 | Key | 含义说明 | Value取值范围 |
1049 | ------- | -------------------------- | ---------------------------------------------------- |
1050 | timeout | 指定测试用例执行的超时时间 | 正整数(单位ms),默认为 5000,例如: -s timeout 15000 |
1051
1052 示例命令:
1053
1054 ```shell
1055 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s timeout 15000
1056 ```
1057
1058- **遇错即停模式**
1059
1060 | Key | 含义说明 | Value取值范围 |
1061 | ------------ | ------------------------------------------------------------ | ---------------------------------------------------- |
1062 | breakOnError | @since1.0.6 遇错即停模式,当执行用例断言失败或者发生错误时,退出测试执行流程 | true, 不传参默认为false, 例如:-s breakOnError true |
1063
1064 示例命令:
1065
1066 ```shell
1067 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s breakOnError true
1068 ```
1069
1070- **测试套中用例信息输出**
1071
1072 输出测试应用中待执行的测试用例信息
1073
1074 | Key | 含义说明 | Value取值范围 |
1075 | ------ | ---------------------------- | ---------------------------------------------- |
1076 | dryRun | 显示待执行的测试用例信息全集 | true, 不传参默认为false, 例如:-s dryRun true |
1077
1078 示例命令:
1079
1080 ```shell
1081 hdc shell aa test -b xxx -m xxx -s unittest OpenHarmonyTestRunner -s dryRun true
1082 ```
1083
1084- **嵌套能力**
1085
1086 1.示例代码
1087 ```javascript
1088 // Test1.test.ets
1089 import { describe, expect, it } from '@ohos/hypium';
1090 import test2 from './Test2.test';
1091
1092 export default function test1() {
1093 describe('test1', () => {
1094 it('assertContain1', 0, () => {
1095 let a = true;
1096 let b = true;
1097 expect(a).assertEqual(b);
1098 })
1099 // 引入测试套test2
1100 test2();
1101 })
1102 }
1103 ```
1104
1105 ```javascript
1106 //Test2.test.ets
1107 import { describe, expect, it } from '@ohos/hypium';
1108
1109 export default function test2() {
1110 describe('test2', () => {
1111 it('assertContain1', 0, () => {
1112 let a = true;
1113 let b = true;
1114 expect(a).assertEqual(b);
1115 })
1116 it('assertContain2', 0, () => {
1117 let a = true;
1118 let b = true;
1119 expect(a).assertEqual(b);
1120 })
1121 })
1122 }
1123 ```
1124
1125 ```javascript
1126 //List.test.ets
1127 import test1 from './nest/Test1.test';
1128
1129 export default function testsuite() {
1130 test1();
1131 }
1132 ```
1133
1134 2.示例筛选参数
1135 ```shell
1136 #执行test1的全部测试用例
1137 -s class test1
1138 ```
1139 ```shell
1140 #执行test1的子测试用例
1141 -s class test1#assertContain1
1142 ```
1143 ```shell
1144 #执行test1的子测试套test2的测试用例
1145 -s class test1.test2#assertContain1
1146 ```
1147
1148- **跳过能力**
1149
1150 | Key | 含义说明 | Value取值范围 |
1151 | ------------ | ------------------------------------------------------------ | ---------------------------------------------------- |
1152 | skipMessage | @since1.0.17 显示待执行的测试用例信息全集中是否包含跳过测试套和跳过用例的信息 | true/false, 不传参默认为false, 例如:-s skipMessage true |
1153 | runSkipped | @since1.0.17 指定要执行的跳过测试套&跳过用例 | all,skipped,${describeName}#${itName},${describeName},不传参默认为空,例如:-s runSkipped all |
1154
1155 1.示例代码
1156
1157 ```javascript
1158 //Skip1.test.ets
1159 import { expect, xdescribe, xit } from '@ohos/hypium';
1160
1161 export default function skip1() {
1162 xdescribe('skip1', () => {
1163 //注意:在xdescribe中不支持编写it用例
1164 xit('assertContain1', 0, () => {
1165 let a = true;
1166 let b = true;
1167 expect(a).assertEqual(b);
1168 })
1169 })
1170 }
1171 ```
1172
1173 ```javascript
1174 //Skip2.test.ets
1175 import { describe, expect, xit, it } from '@ohos/hypium';
1176
1177 export default function skip2() {
1178 describe('skip2', () => {
1179 //默认会跳过assertContain1
1180 xit('assertContain1', 0, () => {
1181 let a = true;
1182 let b = true;
1183 expect(a).assertEqual(b);
1184 })
1185 it('assertContain2', 0, () => {
1186 let a = true;
1187 let b = true;
1188 expect(a).assertEqual(b);
1189 })
1190 })
1191 }
1192 ```
1193
1194
1195
1196### 使用方式
1197
1198单元测试框架以ohpm包形式发布至[服务组件官网](https://ohpm.openharmony.cn/#/cn/detail/@ohos%2Fhypium),开发者可以下载Deveco Studio后,在应用工程中配置依赖后使用框架能力,测试工程创建及测试脚本执行使用指南请参见[IDE指导文档](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-openharmony-test-framework-0000001263160453)。
1199
1200## Ui测试框架功能特性
1201
1202| No. | 特性 | 功能说明 |
1203| ---- |-----------|-------------------------------------------------|
1204| 1 | Driver | Ui测试的入口,提供查找控件,检查控件存在性以及注入按键能力。 |
1205| 2 | On | 用于描述目标控件特征(文本、id、类型等),`Driver`根据`On`描述的控件特征信息来查找控件。 |
1206| 3 | Component | Driver查找返回的控件对象,提供查询控件属性,滑动查找等触控和检视能力。 |
1207| 4 | UiWindow | Driver查找返回的窗口对象,提供获取窗口属性、操作窗口的能力。 |
1208
1209**使用者在测试脚本通过如下方式引入使用:**
1210
1211```typescript
1212import {Driver,ON,Component,UiWindow,MatchPattern} from '@ohos.UiTest'
1213```
1214
1215> 须知
1216> 1. `On`类提供的接口全部是同步接口,使用者可以使用`builder`模式链式调用其接口构造控件筛选条件。
1217> 2. `Driver`和`Component`类提供的接口全部是异步接口(`Promise`形式),**需使用`await`语法**。
1218> 3. Ui测试用例均需使用**异步**语法编写用例,需遵循单元测试框架异步用例编写规范。
1219
1220
1221
1222在测试用例文件中import `On/Driver/Component`类,然后调用API接口编写测试用例。
1223
1224```javascript
1225import { Driver, ON, Component } from '@kit.TestKit'
1226import { describe, it, expect } from '@ohos/hypium'
1227
1228export default function findComponentTest() {
1229 describe('findComponentTest', () => {
1230 it('uitest_demo0', 0, async () => {
1231 // create Driver
1232 let driver: Driver = Driver.create();
1233 // find component by text
1234 let button: Component = await driver.findComponent(ON.text('Hello World').enabled(true));
1235 // click component
1236 await button.click();
1237 // get and assert component text
1238 let content: string = await button.getText();
1239 expect(content).assertEqual('Hello World');
1240 })
1241 })
1242}
1243```
1244
1245### Driver使用说明
1246
1247`Driver`类作为UiTest测试框架的总入口,提供查找控件,注入按键,单击坐标,滑动控件,手势操作,截图等能力。
1248
1249| No. | API | 功能描述 |
1250| ---- |-----------------------------------------------------------------| ---------------------- |
1251| 1 | create():Promise<Driver> | 静态方法,构造Driver。 |
1252| 2 | findComponent(on:On):Promise<Component> | 查找匹配控件。 |
1253| 3 | pressBack():Promise<void> | 单击BACK键。 |
1254| 4 | click(x:number, y:number):Promise<void> | 基于坐标点的单击。 |
1255| 5 | swipe(x1:number, y1:number, x2:number, y2:number):Promise<void> | 基于坐标点的滑动。 |
1256| 6 | assertComponentExist(on:On):Promise<void> | 断言匹配的控件存在。 |
1257| 7 | delayMs(t:number):Promise<void> | 延时。 |
1258| 8 | screenCap(s:path):Promise<void> | 截屏。 |
1259| 9 | findWindow(filter: WindowFilter): Promise<UiWindow> | 查找匹配窗口。 |
1260
1261其中assertComponentExist接口是断言API,用于断言当前界面存在目标控件;如果控件不存在,该API将抛出JS异常,使当前测试用例失败。
1262
1263```javascript
1264import { describe, it} from '@ohos/hypium';
1265import { Driver, ON } from '@kit.TestKit';
1266
1267export default function assertComponentExistTest() {
1268 describe('assertComponentExistTest', () => {
1269 it('Uitest_demo0', 0, async (done: Function) => {
1270 try{
1271 // create Driver
1272 let driver: Driver = Driver.create();
1273 // assert text 'hello' exists on current Ui
1274 await driver.assertComponentExist(ON.text('hello'));
1275 } finally {
1276 done();
1277 }
1278 })
1279 })
1280}
1281```
1282
1283`Driver`完整的API列表请参考[API文档](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts)及[示例文档说明](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-test-kit/js-apis-uitest.md#driver9)。
1284
1285### On使用说明
1286
1287Ui测试框架通过`On`类提供了丰富的控件特征描述API,用来匹配查找要操作或检视的目标控件。`On`提供的API能力具有以下特点:
1288
1289- 支持匹配单属性和匹配多属性组合,例如同时指定目标控件text和id。
1290- 控件属性支持多种匹配模式(等于,包含,`STARTS_WITH`,`ENDS_WITH`)。
1291- 支持相对定位控件,可通过`isBefore`和`isAfter`等API限定邻近控件特征进行辅助定位。
1292
1293| No. | API | 功能描述 |
1294|-----|------------------------------------|----------------------------|
1295| 1 | id(i:string):On | 指定控件id。 |
1296| 2 | text(t:string, p?:MatchPattern):On | 指定控件文本,可指定匹配模式。 |
1297| 3 | type(t:string):On | 指定控件类型。 |
1298| 4 | enabled(e:bool):On | 指定控件使能状态。 |
1299| 5 | clickable(c:bool):On | 指定控件可单击状态。 |
1300| 6 | longClickable(l:bool):On | 指定控件可长按状态。 |
1301| 7 | focused(f:bool):On | 指定控件获焦状态。 |
1302| 8 | scrollable(s:bool):On | 指定控件可滑动状态。 |
1303| 9 | selected(s:bool):On | 指定控件选中状态。 |
1304| 10 | checked(c:bool):On | 指定控件选择状态。 |
1305| 11 | checkable(c:bool):On | 指定控件可选择状态。 |
1306| 12 | isBefore(b:On):On | **相对定位**,限定目标控件位于指定特征控件之前。 |
1307| 13 | isAfter(b:On):On | **相对定位**,限定目标控件位于指定特征控件之后。 |
1308
1309其中,`text`属性支持{`MatchPattern.EQUALS`,`MatchPattern.CONTAINS`,`MatchPattern.STARTS_WITH`,`MatchPattern.ENDS_WITH`}四种匹配模式,缺省使用`MatchPattern.EQUALS`模式。
1310
1311`On`完整的API列表请参考[API文档](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts)及[示例文档说明](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-test-kit/js-apis-uitest.md#on9)。
1312
1313#### 控件绝对定位
1314
1315**示例代码1**:查找id是`Id_button`的控件。
1316
1317```javascript
1318let button: Component = await driver.findComponent(ON.id('Id_button'))
1319```
1320
1321 **示例代码2**:查找id是`Id_button`并且状态是`enabled`的控件,适用于无法通过单一属性定位的场景。
1322
1323```javascript
1324let button: Component = await driver.findComponent(ON.id('Id_button').enabled(true))
1325```
1326
1327通过`On.id(x).enabled(y)`来指定目标控件的多个属性。
1328
1329**示例代码3**:查找文本中包含`hello`的控件,适用于不能完全确定控件属性取值的场景。
1330
1331```javascript
1332let txt: Component = await driver.findComponent(ON.text('hello', MatchPattern.CONTAINS))
1333```
1334
1335通过向`On.text()`方法传入第二个参数`MatchPattern.CONTAINS`来指定文本匹配规则;默认规则是`MatchPattern.EQUALS`,即目标控件text属性必须严格等于给定值。
1336
1337#### 控件相对定位
1338
1339**示例代码1**:查找位于文本控件`Item3_3`后面的,id是`Id_switch`的Switch控件。
1340
1341```javascript
1342let switch: Component = await driver.findComponent(ON.id('Id_switch').isAfter(ON.text('Item3_3')))
1343```
1344
1345通过`On.isAfter`方法,指定位于目标控件前面的特征控件属性,通过该特征控件进行相对定位。一般地,特征控件是某个具有全局唯一特征的控件(例如具有唯一的id或者唯一的text)。
1346
1347类似的,可以使用`On.isBefore`控件指定位于目标控件后面的特征控件属性,实现相对定位。
1348
1349### Component使用说明
1350
1351`Component`类代表了Ui界面上的一个控件,一般是通过`Driver.findComponent(on)`方法查找到的。通过该类的实例,用户可以获取控件属性,单击控件,滑动查找,注入文本等操作。
1352
1353`Component`包含的常用API:
1354
1355| No. | API | 功能描述 |
1356|-----|------------------------------------|----------------------------|
1357| 1 | click():Promise<void> | 单击该控件。 |
1358| 2 | inputText(t:string):Promise<void> | 向控件中输入文本(适用于文本框控件)。 |
1359| 3 | scrollSearch(s:On):Promise<Component> | 在该控件上滑动查找目标控件(适用于List等控件)。 |
1360| 4 | scrollToTop(s:number):Promise<void> | 滑动到该控件顶部(适用于List等控件)。 |
1361| 5 | scrollTobottom(s:number):Promise<void> | 滑动到该控件底部(适用于List等控件)。 |
1362| 6 | getText():Promise<string> | 获取控件text。 |
1363| 7 | getId():Promise<number> | 获取控件id。 |
1364| 8 | getType():Promise<string> | 获取控件类型。 |
1365| 9 | isEnabled():Promise<bool> | 获取控件使能状态。 |
1366
1367`Component`完整的API列表请参考[API文档](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts)及[示例文档说明](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-test-kit/js-apis-uitest.md#component9)。
1368
1369**示例代码1**:单击控件。
1370
1371```javascript
1372let button: Component = await driver.findComponent(ON.id('Id_button'))
1373await button.click()
1374```
1375
1376**示例代码2**:通过get接口获取控件属性后,可以使用单元测试框架提供的assert*接口做断言检查。
1377
1378```javascript
1379let component: Component = await driver.findComponent(ON.id('Id_title'))
1380expect(component !== null).assertTrue()
1381```
1382
1383**示例代码3**:在List控件中滑动查找text是`Item3_3`的子控件。
1384
1385```javascript
1386let list: Component = await driver.findComponent(ON.id('Id_list'))
1387let found: Component = await list.scrollSearch(ON.text('Item3_3'))
1388expect(found).assertTrue()
1389```
1390
1391**示例代码4**:向输入框控件中输入文本。
1392
1393```javascript
1394let editText: Component = await driver.findComponent(ON.type('InputText'))
1395await editText.inputText('user_name')
1396```
1397### UiWindow使用说明
1398
1399`UiWindow`类代表了Ui界面上的一个窗口,一般是通过`Driver.findWindow(WindowFilter)`方法查找到的。通过该类的实例,用户可以获取窗口属性,并进行窗口拖动、调整窗口大小等操作。
1400
1401`UiWindow`包含的常用API:
1402
1403| No. | API | 功能描述 |
1404| ---- | ------------------------------------------------------------ | -------------------------------------------------- |
1405| 1 | getBundleName(): Promise<string> | 获取窗口所属应用包名。 |
1406| 2 | getTitle(): Promise<string> | 获取窗口标题信息。 |
1407| 3 | focus(): Promise<bool> | 使得当前窗口获取焦点。 |
1408| 4 | moveTo(x: number, y: number): Promise<bool> | 将当前窗口移动到指定位置(适用于支持移动的窗口)。 |
1409| 5 | resize(wide: number, height: number, direction: ResizeDirection): Promise<bool> | 调整窗口大小(适用于支持调整大小的窗口)。 |
1410| 6 | split(): Promise<bool> | 将窗口模式切换为分屏模式(适用于支持分屏的窗口)。 |
1411| 7 | close(): Promise<bool> | 关闭当前窗口。 |
1412
1413`UiWindow`完整的API列表请参考[API文档](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.UiTest.d.ts)及[示例文档说明](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-test-kit/js-apis-uitest.md#uiwindow9)。
1414
1415**示例代码1**:获取窗口属性。
1416
1417```javascript
1418let window: UiWindow = await driver.findWindow({actived: true})
1419let bundelName: string = await window.getBundleName()
1420```
1421
1422**示例代码2**:移动窗口。
1423
1424```javascript
1425let window: UiWindow = await driver.findWindow({actived: true})
1426await window.moveTo(500,500)
1427```
1428
1429**示例代码3**:关闭窗口。
1430
1431```javascript
1432let window: UiWindow = await driver.findWindow({actived: true})
1433await window.close()
1434```
1435
1436### 使用方式
1437
1438 开发者可以下载Deveco Studio创建测试工程后,在其中调用框架提供接口进行相关测试操作,测试工程创建及测试脚本执行使用指南请参见[IDE指导文档](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-openharmony-test-framework-0000001267284568)。
1439 UI测试框架使能需要执行如下命令。
1440
1441>```shell
1442> hdc shell param set persist.ace.testmode.enabled 1
1443>```
1444### UI测试框架自构建方式
1445
1446> Ui测试框架在OpenHarmony-3.1-Release版本中未随版本编译,需手动处理,请参考[3.1-Release版本使用指导](https://gitee.com/openharmony/testfwk_arkxtest/blob/OpenHarmony-3.1-Release/README_zh.md#%E6%8E%A8%E9%80%81ui%E6%B5%8B%E8%AF%95%E6%A1%86%E6%9E%B6%E8%87%B3%E8%AE%BE%E5%A4%87)。
1447
1448开发者如需自行编译Ui测试框架代码验证子修改内容,构建命令和推送位置请参考本章节内容。
1449
1450#### 构建命令
1451
1452```shell
1453./build.sh --product-name rk3568 --build-target uitestkit
1454```
1455#### 推送位置
1456
1457```shell
1458hdc target mount
1459hdc shell mount -o rw,remount /
1460hdc file send uitest /system/bin/uitest
1461hdc file send libuitest.z.so /system/lib/module/libuitest.z.so
1462hdc shell chmod +x /system/bin/uitest
1463```
1464
1465### 命令行使用说明
1466
1467 开发者可以输入如下命令来实现对应功能。
1468
14691、打印使用帮助
1470
1471```shell
1472hdc shell uitest help
1473```
1474
14752、截屏
1476
1477```
1478hdc shell uitest screenCap
1479// 默认存储路径:/data/local/tmp,文件名:时间戳 + .png。
1480hdc shell uitest screenCap -p /data/local/tmp/1.png
1481// 指定存储路径和文件名, 只支持存放在/data/local/tmp/下。
1482```
1483
14843、获取设备当前Ui控件树信息
1485
1486```shell
1487hdc shell uitest dumpLayout
1488// 默认存储路径:/data/local/tmp,文件名:时间戳 + .json。
1489hdc shell uitest screenCap -p /data/local/tmp/1.json
1490// 指定存储路径和文件名, 只支持存放在/data/local/tmp/下。
1491```
1492
14934、录制Ui操作
1494
1495```shell
1496hdc shell uitest uiRecord record
1497// 将当前执行的Ui操作记录到/data/local/tmp/layout/record.csv
1498hdc shell uitest uiRecord read
1499// 将记录的Ui操作打印出来
1500```
1501
15025、 shell命令方式注入UI模拟操作
1503> 支持操作类型:点击 双击 长按 慢滑 快滑 拖拽 输入文字 KeyEvent。
1504
1505| 配置参数名 | 配置参数含义 | 配置参数取值 | 示例 |
1506|-------------|-----------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|
1507| click | 模拟单击操作 | point_x (必选参数,点击x坐标点)<br/> point_y (必选参数,点击y坐标点) | hdc shell uitest uiInput click point_x point_y |
1508| doubleClick | 模拟双击操作 | point_x (必选参数,双击x坐标点)<br/> point_y (必选参数,双击y坐标点) | hdc shell uitest uiInput doubleClick point_x point_y |
1509| longClick | 模拟长按操作 | point_x (必选参数,长按x坐标点)<br/> point_y (必选参数,长按y坐标点) | hdc shell uitest uiInput longClick point_x point_y |
1510| fling | 模拟快滑操作 | from_x (必选参数,滑动起点x坐标)<br/> from_y(必选参数,滑动起点y坐标)<br/> to_x(必选参数,滑动终点x坐标)<br/> to_y(必选参数,滑动终点y坐标)<br/> swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s)<br/> stepLength(可选参数,滑动步长,默认值:滑动距离/50, 单位: px) | hdc shell uitest uiInput fling from_x from_y to_x to_y swipeVelocityPps_ stepLength |
1511| swipe | 模拟慢滑操作 | from_x (必选参数,滑动起点x坐标)<br/> from_y(必选参数,滑动起点y坐标)<br/> to_x(必选参数,滑动终点x坐标)<br/> to_y(必选参数,滑动终点y坐标)<br/> swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s)) | hdc shell uitest uiInput swipe from_x from_y to_x to_y swipeVelocityPps_ |
1512| drag | 模拟拖拽操作 | from_x (必选参数,拖拽起点x坐标)<br/> from_y(必选参数,拖拽起点y坐标)<br/> to_x(必选参数,拖拽终点x坐标)<br/> to_y(必选参数,拖拽终点y坐标)<br/> swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s)) | hdc shell uitest uiInput drag from_x from_y to_x to_y swipeVelocityPps_ |
1513| dircFling | 模拟指定方向滑动操作 | direction (可选参数,滑动方向,可选值: [0,1,2,3], 滑动方向: [左,右,上,下],默认值: 0)<br/> swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s)<br/> stepLength(可选参数,滑动步长,默认值:滑动距离/50, 单位: px) | hdc shell uitest uiInput dircFling direction swipeVelocityPps_ stepLength |
1514| inputText | 模拟输入框输入文本操作 | point_x (必选参数,输入框x坐标点)<br/> point_y (必选参数,输入框y坐标点)<br/> input(输入文本) | hdc shell uitest uiInput inputText point_x point_y text |
1515| keyEvent | 模拟实体按键事件(如:键盘,电源键,返回上一级,返回桌面等),以及组合按键操作 | keyID (必选参数,实体按键对应ID)<br/> keyID2 (可选参数,实体按键对应ID) | hdc shell uitest uiInput keyEvent keyID |
1516
1517示例代码1:执行点击事件。
1518```shell
1519 hdc shell uitest uiInput click 100 100
1520```
1521示例代码2:执行双击事件。
1522```shell
1523 hdc shell uitest uiInput doubleClick 100 100
1524```
1525示例代码3:执行长按事件。
1526```shell
1527 hdc shell uitest uiInput longClick 100 100
1528```
1529示例代码4:执行快滑操作。
1530```shell
1531hdc shell uitest uiInput fling 10 10 200 200 500
1532```
1533示例代码5:执行慢滑操作。
1534```shell
1535hdc shell uitest uiInput swipe 10 10 200 200 500
1536```
1537示例代码6:执行拖拽操作。
1538```shell
1539hdc shell uitest uiInput drag 10 10 100 100 500
1540```
1541示例代码7:执行向左滑动操作。
1542```shell
1543hdc shell uitest uiInput dircFling 0 500
1544```
1545示例代码8:执行向右滑动操作。
1546```shell
1547hdc shell uitest uiInput dircFling 1 600
1548```
1549示例代码9:执行向上滑动操作。
1550```shell
1551hdc shell uitest uiInput dircFling 2
1552```
1553示例代码10:执行向下滑动操作。
1554```shell
1555hdc shell uitest uiInput dircFling 3
1556```
1557
1558示例代码11:执行输入框输入操作。
1559```shell
1560hdc shell uitest uiInput inputText 100 100 hello
1561```
1562
1563示例代码12:执行返回主页操作。
1564```shell
1565hdc shell uitest uiInput keyEvent Home
1566```
1567示例代码13:执行返回上一步操作。
1568```shell
1569hdc shell uitest uiInput keyEvent Back
1570```
1571示例代码14:执行组合键粘贴操作。
1572```shell
1573hdc shell uitest uiInput keyEvent 2072 2038
1574```
1575
1576### 版本信息
1577
1578| 版本号 | 功能说明 |
1579| :------ | :----------------------------------------------------------- |
1580| 3.2.2.1 | 1、增加抛滑、获取/设置屏幕方向接口<br />2、窗口处理逻辑增加不支持场景处理逻辑 |
1581| 3.2.3.0 | 1、滑动控件进行滑动查找、滑动到尾部/顶部功能优化 |
1582| 3.2.4.0 | 1、接口调用异常时会抛出错误码 |
1583| 3.2.5.0 | 1、通信机制变更 |
1584| 3.2.6.0 | 1、增加模拟鼠标操作能力接口<br />2、增加指定应用的窗口下查找目标控件接口 |
1585| 4.0.1.1 | 1、支持在daemon运行时执行uitest dumpLayout |
1586| 4.0.1.2 | 1、模拟鼠标动作、键鼠协同功能优化 |
1587| 4.0.1.3 | 1、示例代码更新<br />2、滑动控件进行滑动查找、滑动到尾部/顶部功能优化 |
1588| 4.0.1.4 | 1、可选参数传入undefined时,当作默认值处理 |
1589| 4.0.2.0 | 1、支持监听toast和dialog控件出现,使用callback的形式返回结果。 |
1590| 4.0.3.0 | 1、增加加载运行.abc文件机制。 |
1591| 4.0.4.0 | 1、支持abc_loader框架获取UI操作录制数据,屏幕数据,控件树等,并以callback的形式返回结果<br />2、修改录制数据结构 |
1592| 4.1.1.1 | 1、对接批量获取控件信息能力,缩短获取控件信息的耗时。 |
1593| 4.1.2.0 | 1、增加shell命令方式注入UI模拟操作。 |
1594| 4.1.3.0 | 1、新增命令行功能,uitest dumuLayout -a ,dump信息中包含控件的背景色、字体颜色/大小信息。 |
1595| 4.1.4.0 | 1、dump信息中增加hint与description字段。<br />2、优化多指操作。<br />3、优化查找控件的效率。<br />4、uitest uiInput执行效率提升。 |
1596| 5.0.1.0 | 1、优化swipe操作。<br />2、inputText输入中文的实现方式改为设置剪贴板数据后,长按控件点击粘贴。 |
1597