js-apis-uitest.md 126.1 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.UiTest
E
add doc  
ester.zhou 已提交
2

E
esterzhou 已提交
3 4 5 6 7 8 9 10
The **UiTest** module provides APIs that you can use to simulate UI actions during testing, such as clicks, double-clicks, long-clicks, and swipes.

This module provides the following functions:

- [On<sup>9+</sup>](#on9): provides UI component feature description APIs for component filtering and matching.
- [Component<sup>9+</sup>](#component9): represents a component on the UI and provides APIs for obtaining component attributes, clicking a component, scrolling to search for a component, and text injection.
- [Driver<sup>9+</sup>](#driver9): works as the entry class and provides APIs for features such as component matching/search, key injection, coordinate clicking/sliding, and screenshot.
- [UiWindow<sup>9+</sup>](#uiwindow9): works as the entry class and provides APIs for obtaining window attributes, dragging windows, and adjusting window sizes.
E
ester.zhou 已提交
11 12 13
- [By<sup>(deprecated)</sup>](#bydeprecated): provides UI component feature description APIs for component filtering and matching. This class is deprecated since API version 9. You are advised to use [On<sup>9+</sup>](#on9) instead.
- [UiComponent<sup>(deprecated)</sup>](#uicomponentdeprecated): represents a component on the UI and provides APIs for obtaining component attributes, clicking a component, scrolling to search for a component, and text injection. This class is deprecated since API version 9. You are advised to use [Component<sup>9+</sup>](#component9) instead.
- [UiDriver<sup>(deprecated)</sup>](#uidriverdeprecated): works as the entry class and provides APIs for features such as component matching/search, key injection, coordinate clicking/sliding, and screenshot. This class is deprecated since API version 9. You are advised to use [Driver<sup>9+</sup>](#driver9) instead.
E
esterzhou 已提交
14 15

>**NOTE**
E
add doc  
ester.zhou 已提交
16
>
E
esterzhou 已提交
17
>The 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.
E
add doc  
ester.zhou 已提交
18 19 20 21


## Modules to Import

E
esterzhou 已提交
22
```js
E
ester.zhou 已提交
23
import {UiComponent, UiDriver, Component, Driver, UiWindow, ON, BY, MatchPattern, DisplayRotation, ResizeDirection, WindowMode, PointerMatrix, UiDirection, MouseButton, UIElementInfo, UIEventObserver} from '@ohos.UiTest';
E
add doc  
ester.zhou 已提交
24 25
```

E
esterzhou 已提交
26
## MatchPattern
E
add doc  
ester.zhou 已提交
27

E
esterzhou 已提交
28
Enumerates the match patterns supported for component attributes.
E
add doc  
ester.zhou 已提交
29

E
esterzhou 已提交
30
**System capability**: SystemCapability.Test.UiTest
E
add doc  
ester.zhou 已提交
31

E
esterzhou 已提交
32 33
| Name       | Value  | Description          |
| ----------- | ---- | -------------- |
E
ester.zhou 已提交
34
| EQUALS      | 0    | Equals the given value.  |
E
esterzhou 已提交
35 36 37
| CONTAINS    | 1    | Contains the given value.  |
| STARTS_WITH | 2    | Starts with the given value.|
| ENDS_WITH   | 3    | Ends with the given value.|
E
add doc  
ester.zhou 已提交
38

E
esterzhou 已提交
39
## ResizeDirection<sup>9+</sup>
E
add doc  
ester.zhou 已提交
40

E
esterzhou 已提交
41
Enumerates the directions in which a window can be resized.
E
add doc  
ester.zhou 已提交
42 43 44

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
45 46 47 48 49 50 51 52 53 54
| Name      | Value  | Description    |
| ---------- | ---- | -------- |
| LEFT       | 0    | Left.  |
| RIGHT      | 1    | Right.  |
| UP         | 2    | Up.  |
| DOWN       | 3    | Down.  |
| LEFT_UP    | 4    | Upper left.|
| LEFT_DOWN  | 5    | Lower left.|
| RIGHT_UP   | 6    | Upper right.|
| RIGHT_DOWN | 7    | Lower right.|
E
add doc  
ester.zhou 已提交
55

E
esterzhou 已提交
56
## Point<sup>9+</sup>
E
add doc  
ester.zhou 已提交
57

E
esterzhou 已提交
58
Provides the coordinates of a point.
E
add doc  
ester.zhou 已提交
59

E
esterzhou 已提交
60
**System capability**: SystemCapability.Test.UiTest
E
add doc  
ester.zhou 已提交
61

E
esterzhou 已提交
62 63
| Name| Type  | Readable| Writable| Description            |
| ---- | ------ | ---- | ---- | ---------------- |
E
ester.zhou 已提交
64 65
| x    | number | Yes  | No  | X-coordinate of a point.|
| y    | number | Yes  | No  | Y-coordinate of a point.|
E
esterzhou 已提交
66 67 68 69 70 71 72

## Rect<sup>9+</sup>

Provides bounds information of a component.

**System capability**: SystemCapability.Test.UiTest

E
ester.zhou 已提交
73 74 75 76 77 78
| Name  | Type  | Readable| Writable| Description                     |
| ------ | ------ | ---- | ---- | ------------------------- |
| left   | number | Yes  | No  | X-coordinate of the upper left corner of the component bounds.|
| top    | number | Yes  | No  | Y-coordinate of the upper left corner of the component bounds.|
| right  | number | Yes  | No  | X-coordinate of the lower right corner of the component bounds.|
| bottom | number | Yes  | No  | Y-coordinate of the lower right corner of the component bounds.|
E
esterzhou 已提交
79 80 81 82 83

## WindowMode<sup>9+</sup>

Enumerates the window modes.

E
ester.zhou 已提交
84 85
**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
86 87 88 89 90 91 92 93 94 95 96
| Name      | Value  | Description      |
| ---------- | ---- | ---------- |
| FULLSCREEN | 0    | Full-screen mode.|
| PRIMARY    | 1    | Primary window mode.  |
| SECONDARY  | 2    | Secondary window mode.|
| FLOATING   | 3    | Floating window mode.|

## DisplayRotation<sup>9+</sup>

Describes the display rotation of the device.

E
ester.zhou 已提交
97 98
**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| Name        | Value  | Description                                    |
| ------------ | ---- | ---------------------------------------- |
| ROTATION_0   | 0    | The device display is not rotated and is in its original vertical orientation.    |
| ROTATION_90  | 1    | The device display rotates 90° clockwise and is in landscape orientation.     |
| ROTATION_180 | 2    | The device display rotates 180° clockwise and is in reverse vertical orientation.|
| ROTATION_270 | 3    | The device display rotates 270° clockwise and is in reverse landscape orientation.|

## WindowFilter<sup>9+</sup>

Provides the flag attributes of this window.

**System capability**: SystemCapability.Test.UiTest

| Name      | Type   | Readable| Writable| Description                      |
| ---------- | ------- | ---- | ---- | -------------------------- |
| bundleName | string  | Yes  | No  | Bundle name of the application to which the window belongs.      |
| title      | string  | Yes  | No  | Title of the window.          |
| focused    | boolean | Yes  | No  | Whether the window is in focused state.    |
| actived    | boolean | Yes  | No  | Whether the window is interacting with the user.|

E
ester.zhou 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
## UiDirection<sup>10+</sup>

Describes the direction of a UI operation such as fling.

**System capability**: SystemCapability.Test.UiTest

| Name | Value  | Description  |
| ----- | ---- | ------ |
| LEFT  | 0    | Leftward.|
| RIGHT | 1    | Rightward.|
| UP    | 2    | Upward.|
| DOWN  | 3    | Downward.|

## MouseButton<sup>10+</sup>

Describes the injected simulated mouse button.

**System capability**: SystemCapability.Test.UiTest

| Name               | Value  | Description        |
| ------------------- | ---- | ------------ |
| MOUSE_BUTTON_LEFT   | 0    | Left button on the mouse.  |
| MOUSE_BUTTON_RIGHT  | 1    | Right button on the mouse.  |
| MOUSE_BUTTON_MIDDLE | 2    | Middle button on the mouse.|

E
ester.zhou 已提交
144 145 146 147 148 149 150 151 152 153 154 155
## UIElementInfo<sup>10+</sup>

Provides information about the UI event.

**System capability**: SystemCapability.Test.UiTest

| Name      | Type  | Readable| Writable| Description                 |
| ---------- | ------ | ---- | ---- | --------------------- |
| bundleName | string | Yes  | No  | Bundle name of the home application.     |
| type       | string | Yes  | No  | Component or window type.      |
| text       | string | Yes  | No  | Text information of the component or window.|

E
esterzhou 已提交
156 157 158
## On<sup>9+</sup>

Since API version 9, the UiTest framework provides a wide range of UI component feature description APIs in the **On** class to filter and match components.
E
ester.zhou 已提交
159 160 161 162 163

The API capabilities provided by the **On** class exhibit the following features:

- Allow one or more attributes as the match conditions. For example, you can specify both the **text** and **id** attributes to find the target component.
- Provide multiple match patterns for component attributes.
E
ester.zhou 已提交
164
- Support absolute positioning and relative positioning for components. APIs such as [ON.isBefore](#isbefore9) and [ON.isAfter](#isafter9) can be used to specify the features of adjacent components to assist positioning.
E
ester.zhou 已提交
165 166

All APIs provided in the **On** class are synchronous. You are advised to use the static constructor **ON** to create an **On** object in chain mode.
E
add doc  
ester.zhou 已提交
167

E
ester.zhou 已提交
168
```js
E
esterzhou 已提交
169
ON.text('123').type('button');
E
add doc  
ester.zhou 已提交
170 171
```

E
esterzhou 已提交
172
### text<sup>9+</sup>
E
add doc  
ester.zhou 已提交
173

E
esterzhou 已提交
174
text(txt: string, pattern?: MatchPattern): On
E
add doc  
ester.zhou 已提交
175

E
esterzhou 已提交
176
Specifies the text attribute of the target component. Multiple match patterns are supported.
E
add doc  
ester.zhou 已提交
177 178 179 180 181

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
182 183 184 185
| Name | Type                         | Mandatory| Description                                               |
| ------- | ----------------------------- | ---- | --------------------------------------------------- |
| txt     | string                        | Yes  | Component text, used to match the target component.               |
| pattern | [MatchPattern](#matchpattern) | No  | Match pattern. The default value is [EQUALS](#matchpattern).|
E
add doc  
ester.zhou 已提交
186 187 188

**Return value**

E
esterzhou 已提交
189 190 191
| Type      | Description                              |
| ---------- | ---------------------------------- |
| [On](#on9) | **On** object that matches the text attribute of the target component.|
E
add doc  
ester.zhou 已提交
192 193 194

**Example**

E
ester.zhou 已提交
195
```js
E
esterzhou 已提交
196
let on = ON.text('123'); // Use the static constructor ON to create an On object and specify the text attribute of the target component.
E
add doc  
ester.zhou 已提交
197 198 199
```


E
esterzhou 已提交
200
### id<sup>9+</sup>
E
add doc  
ester.zhou 已提交
201

E
esterzhou 已提交
202
id(id: string): On
E
add doc  
ester.zhou 已提交
203

E
ester.zhou 已提交
204
Specifies the ID attribute of the target component.
E
add doc  
ester.zhou 已提交
205 206 207 208 209

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
ester.zhou 已提交
210 211
| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
E
esterzhou 已提交
212
| id     | string | Yes  | Component ID.|
E
add doc  
ester.zhou 已提交
213 214 215

**Return value**

E
esterzhou 已提交
216 217 218
| Type      | Description                            |
| ---------- | -------------------------------- |
| [On](#on9) | **On** object that matches the ID attribute of the target component.|
E
add doc  
ester.zhou 已提交
219 220 221

**Example**

E
ester.zhou 已提交
222
```js
E
esterzhou 已提交
223
let on = ON.id('123'); // Use the static constructor ON to create an On object and specify the ID attribute of the target component.
E
add doc  
ester.zhou 已提交
224 225 226
```


E
esterzhou 已提交
227
### type<sup>9+</sup>
E
add doc  
ester.zhou 已提交
228

E
esterzhou 已提交
229
type(tp: string): On
E
add doc  
ester.zhou 已提交
230

E
ester.zhou 已提交
231
Specifies the type attribute of the target component.
E
add doc  
ester.zhou 已提交
232 233 234 235 236

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
ester.zhou 已提交
237 238
| Name| Type  | Mandatory| Description          |
| ------ | ------ | ---- | -------------- |
E
add doc  
ester.zhou 已提交
239 240 241 242
| tp     | string | Yes  | Component type.|

**Return value**

E
esterzhou 已提交
243 244 245
| Type      | Description                                    |
| ---------- | ---------------------------------------- |
| [On](#on9) | **On** object that matches the type attribute of the target component.|
E
add doc  
ester.zhou 已提交
246 247 248

**Example**

E
ester.zhou 已提交
249
```js
E
esterzhou 已提交
250
let on = ON.type('button'); // Use the static constructor ON to create an On object and specify the type attribute of the target component.
E
add doc  
ester.zhou 已提交
251 252 253
```


E
esterzhou 已提交
254
### clickable<sup>9+</sup>
E
add doc  
ester.zhou 已提交
255

E
esterzhou 已提交
256
clickable(b?: boolean): On
E
add doc  
ester.zhou 已提交
257

E
esterzhou 已提交
258
Specifies the clickable status attribute of the target component.
E
add doc  
ester.zhou 已提交
259 260 261 262 263

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
264 265
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
266
| b      | boolean | No  | Clickable status of the target component.<br>**true**: clickable.<br>**false**: not clickable.<br> Default value: **true**|
E
add doc  
ester.zhou 已提交
267 268 269

**Return value**

E
esterzhou 已提交
270 271 272
| Type      | Description                                      |
| ---------- | ------------------------------------------ |
| [On](#on9) | **On** object that matches the clickable status attribute of the target component.|
E
add doc  
ester.zhou 已提交
273 274 275

**Example**

E
ester.zhou 已提交
276
```js
E
esterzhou 已提交
277
let on = ON.clickable(true); // Use the static constructor ON to create an On object and specify the clickable status attribute of the target component.
E
add doc  
ester.zhou 已提交
278
```
E
ester.zhou 已提交
279

E
esterzhou 已提交
280
### longClickable<sup>9+</sup>
E
ester.zhou 已提交
281

E
esterzhou 已提交
282
longClickable(b?: boolean): On
E
ester.zhou 已提交
283 284 285 286 287 288 289

Specifies the long-clickable status attribute of the target component.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
290 291
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
292
| b      | boolean | No  | Long-clickable status of the target component.<br>**true**: long-clickable.<br>**false**: not long-clickable.<br> Default value: **true**|
E
ester.zhou 已提交
293 294 295

**Return value**

E
esterzhou 已提交
296 297 298
| Type      | Description                                          |
| ---------- | ---------------------------------------------- |
| [On](#on9) | **On** object that matches the long-clickable status attribute of the target component.|
E
ester.zhou 已提交
299 300 301 302

**Example**

```js
E
esterzhou 已提交
303
let on = ON.longClickable(true); // Use the static constructor ON to create an On object and specify the long-clickable status attribute of the target component.
E
add doc  
ester.zhou 已提交
304 305 306
```


E
esterzhou 已提交
307
### scrollable<sup>9+</sup>
E
add doc  
ester.zhou 已提交
308

E
esterzhou 已提交
309
scrollable(b?: boolean): On
E
add doc  
ester.zhou 已提交
310

E
ester.zhou 已提交
311
Specifies the scrollable status attribute of the target component.
E
add doc  
ester.zhou 已提交
312 313 314 315 316

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
317 318
| Name| Type   | Mandatory| Description                                                       |
| ------ | ------- | ---- | ----------------------------------------------------------- |
E
ester.zhou 已提交
319
| b      | boolean | No  | Scrollable status of the target component.<br>**true**: scrollable.<br>**false**: not scrollable.<br> Default value: **true**|
E
add doc  
ester.zhou 已提交
320 321 322

**Return value**

E
esterzhou 已提交
323 324 325
| Type      | Description                                      |
| ---------- | ------------------------------------------ |
| [On](#on9) | **On** object that matches the scrollable status attribute of the target component.|
E
add doc  
ester.zhou 已提交
326 327 328

**Example**

E
ester.zhou 已提交
329
```js
E
esterzhou 已提交
330
let on = ON.scrollable(true); // Use the static constructor ON to create an On object and specify the scrollable status attribute of the target component.
E
add doc  
ester.zhou 已提交
331 332
```

E
esterzhou 已提交
333
### enabled<sup>9+</sup>
E
add doc  
ester.zhou 已提交
334

E
esterzhou 已提交
335
enabled(b?: boolean): On
E
add doc  
ester.zhou 已提交
336

E
ester.zhou 已提交
337
Specifies the enabled status attribute of the target component.
E
add doc  
ester.zhou 已提交
338 339 340 341 342

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
343 344
| Name| Type   | Mandatory| Description                                                     |
| ------ | ------- | ---- | --------------------------------------------------------- |
E
ester.zhou 已提交
345
| b      | boolean | No  | Enabled status of the target component.<br>**true**: enabled.<br>**false**: not enabled.<br> Default value: **true**|
E
add doc  
ester.zhou 已提交
346 347 348

**Return value**

E
esterzhou 已提交
349 350 351
| Type      | Description                                    |
| ---------- | ---------------------------------------- |
| [On](#on9) | **On** object that matches the enabled status attribute of the target component.|
E
add doc  
ester.zhou 已提交
352 353 354

**Example**

E
ester.zhou 已提交
355
```js
E
esterzhou 已提交
356
let on = ON.enabled(true); // Use the static constructor ON to create an On object and specify the enabled status attribute of the target component.
E
add doc  
ester.zhou 已提交
357 358
```

E
esterzhou 已提交
359
### focused<sup>9+</sup>
E
add doc  
ester.zhou 已提交
360

E
esterzhou 已提交
361
focused(b?: boolean): On
E
add doc  
ester.zhou 已提交
362

E
ester.zhou 已提交
363
Specifies the focused status attribute of the target component.
E
add doc  
ester.zhou 已提交
364 365 366 367 368

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
369 370
| Name| Type   | Mandatory| Description                                                 |
| ------ | ------- | ---- | ----------------------------------------------------- |
E
ester.zhou 已提交
371
| b      | boolean | No  | Focused status of the target component.<br>**true**: focused.<br>**false**: not focused.<br> Default value: **true**|
E
add doc  
ester.zhou 已提交
372 373 374

**Return value**

E
esterzhou 已提交
375 376 377
| Type      | Description                                    |
| ---------- | ---------------------------------------- |
| [On](#on9) | **On** object that matches the focused status attribute of the target component.|
E
add doc  
ester.zhou 已提交
378 379 380

**Example**

E
ester.zhou 已提交
381
```js
E
esterzhou 已提交
382
let on = ON.focused(true); // Use the static constructor ON to create an On object and specify the focused status attribute of the target component.
E
add doc  
ester.zhou 已提交
383 384
```

E
esterzhou 已提交
385
### selected<sup>9+</sup>
E
add doc  
ester.zhou 已提交
386

E
esterzhou 已提交
387
selected(b?: boolean): On
E
add doc  
ester.zhou 已提交
388

E
ester.zhou 已提交
389
Specifies the selected status attribute of the target component.
E
add doc  
ester.zhou 已提交
390 391 392 393 394

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
395 396
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
397
| b      | boolean | No  | Selected status of the target component.<br>**true**: selected.<br>**false**: not selected.<br> Default value: **true**|
E
add doc  
ester.zhou 已提交
398 399 400

**Return value**

E
esterzhou 已提交
401 402 403
| Type      | Description                                      |
| ---------- | ------------------------------------------ |
| [On](#on9) | **On** object that matches the selected status attribute of the target component.|
E
ester.zhou 已提交
404 405 406 407

**Example**

```js
E
esterzhou 已提交
408
let on = ON.selected(true); // Use the static constructor ON to create an On object and specify the selected status attribute of the target component.
E
ester.zhou 已提交
409 410
```

E
esterzhou 已提交
411
### checked<sup>9+</sup>
E
ester.zhou 已提交
412

E
esterzhou 已提交
413
checked(b?: boolean): On
E
ester.zhou 已提交
414 415 416 417 418 419 420

Specifies the checked status attribute of the target component.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
421 422
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
423
| b      | boolean | No  | Checked status of the target component.<br>**true**: checked.<br>**false**: not checked.<br> Default value: **false**|
E
ester.zhou 已提交
424 425 426

**Return value**

E
esterzhou 已提交
427 428 429
| Type      | Description                                      |
| ---------- | ------------------------------------------ |
| [On](#on9) | **On** object that matches the checked status attribute of the target component.|
E
add doc  
ester.zhou 已提交
430 431 432

**Example**

E
ester.zhou 已提交
433
```js
E
esterzhou 已提交
434
let on = ON.checked(true); // Use the static constructor ON to create an On object and specify the checked status attribute of the target component.
E
add doc  
ester.zhou 已提交
435
```
E
ester.zhou 已提交
436

E
esterzhou 已提交
437
### checkable<sup>9+</sup>
E
ester.zhou 已提交
438

E
esterzhou 已提交
439
checkable(b?: boolean): On
E
ester.zhou 已提交
440 441 442 443 444 445 446

Specifies the checkable status attribute of the target component.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
447 448
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
449
| b      | boolean | No  | Checkable status of the target component.<br>**true**: checkable.<br>**false**: not checkable.<br> Default value: **false**|
E
ester.zhou 已提交
450 451 452

**Return value**

E
esterzhou 已提交
453 454 455
| Type      | Description                                        |
| ---------- | -------------------------------------------- |
| [On](#on9) | **On** object that matches the checkable status attribute of the target component.|
E
ester.zhou 已提交
456 457 458 459

**Example**

```js
E
esterzhou 已提交
460
let on = ON.checkable(true); // Use the static constructor ON to create an On object and specify the checkable status attribute of the target component.
E
add doc  
ester.zhou 已提交
461 462
```

E
esterzhou 已提交
463
### isBefore<sup>9+</sup>
E
add doc  
ester.zhou 已提交
464

E
esterzhou 已提交
465
isBefore(on: On): On
E
add doc  
ester.zhou 已提交
466

E
ester.zhou 已提交
467
Specifies that the target component is located before the given attribute component.
E
add doc  
ester.zhou 已提交
468 469 470 471 472

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
473 474
| Name| Type      | Mandatory| Description                |
| ------ | ---------- | ---- | -------------------- |
E
ester.zhou 已提交
475
| on     | [On](#on9) | Yes  | Information about the attribute component.|
E
add doc  
ester.zhou 已提交
476 477 478

**Return value**

E
esterzhou 已提交
479 480 481
| Type      | Description                                                |
| ---------- | ---------------------------------------------------- |
| [On](#on9) | **On** object.|
E
add doc  
ester.zhou 已提交
482 483 484

**Example**

E
ester.zhou 已提交
485
```js
E
ester.zhou 已提交
486
let on = ON.isBefore(ON.text('123')); // Create an On object using the static constructor ON, specifying that the target component is located before the given attribute component.
E
add doc  
ester.zhou 已提交
487 488
```

E
esterzhou 已提交
489
### isAfter<sup>9+</sup>
E
add doc  
ester.zhou 已提交
490

E
esterzhou 已提交
491
isAfter(on: On): On
E
add doc  
ester.zhou 已提交
492

E
ester.zhou 已提交
493
Specifies that the target component is located after the given attribute component.
E
add doc  
ester.zhou 已提交
494 495 496 497 498

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
499 500
| Name| Type      | Mandatory| Description                |
| ------ | ---------- | ---- | -------------------- |
E
ester.zhou 已提交
501
| on     | [On](#on9) | Yes  | Information about the attribute component.|
E
add doc  
ester.zhou 已提交
502 503 504

**Return value**

E
esterzhou 已提交
505 506 507
| Type      | Description                                                |
| ---------- | ---------------------------------------------------- |
| [On](#on9) | **On** object.|
E
add doc  
ester.zhou 已提交
508 509 510

**Example**

E
ester.zhou 已提交
511
```js
E
ester.zhou 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
let on = ON.isAfter(ON.text('123')); // Create an On object using the static constructor ON, specifying that the target component is located after the given attribute component.
```

### within<sup>10+</sup>

within(on: On): On

Specifies that the target component is located within the given attribute component.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type      | Mandatory| Description                |
| ------ | ---------- | ---- | -------------------- |
| on     | [On](#on9) | Yes  | Information about the attribute component.|

**Return value**

| Type      | Description                                              |
| ---------- | -------------------------------------------------- |
| [On](#on9) | **On** object.|

**Example**

```js
let on = ON.within(ON.type('List')); // Create an On object using the static constructor ON, specifying that the target component is located within the given attribute component.
```

### inWindow<sup>10+</sup>

inWindow(bundleName: string): On;

E
ester.zhou 已提交
545
Specifies that the target component is located within the given application window.
E
ester.zhou 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name    | Type  | Mandatory| Description            |
| ---------- | ------ | ---- | ---------------- |
| bundleName | string | Yes  | Bundle name of the application window.|

**Return value**

| Type      | Description                                          |
| ---------- | ---------------------------------------------- |
| [On](#on9) | **On** object.|

**Example**

```js
E
ester.zhou 已提交
564
let on = ON.inWindow('com.uitestScene.acts'); // Create an On object using the static constructor ON, specifying that the target component is located within the given application window.
E
add doc  
ester.zhou 已提交
565 566
```

E
esterzhou 已提交
567
## Component<sup>9+</sup>
E
add doc  
ester.zhou 已提交
568

E
ester.zhou 已提交
569 570
Represents a component on the UI and provides APIs for obtaining component attributes, clicking a component, scrolling to search for a component, and text injection.

E
ester.zhou 已提交
571 572
All APIs provided in this class use a promise to return the result and must be invoked using **await**.

E
esterzhou 已提交
573
### click<sup>9+</sup>
E
add doc  
ester.zhou 已提交
574

E
ester.zhou 已提交
575
click(): Promise\<void>
E
add doc  
ester.zhou 已提交
576 577 578 579 580

Clicks this component.

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
581 582 583 584 585 586
**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
587 588
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
589

E
add doc  
ester.zhou 已提交
590 591
**Example**

E
ester.zhou 已提交
592
```js
E
ester.zhou 已提交
593
async function demo() {
E
esterzhou 已提交
594 595 596
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    await button.click();
E
ester.zhou 已提交
597
}
E
add doc  
ester.zhou 已提交
598 599
```

E
esterzhou 已提交
600
### doubleClick<sup>9+</sup>
E
add doc  
ester.zhou 已提交
601

E
ester.zhou 已提交
602
doubleClick(): Promise\<void>
E
add doc  
ester.zhou 已提交
603 604 605 606 607

Double-clicks this component.

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
608 609 610 611 612 613
**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
614 615
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
616

E
add doc  
ester.zhou 已提交
617 618
**Example**

E
ester.zhou 已提交
619
```js
E
ester.zhou 已提交
620
async function demo() {
E
esterzhou 已提交
621 622 623
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    await button.doubleClick();
E
ester.zhou 已提交
624
}
E
add doc  
ester.zhou 已提交
625 626
```

E
esterzhou 已提交
627
### longClick<sup>9+</sup>
E
add doc  
ester.zhou 已提交
628

E
ester.zhou 已提交
629
longClick(): Promise\<void>
E
add doc  
ester.zhou 已提交
630 631 632 633 634

Long-clicks this component.

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
635 636 637 638 639 640
**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
641 642
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
643

E
add doc  
ester.zhou 已提交
644 645
**Example**

E
ester.zhou 已提交
646
```js
E
ester.zhou 已提交
647
async function demo() {
E
esterzhou 已提交
648 649 650
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    await button.longClick();
E
ester.zhou 已提交
651
}
E
add doc  
ester.zhou 已提交
652 653
```

E
esterzhou 已提交
654
### getId<sup>9+</sup>
E
add doc  
ester.zhou 已提交
655

E
esterzhou 已提交
656
getId(): Promise\<string>
E
add doc  
ester.zhou 已提交
657 658 659 660 661 662 663

Obtains the ID of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
ester.zhou 已提交
664 665
| Type            | Description                           |
| ---------------- | ------------------------------- |
E
esterzhou 已提交
666
| Promise\<string> | Promise used to return the ID of the component.|
E
add doc  
ester.zhou 已提交
667

E
esterzhou 已提交
668
**Error codes**
E
add doc  
ester.zhou 已提交
669

E
esterzhou 已提交
670
For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).
E
add doc  
ester.zhou 已提交
671

E
esterzhou 已提交
672 673
| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
674 675
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
676 677 678

**Example**

E
ester.zhou 已提交
679
```js
E
ester.zhou 已提交
680
async function demo() {
E
esterzhou 已提交
681 682 683
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    let num = await button.getId();
E
ester.zhou 已提交
684
}
E
add doc  
ester.zhou 已提交
685 686
```

E
esterzhou 已提交
687
### getText<sup>9+</sup>
E
add doc  
ester.zhou 已提交
688

E
ester.zhou 已提交
689
getText(): Promise\<string>
E
add doc  
ester.zhou 已提交
690 691 692 693 694 695 696

Obtains the text information of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
ester.zhou 已提交
697 698 699
| Type            | Description                             |
| ---------------- | --------------------------------- |
| Promise\<string> | Promise used to return the text information of the component.|
E
add doc  
ester.zhou 已提交
700

E
esterzhou 已提交
701 702 703 704 705 706
**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
707 708
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
709

E
add doc  
ester.zhou 已提交
710 711
**Example**

E
ester.zhou 已提交
712
```js
E
ester.zhou 已提交
713
async function demo() {
E
esterzhou 已提交
714 715 716
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    let text = await button.getText();
E
ester.zhou 已提交
717
}
E
add doc  
ester.zhou 已提交
718 719
```

E
esterzhou 已提交
720
### getType<sup>9+</sup>
E
add doc  
ester.zhou 已提交
721

E
ester.zhou 已提交
722
getType(): Promise\<string>
E
add doc  
ester.zhou 已提交
723 724 725 726 727 728 729

Obtains the type of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
ester.zhou 已提交
730 731 732
| Type            | Description                         |
| ---------------- | ----------------------------- |
| Promise\<string> | Promise used to return the type of the component.|
E
add doc  
ester.zhou 已提交
733

E
esterzhou 已提交
734 735 736 737 738 739
**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
740 741
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
742

E
add doc  
ester.zhou 已提交
743 744
**Example**

E
ester.zhou 已提交
745
```js
E
ester.zhou 已提交
746
async function demo() {
E
esterzhou 已提交
747 748 749
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    let type = await button.getType();
E
ester.zhou 已提交
750
}
E
add doc  
ester.zhou 已提交
751 752
```

E
esterzhou 已提交
753
### getBounds<sup>9+</sup>
E
ester.zhou 已提交
754 755 756 757 758 759 760 761 762

getBounds(): Promise\<Rect>

Obtains the bounds of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
763 764 765 766 767 768 769 770 771 772
| Type                    | Description                                 |
| ------------------------ | ------------------------------------- |
| Promise\<[Rect](#rect9)> | Promise used to return the bounds of the component.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
773 774
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    let rect = await button.getBounds();
}
```

### getBoundsCenter<sup>9+</sup>

getBoundsCenter(): Promise\<Point>

Obtains the information about the center of the bounding box around this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type                      | Description                                           |
| -------------------------- | ----------------------------------------------- |
| Promise\<[Point](#point9)> | Promise used to return the information about the center of the bounding box around the component.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
806 807
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
ester.zhou 已提交
808 809 810 811 812

**Example**

```js
async function demo() {
E
esterzhou 已提交
813 814 815
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    let point = await button.getBoundsCenter();
E
ester.zhou 已提交
816 817 818
}
```

E
esterzhou 已提交
819
### isClickable<sup>9+</sup>
E
add doc  
ester.zhou 已提交
820

E
esterzhou 已提交
821
isClickable(): Promise\<boolean>
E
add doc  
ester.zhou 已提交
822 823 824 825 826 827 828

Obtains the clickable status of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
829 830 831 832 833 834 835 836 837 838
| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is clickable, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
839 840
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
841 842 843

**Example**

E
ester.zhou 已提交
844
```js
E
ester.zhou 已提交
845
async function demo() {
E
esterzhou 已提交
846 847
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
E
ester.zhou 已提交
848
    if(await button.isClickable()) {
E
esterzhou 已提交
849 850 851
        console.info('This button can be Clicked');
    } else {
        console.info('This button can not be Clicked');
E
ester.zhou 已提交
852 853
    }
}
E
add doc  
ester.zhou 已提交
854 855
```

E
esterzhou 已提交
856
### isLongClickable<sup>9+</sup>
E
add doc  
ester.zhou 已提交
857

E
esterzhou 已提交
858
isLongClickable(): Promise\<boolean> 
E
add doc  
ester.zhou 已提交
859

E
esterzhou 已提交
860
Obtains the long-clickable status of this component.
E
ester.zhou 已提交
861 862 863 864 865

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
866 867 868 869 870 871 872 873 874 875
| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the long-clickable status of the component. The value **true** means that the component is long-clickable, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
876 877
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
ester.zhou 已提交
878 879 880 881 882

**Example**

```js
async function demo() {
E
esterzhou 已提交
883 884
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
E
ester.zhou 已提交
885
    if(await button.isLongClickable()) {
E
esterzhou 已提交
886 887 888
        console.info('This button can longClick');
    } else {
        console.info('This button can not longClick');
E
ester.zhou 已提交
889 890 891 892
    }
}
```

E
esterzhou 已提交
893
### isChecked<sup>9+</sup>
E
ester.zhou 已提交
894

E
esterzhou 已提交
895
isChecked(): Promise\<boolean>
E
add doc  
ester.zhou 已提交
896

E
ester.zhou 已提交
897
Obtains the checked status of this component.
E
add doc  
ester.zhou 已提交
898 899 900 901 902

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
903 904 905 906 907 908 909 910 911 912
| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the checked status of the component. The value **true** means that the component is checked, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
913 914
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
915 916 917

**Example**

E
ester.zhou 已提交
918 919
```js
async function demo() {
E
esterzhou 已提交
920 921
    let driver = Driver.create();
    let checkBox = await driver.findComponent(ON.type('Checkbox'));
E
ester.zhou 已提交
922
    if(await checkBox.isChecked()) {
E
esterzhou 已提交
923 924 925
        console.info('This checkBox is checked');
    } else {
        console.info('This checkBox is not checked');
E
ester.zhou 已提交
926 927
    }
}
E
add doc  
ester.zhou 已提交
928
```
E
ester.zhou 已提交
929

E
esterzhou 已提交
930
### isCheckable<sup>9+</sup>
E
ester.zhou 已提交
931

E
esterzhou 已提交
932
isCheckable(): Promise\<boolean>
E
ester.zhou 已提交
933

E
esterzhou 已提交
934
Obtains the checkable status of this component.
E
ester.zhou 已提交
935 936 937 938 939

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
940 941 942 943 944 945 946 947 948 949
| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the checkable status of the component. The value **true** means that the component is checkable, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
950 951
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
ester.zhou 已提交
952 953 954 955 956

**Example**

```js
async function demo() {
E
esterzhou 已提交
957 958
    let driver = Driver.create();
    let checkBox = await driver.findComponent(ON.type('Checkbox'));
E
ester.zhou 已提交
959
    if(await checkBox.isCheckable()) {
E
esterzhou 已提交
960 961 962
        console.info('This checkBox is checkable');
    } else {
        console.info('This checkBox is not checkable');
E
ester.zhou 已提交
963 964 965 966
    }
}
```

E
esterzhou 已提交
967
### isScrollable<sup>9+</sup>
E
ester.zhou 已提交
968

E
esterzhou 已提交
969
isScrollable(): Promise\<boolean>
E
ester.zhou 已提交
970 971 972 973 974 975 976

Obtains the scrollable status of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
977 978 979 980 981 982 983 984 985 986
| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is scrollable, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
987 988
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
ester.zhou 已提交
989 990 991 992

**Example**

```js
E
ester.zhou 已提交
993
async function demo() {
E
esterzhou 已提交
994 995
    let driver = Driver.create();
    let scrollBar = await driver.findComponent(ON.scrollable(true));
E
ester.zhou 已提交
996
    if(await scrollBar.isScrollable()) {
E
esterzhou 已提交
997 998 999
        console.info('This scrollBar can be operated');
    } else {
        console.info('This scrollBar can not be operated');
E
ester.zhou 已提交
1000 1001
    }
}
E
add doc  
ester.zhou 已提交
1002 1003 1004
```


E
esterzhou 已提交
1005
### isEnabled<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1006

E
esterzhou 已提交
1007
isEnabled(): Promise\<boolean>
E
add doc  
ester.zhou 已提交
1008

E
ester.zhou 已提交
1009
Obtains the enabled status of this component.
E
add doc  
ester.zhou 已提交
1010 1011 1012 1013 1014

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
| Type             | Description                                                      |
| ----------------- | ---------------------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is enabled, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1025 1026
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
1027 1028 1029

**Example**

E
ester.zhou 已提交
1030
```js
E
ester.zhou 已提交
1031
async function demo() {
E
esterzhou 已提交
1032 1033
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
E
ester.zhou 已提交
1034
    if(await button.isEnabled()) {
E
esterzhou 已提交
1035 1036 1037
        console.info('This button can be operated');
    } else {
        console.info('This button can not be operated');
E
ester.zhou 已提交
1038 1039 1040
    }
}

E
add doc  
ester.zhou 已提交
1041 1042
```

E
esterzhou 已提交
1043
### isFocused<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1044

E
esterzhou 已提交
1045
isFocused(): Promise\<boolean>
E
add doc  
ester.zhou 已提交
1046 1047 1048 1049 1050 1051 1052

Obtains the focused status of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the focused status of the component. The value **true** means that the component is focused, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1063 1064
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
1065 1066 1067

**Example**

E
ester.zhou 已提交
1068
```js
E
ester.zhou 已提交
1069
async function demo() {
E
esterzhou 已提交
1070 1071
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
E
ester.zhou 已提交
1072
    if(await button.isFocused()) {
E
esterzhou 已提交
1073 1074 1075
        console.info('This button is focused');
    } else {
        console.info('This button is not focused');
E
ester.zhou 已提交
1076 1077
	}
}
E
add doc  
ester.zhou 已提交
1078 1079
```

E
esterzhou 已提交
1080
### isSelected<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1081

E
esterzhou 已提交
1082
isSelected(): Promise\<boolean>
E
add doc  
ester.zhou 已提交
1083 1084 1085 1086 1087 1088 1089

Obtains the selected status of this component.

**System capability**: SystemCapability.Test.UiTest

**Return value**

E
esterzhou 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
| Type             | Description                                               |
| ----------------- | --------------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is selected, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1100 1101
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
1102 1103 1104

**Example**

E
ester.zhou 已提交
1105
```js
E
ester.zhou 已提交
1106
async function demo() {
E
esterzhou 已提交
1107 1108
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
E
ester.zhou 已提交
1109
    if(await button.isSelected()) {
E
esterzhou 已提交
1110 1111 1112
        console.info('This button is selected');
	} else {
        console.info('This button is not selected');
E
ester.zhou 已提交
1113 1114
    }
}
E
add doc  
ester.zhou 已提交
1115 1116
```

E
esterzhou 已提交
1117
### inputText<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1118

E
ester.zhou 已提交
1119
inputText(text: string): Promise\<void>
E
add doc  
ester.zhou 已提交
1120 1121 1122 1123 1124 1125 1126

Enters text into this component (available for text boxes).

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
| Name| Type  | Mandatory| Description                                    |
| ------ | ------ | ---- | ---------------------------------------- |
| text   | string | Yes  | Text to enter, which can contain English and special characters.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1137 1138
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
1139 1140 1141

**Example**

E
ester.zhou 已提交
1142 1143
```js
async function demo() {
E
esterzhou 已提交
1144 1145 1146
    let driver = Driver.create();
    let text = await driver.findComponent(ON.text('hello world'));
    await text.inputText('123');
E
ester.zhou 已提交
1147
}
E
add doc  
ester.zhou 已提交
1148
```
E
ester.zhou 已提交
1149

E
esterzhou 已提交
1150
### clearText<sup>9+</sup>
E
ester.zhou 已提交
1151 1152 1153

clearText(): Promise\<void>

E
esterzhou 已提交
1154
Clears text in this component. This API is applicable to text boxes.
E
ester.zhou 已提交
1155 1156 1157

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
1158 1159 1160 1161
**Error codes**

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1162 1163
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
1164

E
ester.zhou 已提交
1165 1166 1167
**Example**

```js
E
ester.zhou 已提交
1168
async function demo() {
E
esterzhou 已提交
1169 1170 1171
    let driver = Driver.create();
    let text = await driver.findComponent(ON.text('hello world'));
    await text.clearText();
E
ester.zhou 已提交
1172
}
E
add doc  
ester.zhou 已提交
1173 1174
```

E
esterzhou 已提交
1175
### scrollSearch<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1176

E
esterzhou 已提交
1177
scrollSearch(on: On): Promise\<Component>
E
add doc  
ester.zhou 已提交
1178

E
esterzhou 已提交
1179
Scrolls on this component to search for the target component. This API is applicable to components that support scrolling.
E
add doc  
ester.zhou 已提交
1180 1181 1182 1183 1184

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1185 1186 1187
| Name| Type      | Mandatory| Description                |
| ------ | ---------- | ---- | -------------------- |
| on     | [On](#on9) | Yes  | Attributes of the target component.|
E
add doc  
ester.zhou 已提交
1188 1189 1190

**Return value**

E
esterzhou 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
| Type                              | Description                                 |
| ---------------------------------- | ------------------------------------- |
| Promise\<[Component](#component9)> | Promise used to return the target component.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1201 1202
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
1203 1204 1205

**Example**

E
ester.zhou 已提交
1206
```js
E
ester.zhou 已提交
1207
async function demo() {
E
esterzhou 已提交
1208
    let driver = Driver.create();
E
ester.zhou 已提交
1209
    let scrollBar = await driver.findComponent(ON.type('Scroll'));
E
esterzhou 已提交
1210
    let button = await scrollBar.scrollSearch(ON.text('next page'));
E
ester.zhou 已提交
1211
}
E
add doc  
ester.zhou 已提交
1212 1213
```

E
esterzhou 已提交
1214
### scrollToTop<sup>9+</sup>
E
ester.zhou 已提交
1215

E
esterzhou 已提交
1216
scrollToTop(speed?: number): Promise\<void>
E
ester.zhou 已提交
1217

E
esterzhou 已提交
1218
Scrolls to the top of this component. This API is applicable to components that support scrolling.
E
ester.zhou 已提交
1219 1220 1221

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
**Parameters**

| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| speed  | number | No  | Scroll speed, in pixel/s. The value ranges from 200 to 15000. If the set value is not in the range, the default value 600 is used.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1234 1235
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
1236

E
ester.zhou 已提交
1237 1238 1239 1240
**Example**

```js
async function demo() {
E
esterzhou 已提交
1241 1242 1243
    let driver = Driver.create();
    let scrollBar = await driver.findComponent(ON.type('Scroll'));
    await scrollBar.scrollToTop();
E
ester.zhou 已提交
1244 1245 1246
}
```

E
esterzhou 已提交
1247
### scrollToBottom<sup>9+</sup>
E
ester.zhou 已提交
1248

E
esterzhou 已提交
1249
scrollToBottom(speed?: number): Promise\<void>
E
ester.zhou 已提交
1250

E
esterzhou 已提交
1251
Scrolls to the bottom of this component. This API is applicable to components that support scrolling.
E
ester.zhou 已提交
1252 1253 1254

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
**Parameters**

| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| speed  | number | No  | Scroll speed, in pixel/s. The value ranges from 200 to 15000. If the set value is not in the range, the default value 600 is used.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1267 1268
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
esterzhou 已提交
1269

E
ester.zhou 已提交
1270 1271 1272 1273
**Example**

```js
async function demo() {
E
esterzhou 已提交
1274 1275 1276
    let driver = Driver.create();
    let scrollBar = await driver.findComponent(ON.type('Scroll'));
    await scrollBar.scrollToBottom();
E
ester.zhou 已提交
1277 1278 1279
}
```

E
esterzhou 已提交
1280
### dragTo<sup>9+</sup>
E
ester.zhou 已提交
1281

E
esterzhou 已提交
1282
dragTo(target: Component): Promise\<void>
E
ester.zhou 已提交
1283 1284 1285 1286 1287 1288 1289

Drags this component to the target component.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
| Name| Type                    | Mandatory| Description      |
| ------ | ------------------------ | ---- | ---------- |
| target | [Component](#component9) | Yes  | Target component.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1300 1301
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
ester.zhou 已提交
1302 1303 1304 1305 1306

**Example**

```js
async function demo() {
E
esterzhou 已提交
1307 1308 1309 1310
    let driver = Driver.create();
    let button = await driver.findComponent(ON.type('button'));
    let text = await driver.findComponent(ON.text('hello world'));
    await button.dragTo(text);
E
ester.zhou 已提交
1311 1312 1313
    }
```

E
esterzhou 已提交
1314
### pinchOut<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1315

E
esterzhou 已提交
1316
pinchOut(scale: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1317

E
esterzhou 已提交
1318
Pinches a component to scale it up to the specified ratio.
E
add doc  
ester.zhou 已提交
1319

E
esterzhou 已提交
1320
**System capability**: SystemCapability.Test.UiTest
E
add doc  
ester.zhou 已提交
1321

E
esterzhou 已提交
1322
**Parameters**
E
add doc  
ester.zhou 已提交
1323

E
esterzhou 已提交
1324 1325 1326
| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| scale  | number | Yes  | Scale factor.|
E
add doc  
ester.zhou 已提交
1327

E
esterzhou 已提交
1328 1329 1330
**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).
E
add doc  
ester.zhou 已提交
1331

E
esterzhou 已提交
1332 1333
| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1334 1335
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
1336 1337 1338

**Example**

E
ester.zhou 已提交
1339
```js
E
ester.zhou 已提交
1340
async function demo() {
E
esterzhou 已提交
1341 1342 1343 1344
    let driver = Driver.create();
    let image = await driver.findComponent(ON.type('image'));
    await image.pinchOut(1.5);
    }
E
add doc  
ester.zhou 已提交
1345 1346
```

E
esterzhou 已提交
1347
### pinchIn<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1348

E
esterzhou 已提交
1349
pinchIn(scale: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1350

E
esterzhou 已提交
1351
Pinches a component to scale it down to the specified ratio.
E
add doc  
ester.zhou 已提交
1352 1353 1354 1355 1356

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| scale  | number | Yes  | Scale factor.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1367 1368
| 17000002 | if the async function was not called with await. |
| 17000004 | if the component is invisible or destroyed.           |
E
add doc  
ester.zhou 已提交
1369 1370 1371

**Example**

E
ester.zhou 已提交
1372
```js
E
ester.zhou 已提交
1373
async function demo() {
E
esterzhou 已提交
1374 1375 1376 1377
    let driver = Driver.create();
    let image = await driver.findComponent(ON.type('image'));
    await image.pinchIn(0.5);
    }
E
add doc  
ester.zhou 已提交
1378 1379
```

E
esterzhou 已提交
1380
## Driver<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1381

E
esterzhou 已提交
1382
The **Driver** class is the main entry to the UiTest framework. It provides APIs for features such as component matching/search, key injection, coordinate clicking/sliding, and screenshot.
E
ester.zhou 已提交
1383

E
esterzhou 已提交
1384
All APIs provided by this class, except for **Driver.create()**, use a promise to return the result and must be invoked using **await**.
E
add doc  
ester.zhou 已提交
1385

E
esterzhou 已提交
1386
### create<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1387

E
esterzhou 已提交
1388
static create(): Driver
E
add doc  
ester.zhou 已提交
1389

E
esterzhou 已提交
1390
Creates a **Driver** object and returns the object created. This API is a static API.
E
add doc  
ester.zhou 已提交
1391

E
esterzhou 已提交
1392
**System capability**: SystemCapability.Test.UiTest
E
add doc  
ester.zhou 已提交
1393 1394 1395

**Return value**

E
esterzhou 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
| Type| Description          |
| -------- | ---------------------- |
| Driver   | **Driver** object created.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message          |
| -------- | ------------------ |
E
ester.zhou 已提交
1406
| 17000001 | if the test framework failed to initialize. |
E
add doc  
ester.zhou 已提交
1407 1408 1409

**Example**

E
ester.zhou 已提交
1410
```js
E
ester.zhou 已提交
1411
async function demo() {
E
esterzhou 已提交
1412
    let driver = Driver.create();
E
ester.zhou 已提交
1413
}
E
add doc  
ester.zhou 已提交
1414 1415
```

E
esterzhou 已提交
1416
### delayMs<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1417

E
esterzhou 已提交
1418
delayMs(duration: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1419

E
esterzhou 已提交
1420
Delays this **Driver** object within the specified duration.
E
add doc  
ester.zhou 已提交
1421 1422 1423 1424 1425

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1426 1427 1428
| Name  | Type  | Mandatory| Description                  |
| -------- | ------ | ---- | ---------------------- |
| duration | number | Yes  | Duration of time, in ms.|
E
add doc  
ester.zhou 已提交
1429

E
esterzhou 已提交
1430 1431 1432
**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).
E
add doc  
ester.zhou 已提交
1433

E
esterzhou 已提交
1434 1435
| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1436
| 17000002 | if the async function was not called with await. |
E
add doc  
ester.zhou 已提交
1437 1438 1439

**Example**

E
ester.zhou 已提交
1440
```js
E
ester.zhou 已提交
1441
async function demo() {
E
esterzhou 已提交
1442 1443
    let driver = Driver.create();
    await driver.delayMs(1000);
E
ester.zhou 已提交
1444
}
E
add doc  
ester.zhou 已提交
1445 1446
```

E
esterzhou 已提交
1447
### findComponent<sup>9+</sup>
E
ester.zhou 已提交
1448

E
esterzhou 已提交
1449
findComponent(on: On): Promise\<Component>
E
ester.zhou 已提交
1450

E
esterzhou 已提交
1451
Searches this **Driver** object for the target component that matches the given attributes.
E
ester.zhou 已提交
1452 1453 1454 1455 1456

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1457 1458 1459
| Name| Type      | Mandatory| Description                |
| ------ | ---------- | ---- | -------------------- |
| on     | [On](#on9) | Yes  | Attributes of the target component.|
E
ester.zhou 已提交
1460 1461 1462

**Return value**

E
esterzhou 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
| Type                              | Description                             |
| ---------------------------------- | --------------------------------- |
| Promise\<[Component](#component9)> | Promise used to return the found component.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1473
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
1474 1475 1476 1477 1478

**Example**

```js
async function demo() {
E
esterzhou 已提交
1479 1480
    let driver = Driver.create();
    let button = await driver.findComponent(ON.text('next page'));
E
ester.zhou 已提交
1481 1482 1483
}
```

E
esterzhou 已提交
1484
### findComponents<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1485

E
esterzhou 已提交
1486
findComponents(on: On): Promise\<Array\<Component>>
E
add doc  
ester.zhou 已提交
1487

E
esterzhou 已提交
1488
Searches this **Driver** object for all components that match the given attributes.
E
add doc  
ester.zhou 已提交
1489 1490 1491 1492 1493

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1494 1495 1496
| Name| Type      | Mandatory| Description                |
| ------ | ---------- | ---- | -------------------- |
| on     | [On](#on9) | Yes  | Attributes of the target component.|
E
add doc  
ester.zhou 已提交
1497

E
esterzhou 已提交
1498
**Return value**
E
add doc  
ester.zhou 已提交
1499

E
esterzhou 已提交
1500 1501 1502
| Type                                      | Description                                   |
| ------------------------------------------ | --------------------------------------- |
| Promise\<Array\<[Component](#component9)>> | Promise used to return a list of found components.|
E
add doc  
ester.zhou 已提交
1503

E
esterzhou 已提交
1504
**Error codes**
E
add doc  
ester.zhou 已提交
1505

E
esterzhou 已提交
1506
For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).
E
add doc  
ester.zhou 已提交
1507

E
esterzhou 已提交
1508 1509
| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1510
| 17000002 | if the async function was not called with await. |
E
add doc  
ester.zhou 已提交
1511 1512 1513

**Example**

E
ester.zhou 已提交
1514
```js
E
ester.zhou 已提交
1515
async function demo() {
E
esterzhou 已提交
1516 1517
    let driver = Driver.create();
    let buttonList = await driver.findComponents(ON.text('next page'));
E
ester.zhou 已提交
1518
}
E
add doc  
ester.zhou 已提交
1519 1520
```

E
esterzhou 已提交
1521
### findWindow<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1522

E
esterzhou 已提交
1523
findWindow(filter: WindowFilter): Promise\<UiWindow>
E
add doc  
ester.zhou 已提交
1524

E
esterzhou 已提交
1525
Searches for the window that matches the specified attributes.
E
add doc  
ester.zhou 已提交
1526 1527 1528 1529 1530

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
| Name| Type                          | Mandatory| Description            |
| ------ | ------------------------------ | ---- | ---------------- |
| filter | [WindowFilter](#windowfilter9) | Yes  | Attributes of the target window.|

**Return value**

| Type                            | Description                                 |
| -------------------------------- | ------------------------------------- |
| Promise\<[UiWindow](#uiwindow9)> | Promise used to return the target window.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1547
| 17000002 | if the async function was not called with await. |
E
add doc  
ester.zhou 已提交
1548 1549 1550

**Example**

E
ester.zhou 已提交
1551
```js
E
ester.zhou 已提交
1552
async function demo() {
E
esterzhou 已提交
1553 1554
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
E
ester.zhou 已提交
1555
}
E
add doc  
ester.zhou 已提交
1556 1557
```

E
esterzhou 已提交
1558
### waitForComponent<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1559

E
esterzhou 已提交
1560
waitForComponent(on: On, time: number): Promise\<Component>
E
add doc  
ester.zhou 已提交
1561

E
esterzhou 已提交
1562
Searches this **Driver** object for the target component that matches the given attributes within the specified duration.
E
add doc  
ester.zhou 已提交
1563 1564 1565 1566 1567

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576
| Name| Type      | Mandatory| Description                            |
| ------ | ---------- | ---- | -------------------------------- |
| On     | [On](#on9) | Yes  | Attributes of the target component.            |
| time   | number     | Yes  | Duration for searching for the target component, in ms.|

**Return value**

| Type                             | Description                             |
| --------------------------------- | --------------------------------- |
E
ester.zhou 已提交
1577
| Promise\<[Component](#component9)> | Promise used to return the found component.|
E
esterzhou 已提交
1578 1579 1580 1581 1582 1583 1584

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1585
| 17000002 | if the async function was not called with await. |
E
add doc  
ester.zhou 已提交
1586 1587 1588

**Example**

E
ester.zhou 已提交
1589
```js
E
ester.zhou 已提交
1590
async function demo() {
E
esterzhou 已提交
1591 1592
    let driver = Driver.create();
    let button = await driver.waitForComponent(ON.text('next page'),500);
E
ester.zhou 已提交
1593
}
E
add doc  
ester.zhou 已提交
1594 1595
```

E
esterzhou 已提交
1596
### assertComponentExist<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1597

E
esterzhou 已提交
1598
assertComponentExist(on: On): Promise\<void>
E
add doc  
ester.zhou 已提交
1599

E
esterzhou 已提交
1600
Asserts that a component that matches the given attributes exists on the current page.
E
add doc  
ester.zhou 已提交
1601 1602 1603 1604 1605

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
esterzhou 已提交
1606 1607 1608
| Name| Type      | Mandatory| Description                |
| ------ | ---------- | ---- | -------------------- |
| on     | [On](#on9) | Yes  | Attributes of the target component.|
E
add doc  
ester.zhou 已提交
1609

E
esterzhou 已提交
1610
**Error codes**
E
add doc  
ester.zhou 已提交
1611

E
esterzhou 已提交
1612 1613 1614 1615
For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1616 1617
| 17000002 | if the async function was not called with await. |
| 17000003 | if the assertion failed.    |
E
esterzhou 已提交
1618 1619 1620 1621

**Example**

```js
E
ester.zhou 已提交
1622
async function demo() {
E
esterzhou 已提交
1623 1624
    let driver = Driver.create();
    await driver.assertComponentExist(ON.text('next page'));
E
ester.zhou 已提交
1625
}
E
add doc  
ester.zhou 已提交
1626 1627
```

E
esterzhou 已提交
1628
### pressBack<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1629

E
esterzhou 已提交
1630
pressBack(): Promise\<void>
E
add doc  
ester.zhou 已提交
1631

E
esterzhou 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
Presses the Back button on this **Driver** object.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1642
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.pressBack();
}
```

### triggerKey<sup>9+</sup>

triggerKey(keyCode: number): Promise\<void>

Triggers the key of this **Driver** object that matches the given key code.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name | Type  | Mandatory| Description         |
| ------- | ------ | ---- | ------------- |
| keyCode | number | Yes  | Key code.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1673
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.triggerKey(123);
}
```

### triggerCombineKeys<sup>9+</sup>

triggerCombineKeys(key0: number, key1: number, key2?: number): Promise\<void>

Triggers a key combination based on the specified key values. For example, if the value of **Key** is (2072, 2019), the **Driver** object finds and clicks the key combination that matches the value, for example, **Ctrl+C**.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description               |
| ------ | ------ | ---- | ------------------- |
| key0   | number | Yes  | The first key value.|
| key1   | number | Yes  | The second key value.|
| key2   | number | No  | The third key value.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1706
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.triggerCombineKeys(2072, 2047, 2035);
}
```


### click<sup>9+</sup>

click(x: number, y: number): Promise\<void>

Clicks a specific point of this **Driver** object based on the given coordinates.
E
add doc  
ester.zhou 已提交
1723 1724 1725 1726 1727

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
ester.zhou 已提交
1728 1729
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
E
esterzhou 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738
| x      | number | Yes  | X-coordinate of the target point.|
| y      | number | Yes  | Y-coordinate of the target point.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1739
| 17000002 | if the async function was not called with await. |
E
add doc  
ester.zhou 已提交
1740 1741 1742

**Example**

E
ester.zhou 已提交
1743
```js
E
ester.zhou 已提交
1744
async function demo() {
E
esterzhou 已提交
1745 1746
    let driver = Driver.create();
    await driver.click(100,100);
E
ester.zhou 已提交
1747
}
E
add doc  
ester.zhou 已提交
1748 1749
```

E
esterzhou 已提交
1750
### doubleClick<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1751

E
esterzhou 已提交
1752
doubleClick(x: number, y: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1753

E
esterzhou 已提交
1754
Double-clicks a specific point of this **Driver** object based on the given coordinates.
E
add doc  
ester.zhou 已提交
1755 1756 1757 1758 1759

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
ester.zhou 已提交
1760 1761
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
E
esterzhou 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770
| x      | number | Yes  | X-coordinate of the target point.|
| y      | number | Yes  | Y-coordinate of the target point.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1771
| 17000002 | if the async function was not called with await. |
E
add doc  
ester.zhou 已提交
1772 1773 1774

**Example**

E
ester.zhou 已提交
1775
```js
E
ester.zhou 已提交
1776
async function demo() {
E
esterzhou 已提交
1777 1778
    let driver = Driver.create();
    await driver.doubleClick(100,100);
E
ester.zhou 已提交
1779
}
E
add doc  
ester.zhou 已提交
1780 1781
```

E
esterzhou 已提交
1782
### longClick<sup>9+</sup>
E
ester.zhou 已提交
1783

E
esterzhou 已提交
1784
longClick(x: number, y: number): Promise\<void>
E
ester.zhou 已提交
1785

E
esterzhou 已提交
1786
Long-clicks a specific point of this **Driver** object based on the given coordinates.
E
ester.zhou 已提交
1787 1788 1789 1790 1791 1792 1793

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
E
esterzhou 已提交
1794 1795 1796 1797 1798 1799 1800 1801 1802
| x      | number | Yes  | X-coordinate of the target point.|
| y      | number | Yes  | Y-coordinate of the target point.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1803
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
1804 1805 1806 1807 1808

**Example**

```js
async function demo() {
E
esterzhou 已提交
1809 1810
    let driver = Driver.create();
    await driver.longClick(100,100);
E
ester.zhou 已提交
1811 1812 1813
}
```

E
esterzhou 已提交
1814
### swipe<sup>9+</sup>
E
add doc  
ester.zhou 已提交
1815

E
esterzhou 已提交
1816
swipe(startx: number, starty: number, endx: number, endy: number, speed?: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1817

E
esterzhou 已提交
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
Swipes on this **Driver** object from the given start point to the given end point.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| startx | number | Yes  | X-coordinate of the start point.                      |
| starty | number | Yes  | Y-coordinate of the start point.                      |
| endx   | number | Yes  | X-coordinate of the end point.                      |
| endy   | number | Yes  | Y-coordinate of the end point.                      |
| speed  | number | No  | Scroll speed, in pixel/s. The value ranges from 200 to 15000. If the set value is not in the range, the default value 600 is used.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1838
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.swipe(100,100,200,200,600);
}
```

### drag<sup>9+</sup>

drag(startx: number, starty: number, endx: number, endy: number, speed?: number): Promise\<void>

Drags this **Driver** object from the given start point to the given end point.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| startx | number | Yes  | X-coordinate of the start point.                      |
| starty | number | Yes  | Y-coordinate of the start point.                      |
| endx   | number | Yes  | X-coordinate of the end point.                      |
| endy   | number | Yes  | Y-coordinate of the end point.                      |
| speed  | number | No  | Scroll speed, in pixel/s. The value ranges from 200 to 15000. If the set value is not in the range, the default value 600 is used.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1873
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.drag(100,100,200,200,600);
}
```

### screenCap<sup>9+</sup>

screenCap(savePath: string): Promise\<boolean>

Captures the current screen of this **Driver** object and saves it as a PNG image to the given save path.
E
add doc  
ester.zhou 已提交
1889 1890 1891 1892 1893

**System capability**: SystemCapability.Test.UiTest

**Parameters**

E
ester.zhou 已提交
1894 1895
| Name  | Type  | Mandatory| Description          |
| -------- | ------ | ---- | -------------- |
E
add doc  
ester.zhou 已提交
1896 1897
| savePath | string | Yes  | File save path.|

E
ester.zhou 已提交
1898 1899
**Return value**

E
esterzhou 已提交
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
| Type             | Description                                  |
| ----------------- | -------------------------------------- |
| Promise\<boolean> | Promise used to return the operation result. The value **true** means that the operation is successful.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1910
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
1911

E
add doc  
ester.zhou 已提交
1912 1913
**Example**

E
ester.zhou 已提交
1914
```js
E
ester.zhou 已提交
1915
async function demo() {
E
esterzhou 已提交
1916
    let driver = Driver.create();
E
ester.zhou 已提交
1917
    await driver.screenCap('/data/storage/el2/base/cache/1.png');
E
ester.zhou 已提交
1918
}
E
add doc  
ester.zhou 已提交
1919
```
E
ester.zhou 已提交
1920

E
esterzhou 已提交
1921
### setDisplayRotation<sup>9+</sup>
E
ester.zhou 已提交
1922

E
esterzhou 已提交
1923 1924 1925
setDisplayRotation(rotation: DisplayRotation): Promise\<void>

Sets the display rotation of the device.
E
ester.zhou 已提交
1926 1927 1928

**System capability**: SystemCapability.Test.UiTest

E
esterzhou 已提交
1929 1930 1931 1932 1933 1934 1935 1936 1937
**Parameters**

| Name  | Type                                | Mandatory| Description            |
| -------- | ------------------------------------ | ---- | ---------------- |
| rotation | [DisplayRotation](#displayrotation9) | Yes  | Display rotation of the device.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).
E
ester.zhou 已提交
1938

E
esterzhou 已提交
1939 1940
| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1941
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.setDisplayRotation(DisplayRotation.ROTATION_180);
}
```

### getDisplayRotation<sup>9+</sup>

getDisplayRotation(): Promise\<DisplayRotation>

Obtains the display rotation of the current device.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type                                          | Description                                   |
| ---------------------------------------------- | --------------------------------------- |
| Promise\<[DisplayRotation](#displayrotation9)> | Promise used to return the display rotation of the current device.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
1972
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let rotation = await driver.getDisplayRotation();
}
```

### setDisplayRotationEnabled<sup>9+</sup>

setDisplayRotationEnabled(enabled: boolean): Promise\<void>

Enables or disables display rotation.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name | Type   | Mandatory| Description                                                   |
| ------- | ------- | ---- | ------------------------------------------------------- |
| enabled | boolean | Yes  | Whether to enable display rotation. The value **true** means to enable display rotation, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2003
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.setDisplayRotationEnabled(false);
}
```

### getDisplaySize<sup>9+</sup>

getDisplaySize(): Promise\<Point>

Obtains the display size of the current device.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type                      | Description                                   |
| -------------------------- | --------------------------------------- |
| Promise\<[Point](#point9)> | Promise used to return the display size of the current device.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2034
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let size = await driver.getDisplaySize();
}
```

### getDisplayDensity<sup>9+</sup>

getDisplayDensity(): Promise\<Point>

Obtains the display density of the current device.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type                      | Description                                     |
| -------------------------- | ----------------------------------------- |
| Promise\<[Point](#point9)> | Promise used to return the display density of the current device.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2065
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let density = await driver.getDisplayDensity();
}
```

### wakeUpDisplay<sup>9+</sup>

wakeUpDisplay(): Promise\<void>

Wakes up the device display.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2090
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.wakeUpDisplay();
}
```

### pressHome<sup>9+</sup>

pressHome(): Promise\<void>

Returns to the home screen.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2115
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.pressHome();
}
```

### waitForIdle<sup>9+</sup>

waitForIdle(idleTime: number, timeout: number): Promise\<boolean>

Checks whether all components on the current page are idle.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| idleTime | number | Yes  | Idle time threshold, in milliseconds. If the duration for which a component remains inactive reaches this threshold, it is considered as idle.|
| timeout  | number | Yes  | Maximum idle waiting time, in milliseconds.                            |

**Return value**

| Type             | Description                                               |
| ----------------- | --------------------------------------------------- |
| Promise\<boolean> | Promise used to return the result.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2153
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let idled = await driver.waitForIdle(4000,5000);
}
```

### fling<sup>9+</sup>

fling(from: Point, to: Point, stepLen: number, speed: number): Promise\<void>

Simulates a fling operation on the screen.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name | Type            | Mandatory| Description                                                        |
| ------- | ---------------- | ---- | ------------------------------------------------------------ |
| from    | [Point](#point9) | Yes  | Coordinates of the point where the finger touches the screen.                                  |
| to      | [Point](#point9) | Yes  | Coordinates of the point where the finger leaves the screen.                                    |
| stepLen | number           | Yes  | Fling step length, in pixels.                                    |
E
ester.zhou 已提交
2179
| speed   | number           | Yes  | Fling speed, in pixel/s. The value ranges from 200 to 40000. If the set value is not in the range, the default value 600 is used.|
E
esterzhou 已提交
2180 2181 2182 2183 2184 2185 2186

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2187
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2188 2189 2190 2191 2192 2193

**Example**

```js
async function demo() {
    let driver = Driver.create();
E
ester.zhou 已提交
2194
    await driver.fling({x: 500, y: 480},{x: 450, y: 480},5,600);
E
esterzhou 已提交
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
}
```

### injectMultiPointerAction<sup>9+</sup>

injectMultiPointerAction(pointers: PointerMatrix, speed?: number): Promise\<boolean>

Injects a multi-touch operation to the device.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name  | Type                            | Mandatory| Description                                                        |
| -------- | -------------------------------- | ---- | ------------------------------------------------------------ |
| pointers | [PointerMatrix](#pointermatrix9) | Yes  | Scroll trajectory, including the number of fingers and an array of coordinates along the trajectory.                  |
| speed    | number                           | No  | Scroll speed, in pixel/s. The value ranges from 200 to 15000. If the set value is not in the range, the default value 600 is used.|

**Return value**

| Type             | Description                                 |
| ----------------- | ------------------------------------- |
| Promise\<boolean> | Promise used to return the result.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2225
| 17000002 | if the async function was not called with await. |
E
esterzhou 已提交
2226 2227 2228 2229 2230 2231 2232

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let pointers = PointerMatrix.create(2,3);
E
ester.zhou 已提交
2233 2234 2235 2236 2237 2238
    pointers.setPoint(0,0,{x:230,y:480});
    pointers.setPoint(0,1,{x:250,y:380});
    pointers.setPoint(0,2,{x:270,y:280});
    pointers.setPoint(1,0,{x:230,y:680});
    pointers.setPoint(1,1,{x:240,y:580});
    pointers.setPoint(1,2,{x:250,y:480});
E
esterzhou 已提交
2239 2240 2241 2242
    await driver.injectMultiPointerAction(pointers);
}
```

E
ester.zhou 已提交
2243 2244
### fling<sup>10+</sup>

E
ester.zhou 已提交
2245
fling(direction: UiDirection, speed: number): Promise\<void>;
E
ester.zhou 已提交
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263

Simulates a fling operation on the screen, in the specified direction and speed.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name   | Type                         | Mandatory| Description                                                        |
| --------- | ----------------------------- | ---- | ------------------------------------------------------------ |
| direction | [UiDirection](#uidirection10) | Yes  | Direction of the fling operation.                                            |
| speed     | number                        | Yes  | Fling speed, in pixel/s. The value ranges from 200 to 40000. If the set value is not in the range, the default value 600 is used.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2264
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.fling(UiDirection.DOWN, 10000);
}
```

### screenCapture<sup>10+</sup>

E
ester.zhou 已提交
2277
screenCapture(savePath: string, rect?: Rect): Promise\<boolean>;
E
ester.zhou 已提交
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301

Captures the specified area of the current screen and saves the captured screenshot as a PNG image to the specified path.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name  | Type          | Mandatory| Description                  |
| -------- | -------------- | ---- | ---------------------- |
| savePath | string         | Yes  | File save path.        |
| rect     | [Rect](#rect9) | No  | Area of the screen to capture. The default value is the entire screen.|

**Return value**

| Type             | Description                                  |
| ----------------- | -------------------------------------- |
| Promise\<boolean> | Promise used to return the operation result. The value **true** means that the operation is successful.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2302
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.screenCapture('/data/storage/el2/base/cache/1.png', {left: 0, top: 0, right: 100, bottom: 100});
}
```

### mouseClick<sup>10+</sup>

E
ester.zhou 已提交
2315
mouseClick(p: Point, btnId: MouseButton, key1?: number, key2?: number): Promise\<void>;
E
ester.zhou 已提交
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325

Injects a mouse click at the specified coordinates, with the optional key or key combination. For example, if the value of **key1** is **2072**, the **Ctrl** button is pressed with the mouse click.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type                         | Mandatory| Description               |
| ------ | ----------------------------- | ---- | ------------------- |
| p      | [Point](#point9)              | Yes  | Coordinates of the mouse click.   |
E
ester.zhou 已提交
2326 2327
| btnId  | [MouseButton](#mousebutton10) | Yes  | Mouse button pressed.   |
| key1   | number                        | No  | The first key value.|
E
ester.zhou 已提交
2328 2329 2330 2331 2332 2333 2334 2335
| key2   | number                        | No  | The second key value.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2336
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.mouseClick({x:248, y:194}, MouseButton.MOUSE_BUTTON_LEFT, 2072);
}
```

### mouseScroll<sup>10+</sup>

E
ester.zhou 已提交
2349
mouseScroll(p: Point, down: boolean, d: number, key1?: number, key2?: number): Promise\<void>;
E
ester.zhou 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361

Injects a mouse scroll action at the specified coordinates, with the optional key or key combination. For example, if the value of **key1** is **2072**, the **Ctrl** button is pressed with mouse scrolling.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type            | Mandatory| Description                                               |
| ------ | ---------------- | ---- | --------------------------------------------------- |
| p      | [Point](#point9) | Yes  | Coordinates of the mouse click.                                   |
| down   | boolean          | Yes  | Whether the scroll wheel slides downward.                             |
| d      | number           | Yes  | Number of grids by which the scroll wheel slides. Sliding by one grid means a 120-pixel offset of the target point.|
E
ester.zhou 已提交
2362
| key1   | number           | No  | The first key value.                                |
E
ester.zhou 已提交
2363 2364 2365 2366 2367 2368 2369 2370
| key2   | number           | No  | The second key value.                                |

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2371
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.mouseScroll({x:360, y:640}, true, 30, 2072)
}
```

### mouseMoveTo<sup>10+</sup>

E
ester.zhou 已提交
2384
mouseMoveTo(p: Point): Promise\<void>;
E
ester.zhou 已提交
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401

Moves the cursor to the target point.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type            | Mandatory| Description          |
| ------ | ---------------- | ---- | -------------- |
| p      | [Point](#point9) | Yes  | Coordinates of the target point.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2402
| 17000002 | if the async function was not called with await. |
E
ester.zhou 已提交
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412

**Example**

```js
async function demo() {
    let driver = Driver.create();
    await driver.mouseMoveTo({x:100, y:100})
}
```

E
ester.zhou 已提交
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
### createUIEventObserver<sup>10+</sup>

createUIEventObserver(): UIEventObserver;

Creates a UI event listener.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type                                           | Description                                 |
| ----------------------------------------------- | ------------------------------------- |
| Promise\<[UIEventObserver](#uieventobserver10)> | Promise used to return the target window.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
| 17000002 | if the async function was not called with await. |

**Example**

```js
async function demo() {
    let driver = Driver.create();
E
ester.zhou 已提交
2440
    let observer = await driver.createUIEventObserver()
E
ester.zhou 已提交
2441 2442 2443
}
```

E
esterzhou 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
## PointerMatrix<sup>9+</sup>

Implements a **PointerMatrix** object that stores coordinates and behaviors of each action of each finger in a multi-touch operation.

### create<sup>9+</sup>

static create(fingers: number, steps: number): PointerMatrix

Creates a **PointerMatrix** object and returns the object created. This API is a static API.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name | Type  | Mandatory| Description                                      |
| ------- | ------ | ---- | ------------------------------------------ |
| fingers | number | Yes  | Number of fingers in the multi-touch operation. Value range: [1,10].|
| steps   | number | Yes  | Number of steps operated by each finger. Value range: [1,1000].|

**Return value**

| Type                            | Description                         |
| -------------------------------- | ----------------------------- |
| [PointerMatrix](#pointermatrix9) | **PointerMatrix** object created.|

**Example**

```js
async function demo() {
    let pointerMatrix = PointerMatrix.create(2,3);
}
```

### setPoint<sup>9+</sup>

setPoint(finger: number, step: number, point: Point): void

Sets the coordinates for the action corresponding to the specified finger and step in the **PointerMatrix** object.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type            | Mandatory| Description            |
| ------ | ---------------- | ---- | ---------------- |
| finger | number           | Yes  | Sequence number of the finger.    |
| step   | number           | Yes  | Sequence number of the step.    |
| point  | [Point](#point9) | Yes  | Coordinates of the action.|

**Example**

```js
async function demo() {
    let pointers = PointerMatrix.create(2,3);
E
ester.zhou 已提交
2498 2499 2500 2501 2502 2503
    pointers.setPoint(0,0,{x:230,y:480});
    pointers.setPoint(0,1,{x:250,y:380});
    pointers.setPoint(0,2,{x:270,y:280});
    pointers.setPoint(1,0,{x:230,y:680});
    pointers.setPoint(1,1,{x:240,y:580});
    pointers.setPoint(1,2,{x:250,y:480});
E
esterzhou 已提交
2504 2505 2506 2507 2508 2509
}
```

## UiWindow<sup>9+</sup>

The **UiWindow** class represents a window on the UI and provides APIs for obtaining window attributes, dragging a window, and adjusting the window size.
E
ester.zhou 已提交
2510

E
esterzhou 已提交
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
All APIs provided in this class use a promise to return the result and must be invoked using **await**.

### getBundleName<sup>9+</sup>

getBundleName(): Promise\<string>

Obtains the bundle name of the application to which this window belongs.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type            | Description                                     |
| ---------------- | ----------------------------------------- |
| Promise\<string> | Promise used to return the bundle name.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2533 2534
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
E
esterzhou 已提交
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    let name = await window.getBundleName();
}
```

### getBounds<sup>9+</sup>

getBounds(): Promise\<Rect>

Obtains the bounds information of this window.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type                    | Description                             |
| ------------------------ | --------------------------------- |
| Promise\<[Rect](#rect9)> | Promise used to return the bounds information of the window.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2566 2567
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
E
esterzhou 已提交
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    let rect = await window.getBounds();
}
```

### getTitle<sup>9+</sup>

getTitle(): Promise\<string>

Obtains the title of this window.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type            | Description                             |
| ---------------- | --------------------------------- |
| Promise\<string> | Promise used to return the title of the window.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2599 2600
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
E
esterzhou 已提交
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    let rect = await window.getTitle();
}
```

### getWindowMode<sup>9+</sup>

getWindowMode(): Promise\<WindowMode>

Obtains the window mode of this window.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type                                | Description                                 |
| ------------------------------------ | ------------------------------------- |
| Promise\<[WindowMode](#windowmode9)> | Promise used to return the window mode of the window.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2632 2633
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.          |
E
esterzhou 已提交
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    let mode = await window.getWindowMode();
}
```

### isFocused<sup>9+</sup>

isFocused(): Promise\<boolean>

Checks whether this window is in focused state.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the window is in focused state, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2665 2666
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
E
esterzhou 已提交
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    let focused = await window.isFocused();
}
```

### isActived<sup>9+</sup>

isActived(): Promise\<boolean>

Checks whether this window is active.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the window is active, and **false** means the opposite.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2698 2699
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
E
esterzhou 已提交
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    let focused = await window.isActived();
}
```

### focus<sup>9+</sup>

focus(): Promise\<void>

Moves the focus to this window.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2725 2726
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
E
esterzhou 已提交
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.focus();
}
```

### moveTo<sup>9+</sup>

moveTo(x: number, y: number): Promise\<void>

Moves this window to the target point. This API is applicable to moveable windows.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| x      | number | Yes  | X-coordinate of the target point.|
| y      | number | Yes  | Y-coordinate of the target point.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2759 2760 2761
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
| 17000005 | if the action is not supported on this window.         |
E
esterzhou 已提交
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.moveTo(100, 100);
}
```

### resize<sup>9+</sup>

resize(wide: number, height: number, direction: ResizeDirection): Promise\<void>

Resizes this window based on the specified width, height, and resize direction. This API is applicable to resizable windows.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name   | Type                                | Mandatory| Description                                                        |
| --------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| wide      | number                               | Yes  | Target width.                        |
| height    | number                               | Yes  | Target height.                        |
| direction | [ResizeDirection](#resizedirection9) | Yes  | Resize direction.|

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2795 2796 2797
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
| 17000005 | if the action is not supported on this window.         |
E
esterzhou 已提交
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.resize(100, 100, ResizeDirection.LEFT);
}
```

### split<sup>9+</sup>

split(): Promise\<void>

Switches the window to split-screen mode. This API is applicable to windows that support screen splitting.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2823 2824 2825
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.         |
| 17000005 | if the action is not supported on this window.         |
E
esterzhou 已提交
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.split();
}
```

### maximize<sup>9+</sup>

maximize(): Promise\<void>

Maximizes this window. This API is applicable to windows that can be maximized.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2851 2852 2853
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
| 17000005 | if the action is not supported on this window.         |
E
esterzhou 已提交
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.maximize();
}
```

### minimize<sup>9+</sup>

minimize(): Promise\<void>

Minimizes this window. This API is applicable to windows that can be minimized.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2879 2880 2881
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.          |
| 17000005 | if the action is not supported on this window.         |
E
esterzhou 已提交
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.minimize();
}
```

### resume<sup>9+</sup>

resume(): Promise\<void>

Restores this window to the previous window mode.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2907 2908 2909
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.          |
| 17000005 | if the action is not supported on this window.         |
E
esterzhou 已提交
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.resume();
}
```

### close<sup>9+</sup>

close(): Promise\<void>

Closes this window.

**System capability**: SystemCapability.Test.UiTest

**Error codes**

For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md).

| ID| Error Message                              |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
2935 2936 2937
| 17000002 | if the async function was not called with await. |
| 17000004 | if the window is invisible or destroyed.           |
| 17000005 | if the action is not supported on this window.         |
E
esterzhou 已提交
2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948

**Example**

```js
async function demo() {
    let driver = Driver.create();
    let window = await driver.findWindow({actived: true});
    await window.close();
}
```

E
ester.zhou 已提交
2949 2950
## UIEventObserver<sup>10+</sup>

E
ester.zhou 已提交
2951
Implements a UI event listener.
E
ester.zhou 已提交
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971

### once('toastShow')

once(type: 'toastShow', callback: Callback\<UIElementInfo>):void;

Subscribes to events of the toast component. This API uses a callback to return the result.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name  | Type                                        | Mandatory| Description                             |
| -------- | -------------------------------------------- | ---- | --------------------------------- |
| type     | string                                       | Yes  | Event type. The value is fixed at **'toastShow'**.|
| callback | Callback\<[UIElementInfo](#uielementinfo10)> | Yes  | Callback used to return the result.         |

**Example**

```js
async function demo() {
E
ester.zhou 已提交
2972
    let driver = Driver.create();
E
ester.zhou 已提交
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
    let observer = await driver.createUIEventObserver()
    let  callback = (UIElementInfo)=>{
        console.info(UIElementInfo.bundleName)
        console.info(UIElementInfo.text)
        console.info(UIElementInfo.type)
     }
    observer.once('toastShow', callback)
}
```

### once('dialogShow')

once(type: 'dialogShow', callback: Callback\<UIElementInfo>): void;

Subscribes to events of the dialog component. This API uses a callback to return the result.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name  | Type                                        | Mandatory| Description                              |
| -------- | -------------------------------------------- | ---- | ---------------------------------- |
| type     | string                                       | Yes  | Event type. The value is fixed at **'dialogShow'**.|
| callback | Callback\<[UIElementInfo](#uielementinfo10)> | Yes  | Callback used to return the result.          |

**Example**

```js
async function demo() {
E
ester.zhou 已提交
3002
    let driver = Driver.create();
E
ester.zhou 已提交
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
    let observer = await driver.createUIEventObserver()
    let  callback = (UIElementInfo)=>{
        console.info(UIElementInfo.bundleName)
        console.info(UIElementInfo.text)
        console.info(UIElementInfo.type)
     }
    observer.once('dialogShow', callback)
}
```

E
esterzhou 已提交
3013 3014 3015
## By<sup>(deprecated)</sup>

The UiTest framework provides a wide range of UI component feature description APIs in the **By** class to filter and match components.
E
ester.zhou 已提交
3016

E
ester.zhou 已提交
3017 3018 3019
The API capabilities provided by the **By** class exhibit the following features:

- Allow one or more attributes as the match conditions. For example, you can specify both the **text** and **id** attributes to find the target component.
E
ester.zhou 已提交
3020 3021 3022 3023
- Provide multiple match patterns for component attributes.
- Support absolute positioning and relative positioning for components. APIs such as [By.isBefore<sup>(deprecated)</sup>](#isbeforedeprecated) and [By.isAfter<sup>(deprecated)</sup>](#isafterdeprecated) can be used to specify the features of adjacent components to assist positioning.

All APIs provided in the **By** class are synchronous. You are advised to use the static constructor **BY** to create a **By** object in chain mode.
E
esterzhou 已提交
3024

E
ester.zhou 已提交
3025
This class is deprecated since API version 9. You are advised to use [On<sup>9+</sup>](#on9) instead.
E
esterzhou 已提交
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036

```js
BY.text('123').type('button');
```

### text<sup>(deprecated)</sup>

text(txt: string, pattern?: MatchPattern): By

Specifies the text attribute of the target component. Multiple match patterns are supported.

E
ester.zhou 已提交
3037
This API is deprecated since API version 9. You are advised to use [text<sup>9+</sup>](#text9) instead.
E
esterzhou 已提交
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name | Type                         | Mandatory| Description                                               |
| ------- | ----------------------------- | ---- | --------------------------------------------------- |
| txt     | string                        | Yes  | Component text, used to match the target component.               |
| pattern | [MatchPattern](#matchpattern) | No  | Match pattern. The default value is [EQUALS](#matchpattern).|

**Return value**

| Type               | Description                              |
| ------------------- | ---------------------------------- |
| [By](#bydeprecated) | **By** object that matches the text attribute of the target component.|

**Example**

```js
let by = BY.text('123'); // Use the static constructor BY to create a By object and specify the text attribute of the target component.
```


### key<sup>(deprecated)</sup>

key(key: string): By

Specifies the key attribute of the target component.

E
ester.zhou 已提交
3067
This API is deprecated since API version 9. You are advised to use [id<sup>9+</sup>](#id9) instead.
E
esterzhou 已提交
3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description             |
| ------ | ------ | ---- | ----------------- |
| key    | string | Yes  | Component key.|

**Return value**

| Type               | Description                               |
| ------------------- | ----------------------------------- |
| [By](#bydeprecated) | **By** object that matches the key attribute of the target component.|

**Example**

```js
let by = BY.key('123'); // Use the static constructor BY to create a By object and specify the key attribute of the target component.
```


### id<sup>(deprecated)</sup>

id(id: number): By

Specifies the ID attribute of the target component.

This API is deprecated since API version 9.

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| id     | number | Yes  | Component ID.|

**Return value**

| Type               | Description                            |
| ------------------- | -------------------------------- |
| [By](#bydeprecated) | **By** object that matches the ID attribute of the target component.|

**Example**

```js
let by = BY.id(123); // Use the static constructor BY to create a By object and specify the ID attribute of the target component.
```


### type<sup>(deprecated)</sup>

type(tp: string): By

Specifies the type attribute of the target component.

E
ester.zhou 已提交
3125
This API is deprecated since API version 9. You are advised to use [type<sup>9+</sup>](#type9) instead.
E
esterzhou 已提交
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description          |
| ------ | ------ | ---- | -------------- |
| tp     | string | Yes  | Component type.|

**Return value**

| Type               | Description                                    |
| ------------------- | ---------------------------------------- |
| [By](#bydeprecated) | **By** object that matches the type attribute of the target component.|

**Example**

```js
let by = BY.type('button'); // Use the static constructor BY to create a By object and specify the type attribute of the target component.
```


### clickable<sup>(deprecated)</sup>

clickable(b?: boolean): By

Specifies the clickable status attribute of the target component.

E
ester.zhou 已提交
3154
This API is deprecated since API version 9. You are advised to use [clickable<sup>9+</sup>](#clickable9) instead.
E
esterzhou 已提交
3155 3156 3157 3158 3159 3160 3161

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
3162
| b      | boolean | No  | Clickable status of the target component.<br>**true**: clickable.<br>**false**: not clickable.<br> Default value: **true**|
E
esterzhou 已提交
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182

**Return value**

| Type               | Description                                      |
| ------------------- | ------------------------------------------ |
| [By](#bydeprecated) | **By** object that matches the clickable status attribute of the target component.|

**Example**

```js
let by = BY.clickable(true); // Use the static constructor BY to create a By object and specify the clickable status attribute of the target component.
```


### scrollable<sup>(deprecated)</sup>

scrollable(b?: boolean): By

Specifies the scrollable status attribute of the target component.

E
ester.zhou 已提交
3183
This API is deprecated since API version 9. You are advised to use [scrollable<sup>9+</sup>](#scrollable9) instead.
E
esterzhou 已提交
3184 3185 3186 3187 3188 3189 3190

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type   | Mandatory| Description                                                       |
| ------ | ------- | ---- | ----------------------------------------------------------- |
E
ester.zhou 已提交
3191
| b      | boolean | No  | Scrollable status of the target component.<br>**true**: scrollable.<br>**false**: not scrollable.<br> Default value: **true**|
E
esterzhou 已提交
3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210

**Return value**

| Type               | Description                                      |
| ------------------- | ------------------------------------------ |
| [By](#bydeprecated) | **By** object that matches the scrollable status attribute of the target component.|

**Example**

```js
let by = BY.scrollable(true); // Use the static constructor BY to create a By object and specify the scrollable status attribute of the target component.
```

### enabled<sup>(deprecated)</sup>

enabled(b?: boolean): By

Specifies the enabled status attribute of the target component.

E
ester.zhou 已提交
3211
This API is deprecated since API version 9. You are advised to use [enabled<sup>9+</sup>](#enabled9) instead.
E
esterzhou 已提交
3212 3213 3214 3215 3216 3217 3218

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type   | Mandatory| Description                                                     |
| ------ | ------- | ---- | --------------------------------------------------------- |
E
ester.zhou 已提交
3219
| b      | boolean | No  | Enabled status of the target component.<br>**true**: enabled.<br>**false**: not enabled.<br> Default value: **true**|
E
esterzhou 已提交
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238

**Return value**

| Type               | Description                                    |
| ------------------- | ---------------------------------------- |
| [By](#bydeprecated) | **By** object that matches the enabled status attribute of the target component.|

**Example**

```js
let by = BY.enabled(true); // Use the static constructor BY to create a By object and specify the enabled status attribute of the target component.
```

### focused<sup>(deprecated)</sup>

focused(b?: boolean): By

Specifies the focused status attribute of the target component.

E
ester.zhou 已提交
3239
This API is deprecated since API version 9. You are advised to use [focused<sup>9+</sup>](#focused9) instead.
E
esterzhou 已提交
3240 3241 3242 3243 3244 3245 3246

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type   | Mandatory| Description                                                 |
| ------ | ------- | ---- | ----------------------------------------------------- |
E
ester.zhou 已提交
3247
| b      | boolean | No  | Focused status of the target component.<br>**true**: focused.<br>**false**: not focused.<br> Default value: **true**|
E
esterzhou 已提交
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266

**Return value**

| Type               | Description                                    |
| ------------------- | ---------------------------------------- |
| [By](#bydeprecated) | **By** object that matches the focused status attribute of the target component.|

**Example**

```js
let by = BY.focused(true); // Use the static constructor BY to create a By object and specify the focused status attribute of the target component.
```

### selected<sup>(deprecated)</sup>

selected(b?: boolean): By

Specifies the selected status of the target component.

E
ester.zhou 已提交
3267
This API is deprecated since API version 9. You are advised to use [selected<sup>9+</sup>](#selected9) instead.
E
esterzhou 已提交
3268 3269 3270 3271 3272 3273 3274

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
3275
| b      | boolean | No  | Selected status of the target component.<br>**true**: selected.<br>**false**: not selected.<br> Default value: **true**|
E
esterzhou 已提交
3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292

**Return value**

| Type               | Description                                      |
| ------------------- | ------------------------------------------ |
| [By](#bydeprecated) | **By** object that matches the selected status attribute of the target component.|

**Example**

```js
let by = BY.selected(true); // Use the static constructor BY to create a By object and specify the selected status attribute of the target component.
```

### isBefore<sup>(deprecated)</sup>

isBefore(by: By): By

E
ester.zhou 已提交
3293
Specifies that the target component is located before the given attribute component.
E
esterzhou 已提交
3294

E
ester.zhou 已提交
3295
This API is deprecated since API version 9. You are advised to use [isBefore<sup>9+</sup>](#isbefore9) instead.
E
esterzhou 已提交
3296 3297 3298 3299 3300 3301 3302

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type               | Mandatory| Description            |
| ------ | ------------------- | ---- | ---------------- |
E
ester.zhou 已提交
3303
| by     | [By](#bydeprecated) | Yes  | Information about the attribute component.|
E
esterzhou 已提交
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313

**Return value**

| Type               | Description                                                |
| ------------------- | ---------------------------------------------------- |
| [By](#bydeprecated) | **By** object.|

**Example**

```js
E
ester.zhou 已提交
3314
let by = BY.isBefore(BY.text('123')); // Use the static constructor BY to create a By object, specifying that the target component is located before the given attribute component.
E
esterzhou 已提交
3315 3316 3317 3318 3319 3320
```

### isAfter<sup>(deprecated)</sup>

isAfter(by: By): By

E
ester.zhou 已提交
3321
Specifies that the target component is located after the given attribute component.
E
esterzhou 已提交
3322

E
ester.zhou 已提交
3323
This API is deprecated since API version 9. You are advised to use [isAfter<sup>9+</sup>](#isafter9) instead.
E
esterzhou 已提交
3324 3325 3326 3327 3328 3329 3330

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type               | Mandatory| Description            |
| ------ | ------------------- | ---- | ---------------- |
E
ester.zhou 已提交
3331
| by     | [By](#bydeprecated) | Yes  | Information about the attribute component.|
E
esterzhou 已提交
3332 3333 3334 3335 3336 3337 3338 3339 3340 3341

**Return value**

| Type               | Description                                                |
| ------------------- | ---------------------------------------------------- |
| [By](#bydeprecated) | **By** object.|

**Example**

```js
E
ester.zhou 已提交
3342
let by = BY.isAfter(BY.text('123')); // Use the static constructor BY to create a By object, specifying that the target component is located after the given attribute component.
E
esterzhou 已提交
3343 3344 3345 3346 3347
```

## UiComponent<sup>(deprecated)</sup>

In **UiTest**, the **UiComponent** class represents a component on the UI and provides APIs for obtaining component attributes, clicking a component, scrolling to search for a component, and text injection.
E
ester.zhou 已提交
3348

E
esterzhou 已提交
3349 3350
All APIs provided in this class use a promise to return the result and must be invoked using **await**.

E
ester.zhou 已提交
3351
This class is deprecated since API version 9. You are advised to use [Component<sup>9+</sup>](#component9) instead.
E
esterzhou 已提交
3352 3353 3354 3355 3356 3357 3358

### click<sup>(deprecated)</sup>

click(): Promise\<void>

Clicks this component.

E
ester.zhou 已提交
3359
This API is deprecated since API version 9. You are advised to use [click<sup>9+</sup>](#click9) instead.
E
esterzhou 已提交
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378

**System capability**: SystemCapability.Test.UiTest

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    await button.click();
}
```

### doubleClick<sup>(deprecated)</sup>

doubleClick(): Promise\<void>

Double-clicks this component.

E
ester.zhou 已提交
3379
This API is deprecated since API version 9. You are advised to use [doubleClick<sup>9+</sup>](#doubleclick9) instead.
E
esterzhou 已提交
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398

**System capability**: SystemCapability.Test.UiTest

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    await button.doubleClick();
}
```

### longClick<sup>(deprecated)</sup>

longClick(): Promise\<void>

Long-clicks this component.

E
ester.zhou 已提交
3399
This API is deprecated since API version 9. You are advised to use [longClick<sup>9+</sup>](#longclick9) instead.
E
esterzhou 已提交
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444

**System capability**: SystemCapability.Test.UiTest

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    await button.longClick();
}
```

### getId<sup>(deprecated)</sup>

getId(): Promise\<number>

Obtains the ID of this component.

This API is deprecated since API version 9.

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type            | Description                           |
| ---------------- | ------------------------------- |
| Promise\<number> | Promise used to return the ID of the component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    let num = await button.getId();
}
```

### getKey<sup>(deprecated)</sup>

getKey(): Promise\<string>

Obtains the key of this component.

E
ester.zhou 已提交
3445
This API is deprecated since API version 9. You are advised to use [getId<sup>9+</sup>](#getid9) instead.
E
esterzhou 已提交
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type            | Description                          |
| ---------------- | ------------------------------ |
| Promise\<string> | Promise used to return the key of the component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    let str_key = await button.getKey();
}
```

### getText<sup>(deprecated)</sup>

getText(): Promise\<string>

Obtains the text information of this component.

E
ester.zhou 已提交
3471
This API is deprecated since API version 9. You are advised to use [getText<sup>9+</sup>](#gettext9) instead.
E
esterzhou 已提交
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type            | Description                             |
| ---------------- | --------------------------------- |
| Promise\<string> | Promise used to return the text information of the component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    let text = await button.getText();
}
```

### getType<sup>(deprecated)</sup>

getType(): Promise\<string>

Obtains the type of this component.

E
ester.zhou 已提交
3497
This API is deprecated since API version 9. You are advised to use [getType<sup>9+</sup>](#gettype9) instead.
E
esterzhou 已提交
3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type            | Description                         |
| ---------------- | ----------------------------- |
| Promise\<string> | Promise used to return the type of the component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    let type = await button.getType();
}
```

### isClickable<sup>(deprecated)</sup>

isClickable(): Promise\<boolean>

Obtains the clickable status of this component.

E
ester.zhou 已提交
3523
This API is deprecated since API version 9. You are advised to use [isClickable<sup>9+</sup>](#isclickable9) instead.
E
esterzhou 已提交
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is clickable, and **false** means the opposite.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    if(await button.isClickable()) {
        console.info('This button can be Clicked');
    } else {
        console.info('This button can not be Clicked');
    }
}
```

### isScrollable<sup>(deprecated)</sup>

isScrollable(): Promise\<boolean>

Obtains the scrollable status of this component.

E
ester.zhou 已提交
3553
This API is deprecated since API version 9. You are advised to use [isScrollable<sup>9+</sup>](#isscrollable9) instead.
E
esterzhou 已提交
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is scrollable, and **false** means the opposite.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let scrollBar = await driver.findComponent(BY.scrollable(true));
    if(await scrollBar.isScrollable()) {
        console.info('This scrollBar can be operated');
    } else {
        console.info('This scrollBar can not be operated');
    }
}
```


### isEnabled<sup>(deprecated)</sup>

isEnabled(): Promise\<boolean>

Obtains the enabled status of this component.

E
ester.zhou 已提交
3584
This API is deprecated since API version 9. You are advised to use [isEnabled<sup>9+</sup>](#isenabled9) instead.
E
esterzhou 已提交
3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type             | Description                                                      |
| ----------------- | ---------------------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is enabled, and **false** means the opposite.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    if(await button.isEnabled()) {
        console.info('This button can be operated');
    } else {
        console.info('This button can not be operated');
    }
}

```

### isFocused<sup>(deprecated)</sup>

isFocused(): Promise\<boolean>

Obtains the focused status of this component.

E
ester.zhou 已提交
3615
This API is deprecated since API version 9. You are advised to use [isFocused<sup>9+</sup>](#isfocused9) instead.
E
esterzhou 已提交
3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the target component is focused, and **false** means the opposite.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    if(await button.isFocused()) {
        console.info('This button is focused');
    } else {
        console.info('This button is not focused');
	}
}
```

### isSelected<sup>(deprecated)</sup>

isSelected(): Promise\<boolean>

Obtains the selected status of this component.

E
ester.zhou 已提交
3645
This API is deprecated since API version 9. You are advised to use [isSelected<sup>9+</sup>](#isselected9) instead.
E
esterzhou 已提交
3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type             | Description                                                 |
| ----------------- | ----------------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is selected, and **false** means the opposite.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.type('button'));
    if(await button.isSelected()) {
        console.info('This button is selected');
    } else {
        console.info('This button is not selected');
    }
}
```

### inputText<sup>(deprecated)</sup>

inputText(text: string): Promise\<void>

Enters text into this component (available for text boxes).

E
ester.zhou 已提交
3675
This API is deprecated since API version 9. You are advised to use [inputText<sup>9+</sup>](#inputtext9) instead.
E
esterzhou 已提交
3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| text   | string | Yes  | Text to enter.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let text = await driver.findComponent(BY.text('hello world'));
    await text.inputText('123');
}
```

### scrollSearch<sup>(deprecated)</sup>

scrollSearch(by: By): Promise\<UiComponent>

Scrolls on this component to search for the target component (applicable to components that support scrolling, such as **\<List>**).

E
ester.zhou 已提交
3701
This API is deprecated since API version 9. You are advised to use [scrollSearch<sup>9+</sup>](#scrollsearch9) instead.
E
esterzhou 已提交
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type               | Mandatory| Description                |
| ------ | ------------------- | ---- | -------------------- |
| by     | [By](#bydeprecated) | Yes  | Attributes of the target component.|

**Return value**

| Type                                           | Description                                 |
| ----------------------------------------------- | ------------------------------------- |
| Promise\<[UiComponent](#uicomponentdeprecated)> | Promise used to return the target component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let scrollBar = await driver.findComponent(BY.type('Scroll'));
    let button = await scrollBar.scrollSearch(BY.text('next page'));
}
```

## UiDriver<sup>(deprecated)</sup>

The **UiDriver** class is the main entry to the UiTest framework. It provides APIs for features such as component matching/search, key injection, coordinate clicking/sliding, and screenshot.
E
ester.zhou 已提交
3730

E
esterzhou 已提交
3731 3732
All APIs provided by this class, except for **UiDriver.create()**, use a promise to return the result and must be invoked using **await**.

E
ester.zhou 已提交
3733
This class is deprecated since API version 9. You are advised to use [Driver<sup>9+</sup>](#driver9) instead.
E
esterzhou 已提交
3734 3735 3736 3737 3738 3739 3740

### create<sup>(deprecated)</sup>

static create(): UiDriver

Creates a **UiDriver** object and returns the object created. This API is a static API.

E
ester.zhou 已提交
3741
This API is deprecated since API version 9. You are advised to use [create<sup>9+</sup>](#create9) instead.
E
esterzhou 已提交
3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764

**System capability**: SystemCapability.Test.UiTest

**Return value**

| Type    | Description                    |
| -------- | ------------------------ |
| UiDriver | Returns the **UiDriver** object created.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
}
```

### delayMs<sup>(deprecated)</sup>

delayMs(duration: number): Promise\<void>

Delays this **UiDriver** object within the specified duration.

E
ester.zhou 已提交
3765
This API is deprecated since API version 9. You are advised to use [delayMs<sup>9+</sup>](#delayms9) instead.
E
esterzhou 已提交
3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name  | Type  | Mandatory| Description        |
| -------- | ------ | ---- | ------------ |
| duration | number | Yes  | Duration of time.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.delayMs(1000);
}
```

### findComponent<sup>(deprecated)</sup>

findComponent(by: By): Promise\<UiComponent>

Searches this **UiDriver** object for the target component that matches the given attributes.

E
ester.zhou 已提交
3790
This API is deprecated since API version 9. You are advised to use [findComponent<sup>9+</sup>](#findcomponent9) instead.
E
esterzhou 已提交
3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type               | Mandatory| Description                |
| ------ | ------------------- | ---- | -------------------- |
| by     | [By](#bydeprecated) | Yes  | Attributes of the target component.|

**Return value**

| Type                                           | Description                             |
| ----------------------------------------------- | --------------------------------- |
| Promise\<[UiComponent](#uicomponentdeprecated)> | Promise used to return the found component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let button = await driver.findComponent(BY.text('next page'));
}
```

### findComponents<sup>(deprecated)</sup>

findComponents(by: By): Promise\<Array\<UiComponent>>

Searches this **UiDriver** object for all components that match the given attributes.

E
ester.zhou 已提交
3821
This API is deprecated since API version 9. You are advised to use [findComponents<sup>9+</sup>](#findcomponents9) instead.
E
esterzhou 已提交
3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type               | Mandatory| Description                |
| ------ | ------------------- | ---- | -------------------- |
| by     | [By](#bydeprecated) | Yes  | Attributes of the target component.|

**Return value**

| Type                                                   | Description                                   |
| ------------------------------------------------------- | --------------------------------------- |
| Promise\<Array\<[UiComponent](#uicomponentdeprecated)>> | Promise used to return a list of found components.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    let buttonList = await driver.findComponents(BY.text('next page'));
}
```

### assertComponentExist<sup>(deprecated)</sup>

assertComponentExist(by: By): Promise\<void>

Asserts that a component that matches the given attributes exists on the current page. If the component does not exist, the API throws a JS exception, causing the current test case to fail.

E
ester.zhou 已提交
3852
This API is deprecated since API version 9. You are advised to use [assertComponentExist<sup>9+</sup>](#assertcomponentexist9) instead.
E
esterzhou 已提交
3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type               | Mandatory| Description                |
| ------ | ------------------- | ---- | -------------------- |
| by     | [By](#bydeprecated) | Yes  | Attributes of the target component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.assertComponentExist(BY.text('next page'));
}
```

### pressBack<sup>(deprecated)</sup>

pressBack(): Promise\<void>

Presses the Back button on this **UiDriver** object.

E
ester.zhou 已提交
3877
This API is deprecated since API version 9. You are advised to use [pressBack<sup>9+</sup>](#pressback9) instead.
E
esterzhou 已提交
3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895

**System capability**: SystemCapability.Test.UiTest

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.pressBack();
}
```

### triggerKey<sup>(deprecated)</sup>

triggerKey(keyCode: number): Promise\<void>

Triggers the key of this **UiDriver** object that matches the given key code.

E
ester.zhou 已提交
3896
This API is deprecated since API version 9. You are advised to use [triggerKey<sup>9+</sup>](#triggerkey9) instead.
E
esterzhou 已提交
3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name | Type  | Mandatory| Description         |
| ------- | ------ | ---- | ------------- |
| keyCode | number | Yes  | Key code.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.triggerKey(123);
}
```


### click<sup>(deprecated)</sup>

click(x: number, y: number): Promise\<void>

Clicks a specific point of this **UiDriver** object based on the given coordinates.

E
ester.zhou 已提交
3922
This API is deprecated since API version 9. You are advised to use [click<sup>9+</sup>](#click9) instead.
E
esterzhou 已提交
3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| x      | number | Yes  | X-coordinate of the target point.|
| y      | number | Yes  | Y-coordinate of the target point.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.click(100,100);
}
```

### doubleClick<sup>(deprecated)</sup>

doubleClick(x: number, y: number): Promise\<void>

Double-clicks a specific point of this **UiDriver** object based on the given coordinates.

E
ester.zhou 已提交
3948
This API is deprecated since API version 9. You are advised to use [doubleClick<sup>9+</sup>](#doubleclick9) instead.
E
esterzhou 已提交
3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| x      | number | Yes  | X-coordinate of the target point.|
| y      | number | Yes  | Y-coordinate of the target point.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.doubleClick(100,100);
}
```

### longClick<sup>(deprecated)</sup>

longClick(x: number, y: number): Promise\<void>

Long-clicks a specific point of this **UiDriver** object based on the given coordinates.

E
ester.zhou 已提交
3974
This API is deprecated since API version 9. You are advised to use [longClick<sup>9+</sup>](#longclick9) instead.
E
esterzhou 已提交
3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| x      | number | Yes  | X-coordinate of the target point.|
| y      | number | Yes  | Y-coordinate of the target point.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.longClick(100,100);
}
```

### swipe<sup>(deprecated)</sup>

swipe(startx: number, starty: number, endx: number, endy: number): Promise\<void>

Swipes on this **UiDriver** object from the start point to the end point based on the given coordinates.

E
ester.zhou 已提交
4000
This API is deprecated since API version 9. You are advised to use [swipe<sup>9+</sup>](#swipe9) instead.
E
esterzhou 已提交
4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| startx | number | Yes  | X-coordinate of the start point.|
| starty | number | Yes  | Y-coordinate of the start point.|
| endx   | number | Yes  | X-coordinate of the end point.|
| endy   | number | Yes  | Y-coordinate of the end point.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
    await driver.swipe(100,100,200,200);
}
```

### screenCap<sup>(deprecated)</sup>

screenCap(savePath: string): Promise\<boolean>

Captures the current screen of this **UiDriver** object and saves it as a PNG image to the given save path.

E
ester.zhou 已提交
4028
This API is deprecated since API version 9. You are advised to use [screenCap<sup>9+</sup>](#screencap9) instead.
E
esterzhou 已提交
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048

**System capability**: SystemCapability.Test.UiTest

**Parameters**

| Name  | Type  | Mandatory| Description          |
| -------- | ------ | ---- | -------------- |
| savePath | string | Yes  | File save path.|

**Return value**

| Type             | Description                                  |
| ----------------- | -------------------------------------- |
| Promise\<boolean> | Promise used to return the operation result. The value **true** means that the operation is successful.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create();
E
ester.zhou 已提交
4049
    await driver.screenCap('/data/storage/el2/base/cache/1.png');
E
esterzhou 已提交
4050 4051
}
```