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

E
ester.zhou 已提交
3
>**NOTE**<br>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 已提交
4 5 6 7 8 9 10 11 12 13 14
>


## Modules to Import

```
import {UiDriver,BY,MatchPattern} from '@ohos.uitest'
```

## By

E
ester.zhou 已提交
15 16
The UiTest framework provides a wide range of UI component feature description APIs in the **By** class to filter and match components.<br>
The API capabilities provided by the **By** class exhibit the following features: <br>1. 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. <br>2. Provide multiple match patterns for component attributes. <br>3. Support absolute positioning and relative positioning for components. APIs such as [By.isBefore](#byisbefore) and [By.isAfter](#byisafter) can be used to specify the features of adjacent components to assist positioning. <br>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
add doc  
ester.zhou 已提交
17

E
ester.zhou 已提交
18 19 20
```js
BY.text('123').type('button')
```
E
add doc  
ester.zhou 已提交
21 22 23

### By.text

E
ester.zhou 已提交
24
text(txt: string, pattern?: MatchPattern): By
E
add doc  
ester.zhou 已提交
25 26 27 28 29 30 31

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

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

**Parameters**

E
ester.zhou 已提交
32 33 34 35
| Name | Type        | Mandatory| Description                                             |
| ------- | ------------ | ---- | ------------------------------------------------- |
| txt     | string       | Yes  | Component text, used to match the target component.             |
| pattern | MatchPattern | No  | Match pattern. The default value is [EQUALS](#matchpattern).|
E
add doc  
ester.zhou 已提交
36 37 38 39 40 41 42 43 44

**Return value**

| Type| Description          |
| ---- | -------------- |
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
45 46
```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.
E
add doc  
ester.zhou 已提交
47 48 49 50 51
```


### By.key

E
ester.zhou 已提交
52
key(key: string): By
E
add doc  
ester.zhou 已提交
53 54 55 56 57 58 59

Specifies the key attribute of the target component.

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

**Parameters**

E
ester.zhou 已提交
60 61
| Name| Type  | Mandatory| Description             |
| ------ | ------ | ---- | ----------------- |
E
add doc  
ester.zhou 已提交
62 63 64 65
| key    | string | Yes  | Component key.|

**Return value**

E
ester.zhou 已提交
66 67
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
68 69 70 71
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
72 73
```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.
E
add doc  
ester.zhou 已提交
74 75 76 77 78
```


### By.id

E
ester.zhou 已提交
79
id(id: number): By
E
add doc  
ester.zhou 已提交
80

E
ester.zhou 已提交
81
Specifies the ID attribute of the target component.
E
add doc  
ester.zhou 已提交
82 83 84 85 86

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

**Parameters**

E
ester.zhou 已提交
87 88
| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
E
add doc  
ester.zhou 已提交
89 90 91 92
| id     | number | Yes  | Component ID.|

**Return value**

E
ester.zhou 已提交
93 94
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
95 96 97 98
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
99 100
```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.
E
add doc  
ester.zhou 已提交
101 102 103 104 105
```


### By.type

E
ester.zhou 已提交
106
type(tp: string): By
E
add doc  
ester.zhou 已提交
107

E
ester.zhou 已提交
108
Specifies the type attribute of the target component.
E
add doc  
ester.zhou 已提交
109 110 111 112 113

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

**Parameters**

E
ester.zhou 已提交
114 115
| Name| Type  | Mandatory| Description          |
| ------ | ------ | ---- | -------------- |
E
add doc  
ester.zhou 已提交
116 117 118 119
| tp     | string | Yes  | Component type.|

**Return value**

E
ester.zhou 已提交
120 121
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
122 123 124 125
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
126 127
```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.
E
add doc  
ester.zhou 已提交
128 129 130 131 132
```


### By.clickable

E
ester.zhou 已提交
133
clickable(b?: bool): By
E
add doc  
ester.zhou 已提交
134 135 136 137 138 139 140

Specifies the clickable attribute of the target component.

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

**Parameters**

E
ester.zhou 已提交
141 142
| Name| Type| Mandatory| Description                            |
| ------ | ---- | ---- | -------------------------------- |
E
add doc  
ester.zhou 已提交
143 144 145 146
| b      | bool | No  | Clickable status of the target component. The default value is **true**.|

**Return value**

E
ester.zhou 已提交
147 148
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
149 150 151 152
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
153 154
```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.
E
add doc  
ester.zhou 已提交
155
```
E
ester.zhou 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

### By.longClickable<sup>9+</sup>

longClickable(b?: bool): By

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

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

**Parameters**

| Name| Type| Mandatory| Description                                |
| ------ | ---- | ---- | ------------------------------------ |
| b      | bool | No  | Long-clickable status of the target component. The default value is **true**.|

**Return value**

| Type| Description            |
| ---- | ---------------- |
| By   | Returns the **By** object itself.|

**Example**

```js
let by = BY.longClickable(true) // Use the static constructor BY to create a By object and specify the long-clickable status attribute of the target component.
E
add doc  
ester.zhou 已提交
181 182 183 184 185
```


### By.scrollable

E
ester.zhou 已提交
186
scrollable(b?: bool): By
E
add doc  
ester.zhou 已提交
187

E
ester.zhou 已提交
188
Specifies the scrollable status attribute of the target component.
E
add doc  
ester.zhou 已提交
189 190 191 192 193

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

**Parameters**

E
ester.zhou 已提交
194 195
| Name| Type| Mandatory| Description                        |
| ------ | ---- | ---- | ---------------------------- |
E
add doc  
ester.zhou 已提交
196 197 198 199
| b      | bool | No  | Scrollable status of the target component. The default value is **true**.|

**Return value**

E
ester.zhou 已提交
200 201
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
202 203 204 205
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
206 207
```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.
E
add doc  
ester.zhou 已提交
208 209 210 211
```

### By.enabled

E
ester.zhou 已提交
212
enabled(b?: bool): By
E
add doc  
ester.zhou 已提交
213

E
ester.zhou 已提交
214
Specifies the enabled status attribute of the target component.
E
add doc  
ester.zhou 已提交
215 216 217 218 219

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

**Parameters**

E
ester.zhou 已提交
220 221 222
| Name| Type| Mandatory| Description                          |
| ------ | ---- | ---- | ------------------------------ |
| b      | bool | No  | Enabled status of the target component. The default value is **true**.|
E
add doc  
ester.zhou 已提交
223 224 225

**Return value**

E
ester.zhou 已提交
226 227
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
228 229 230 231
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
232 233
```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.
E
add doc  
ester.zhou 已提交
234 235 236 237
```

### By.focused

E
ester.zhou 已提交
238
focused(b?: bool): By
E
add doc  
ester.zhou 已提交
239

E
ester.zhou 已提交
240
Specifies the focused status attribute of the target component.
E
add doc  
ester.zhou 已提交
241 242 243 244 245

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

**Parameters**

E
ester.zhou 已提交
246 247
| Name| Type| Mandatory| Description                      |
| ------ | ---- | ---- | -------------------------- |
E
add doc  
ester.zhou 已提交
248 249 250 251
| b      | bool | No  | Focused status of the target component. The default value is **true**.|

**Return value**

E
ester.zhou 已提交
252 253
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
254 255 256 257
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
258 259
```js
let by = BY.enabled(true) // Use the static constructor BY to create a By object and specify the focused status attribute of the target component.
E
add doc  
ester.zhou 已提交
260 261 262 263
```

### By.selected

E
ester.zhou 已提交
264
selected(b?: bool): By
E
add doc  
ester.zhou 已提交
265

E
ester.zhou 已提交
266
Specifies the selected status attribute of the target component.
E
add doc  
ester.zhou 已提交
267 268 269 270 271

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

**Parameters**

E
ester.zhou 已提交
272 273
| Name| Type| Mandatory| Description                            |
| ------ | ---- | ---- | -------------------------------- |
E
add doc  
ester.zhou 已提交
274 275 276 277
| b      | bool | No  | Selected status of the target component. The default value is **true**.|

**Return value**

E
ester.zhou 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
| Type| Description            |
| ---- | ---------------- |
| By   | Returns the **By** object itself.|

**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.
```

### By.checked<sup>9+</sup>

checked(b?: bool): By

Specifies the checked status attribute of the target component.

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

**Parameters**

| Name| Type| Mandatory| Description                             |
| ------ | ---- | ---- | --------------------------------- |
| b      | bool | No  | Checked status of the target component. The default value is **false**.|

**Return value**

E
add doc  
ester.zhou 已提交
304 305 306 307 308 309
| Type| Description          |
| ---- | -------------- |
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
310 311
```js
let by = BY.checked(true) // Use the static constructor BY to create a By object and specify the checked status attribute of the target component.
E
add doc  
ester.zhou 已提交
312
```
E
ester.zhou 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

### By.checkable<sup>9+</sup>

checkable(b?: bool): By

Specifies the checkable status attribute of the target component.

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

**Parameters**

| Name| Type| Mandatory| Description                                 |
| ------ | ---- | ---- | ------------------------------------- |
| b      | bool | No  | Checkable status of the target component. The default value is **false**.|

**Return value**

| Type| Description            |
| ---- | ---------------- |
| By   | Returns the **By** object itself.|

**Example**

```js
let by = BY.checkable(true) // Use the static constructor BY to create a By object and specify the checkable status attribute of the target component.
E
add doc  
ester.zhou 已提交
338 339 340 341
```

### By.isBefore

E
ester.zhou 已提交
342
isBefore(by: By): By
E
add doc  
ester.zhou 已提交
343 344 345 346 347 348 349

Specifies the attributes of the component before which the target component is located.

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

**Parameters**

E
ester.zhou 已提交
350 351
| Name| Type| Mandatory| Description            |
| ------ | ---- | ---- | ---------------- |
E
add doc  
ester.zhou 已提交
352 353 354 355
| by     | By   | Yes  | Attributes of the component before which the target component is located.|

**Return value**

E
ester.zhou 已提交
356 357
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
358 359 360 361
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
362 363
```js
let by = BY.isBefore(BY.text('123')) // Use the static constructor BY to create a By object and specify the attributes of the component before which the target component is located.
E
add doc  
ester.zhou 已提交
364 365 366 367
```

### By.isAfter

E
ester.zhou 已提交
368
isAfter(by: By): By
E
add doc  
ester.zhou 已提交
369 370 371 372 373 374 375

Specifies the attributes of the component after which the target component is located.

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

**Parameters**

E
ester.zhou 已提交
376 377
| Name| Type| Mandatory| Description            |
| ------ | ---- | ---- | ---------------- |
E
ester.zhou 已提交
378
| by     | By   | Yes  | Attributes of the component before which the target component is located.|
E
add doc  
ester.zhou 已提交
379 380 381

**Return value**

E
ester.zhou 已提交
382 383
| Type| Description            |
| ---- | ---------------- |
E
add doc  
ester.zhou 已提交
384 385 386 387
| By   | Returns the **By** object itself.|

**Example**

E
ester.zhou 已提交
388 389
```js
let by = BY.isAfter(BY.text('123')) // Use the static constructor BY to create a By object and specify the attributes of the component after which the target component is located.
E
add doc  
ester.zhou 已提交
390 391 392 393 394
```

## UiComponent

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 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408
All APIs provided in this class use a promise to return the result and must be invoked using **await**.

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

Provides border information of a component.

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

| Name   | Type| Readable| Writable| Description                     |
| ------- | -------- | ---- | ---- | ------------------------- |
| leftX   | number   | Yes  | No  | X coordinate of the upper left corner of the component borders.|
| topY    | number   | Yes  | No  | Y coordinate of the upper left corner of the component borders.|
| rightX  | number   | Yes  | No  | X coordinate of the lower right corner of the component borders.|
| bottomY | number   | Yes  | No  | Y coordinate of the lower right corner of the component borders.|
E
add doc  
ester.zhou 已提交
409 410 411

### UiComponent.click

E
ester.zhou 已提交
412
click(): Promise\<void>
E
add doc  
ester.zhou 已提交
413 414 415 416 417 418 419

Clicks this component.

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

**Example**

E
ester.zhou 已提交
420
```js
E
ester.zhou 已提交
421 422 423 424 425
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    await button.click()
}
E
add doc  
ester.zhou 已提交
426 427 428 429
```

### UiComponent.doubleClick

E
ester.zhou 已提交
430
doubleClick(): Promise\<void>
E
add doc  
ester.zhou 已提交
431 432 433 434 435 436 437

Double-clicks this component.

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

**Example**

E
ester.zhou 已提交
438
```js
E
ester.zhou 已提交
439 440 441
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
E
ester.zhou 已提交
442
    await button.doubleClick()
E
ester.zhou 已提交
443
}
E
add doc  
ester.zhou 已提交
444 445 446 447
```

### UiComponent.longClick

E
ester.zhou 已提交
448
longClick(): Promise\<void>
E
add doc  
ester.zhou 已提交
449 450 451 452 453 454 455

Long-clicks this component.

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

**Example**

E
ester.zhou 已提交
456
```js
E
ester.zhou 已提交
457 458 459 460 461
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    await button.longClick()
}
E
add doc  
ester.zhou 已提交
462 463 464 465
```

### UiComponent.getId

E
ester.zhou 已提交
466
getId(): Promise\<number>
E
add doc  
ester.zhou 已提交
467 468 469 470 471 472 473

Obtains the ID of this component.

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

**Return value**

E
ester.zhou 已提交
474 475 476
| Type            | Description                           |
| ---------------- | ------------------------------- |
| Promise\<number> | Promise used to return the ID of the component.|
E
add doc  
ester.zhou 已提交
477 478 479

**Example**

E
ester.zhou 已提交
480
```js
E
ester.zhou 已提交
481 482 483 484 485
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    let num = await button.getId()
}
E
add doc  
ester.zhou 已提交
486 487 488 489
```

### UiComponent.getKey

E
ester.zhou 已提交
490
getKey(): Promise\<string>
E
add doc  
ester.zhou 已提交
491 492 493 494 495 496 497

Obtains the key of this component.

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

**Return value**

E
ester.zhou 已提交
498 499 500
| Type            | Description                          |
| ---------------- | ------------------------------ |
| Promise\<string> | Promise used to return the key of the component.|
E
add doc  
ester.zhou 已提交
501 502 503

**Example**

E
ester.zhou 已提交
504
```js
E
ester.zhou 已提交
505 506 507 508 509
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    let str_key = await button.getKey()
}
E
add doc  
ester.zhou 已提交
510 511 512 513
```

### UiComponent.getText

E
ester.zhou 已提交
514
getText(): Promise\<string>
E
add doc  
ester.zhou 已提交
515 516 517 518 519 520 521

Obtains the text information of this component.

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

**Return value**

E
ester.zhou 已提交
522 523 524
| Type            | Description                             |
| ---------------- | --------------------------------- |
| Promise\<string> | Promise used to return the text information of the component.|
E
add doc  
ester.zhou 已提交
525 526 527

**Example**

E
ester.zhou 已提交
528
```js
E
ester.zhou 已提交
529 530 531 532 533
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    let text = await button.getText()
}
E
add doc  
ester.zhou 已提交
534 535 536 537
```

### UiComponent.getType

E
ester.zhou 已提交
538
getType(): Promise\<string>
E
add doc  
ester.zhou 已提交
539 540 541 542 543 544 545

Obtains the type of this component.

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

**Return value**

E
ester.zhou 已提交
546 547 548
| Type            | Description                         |
| ---------------- | ----------------------------- |
| Promise\<string> | Promise used to return the type of the component.|
E
add doc  
ester.zhou 已提交
549 550 551

**Example**

E
ester.zhou 已提交
552
```js
E
ester.zhou 已提交
553 554 555 556 557
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    let type = await button.getType()
}
E
add doc  
ester.zhou 已提交
558 559
```

E
ester.zhou 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
### UiComponent.getBounds<sup>9+</sup>

getBounds(): Promise\<Rect>

Obtains the bounds of this component.

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

**Return value**

| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<Rect> | Promise used to return the bounds of the component.|

**Example**

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

E
add doc  
ester.zhou 已提交
584 585
### UiComponent.isClickable

E
ester.zhou 已提交
586
isClickable(): Promise\<bool>
E
add doc  
ester.zhou 已提交
587 588 589 590 591 592 593

Obtains the clickable status of this component.

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

**Return value**

E
ester.zhou 已提交
594 595 596
| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<bool> | Promise used to return the clickable status of the component.|
E
add doc  
ester.zhou 已提交
597 598 599

**Example**

E
ester.zhou 已提交
600
```js
E
ester.zhou 已提交
601 602 603 604 605 606 607 608 609 610
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')
    }
}
E
add doc  
ester.zhou 已提交
611 612
```

E
ester.zhou 已提交
613
### UiComponent.isLongClickable<sup>9+</sup>
E
add doc  
ester.zhou 已提交
614

E
ester.zhou 已提交
615
isLongClickable(): Promise\<bool> 
E
add doc  
ester.zhou 已提交
616

E
ester.zhou 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
Obtains the long clickable status of this component.

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

**Return value**

| Type          | Description                                       |
| -------------- | ------------------------------------------- |
| Promise\<bool> | Promise used to return the long clickable status of the component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    if(await button.isLongClickable()) {
        console.info('This button can longClick')
    }
    else{
        console.info('This button can not longClick')
    }
}
```

### UiComponent.isChecked<sup>9+</sup>

isChecked(): Promise\<bool>
E
add doc  
ester.zhou 已提交
645

E
ester.zhou 已提交
646
Obtains the checked status of this component.
E
add doc  
ester.zhou 已提交
647 648 649 650 651

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

**Return value**

E
ester.zhou 已提交
652 653 654
| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<bool> | Promise used to return the checked status of the component.|
E
add doc  
ester.zhou 已提交
655 656 657

**Example**

E
ester.zhou 已提交
658 659 660 661 662 663 664 665 666 667 668
```js
async function demo() {
    let driver = UiDriver.create()
    let checkBox = await driver.findComponent(BY.type('Checkbox'))
    if(await checkBox.isChecked) {
        console.info('This checkBox is checked')
    }
    else{
        console.info('This checkBox is not checked')
    }
}
E
add doc  
ester.zhou 已提交
669
```
E
ester.zhou 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

### UiComponent.isCheckable<sup>9+</sup>

isCheckable(): Promise\<bool>

Obtains the checked status of this component.

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

**Return value**

| Type          | Description                                       |
| -------------- | ------------------------------------------- |
| Promise\<bool> | Promise used to return the checked status of the component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create()
    let checkBox = await driver.findComponent(BY.type('Checkbox'))
    if(await checkBox.isCheckable) {
        console.info('This checkBox is checkable')
    }
    else{
        console.info('This checkBox is not checkable')
    }
}
```

### UiComponent.isScrollable

isScrollable(): Promise\<bool>

Obtains the scrollable status of this component.

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

**Return value**

| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<bool> | Promise used to return the scrollable status of the component.|

**Example**

```js
E
ester.zhou 已提交
717 718 719 720 721 722 723 724 725 726
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')
    }
}
E
add doc  
ester.zhou 已提交
727 728 729 730 731
```


### UiComponent.isEnabled

E
ester.zhou 已提交
732
isEnabled(): Promise\<bool>
E
add doc  
ester.zhou 已提交
733

E
ester.zhou 已提交
734
Obtains the enabled status of this component.
E
add doc  
ester.zhou 已提交
735 736 737 738 739

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

**Return value**

E
ester.zhou 已提交
740 741 742
| Type          | Description                           |
| -------------- | ------------------------------- |
| Promise\<bool> | Promise used to return the enabled status of the component.|
E
add doc  
ester.zhou 已提交
743 744 745

**Example**

E
ester.zhou 已提交
746
```js
E
ester.zhou 已提交
747 748 749 750 751 752 753 754 755 756 757
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')
    }
}

E
add doc  
ester.zhou 已提交
758 759 760 761
```

### UiComponent.isFocused

E
ester.zhou 已提交
762
isFocused(): Promise\<bool>
E
add doc  
ester.zhou 已提交
763 764 765 766 767 768 769

Obtains the focused status of this component.

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

**Return value**

E
ester.zhou 已提交
770 771 772
| Type          | Description                               |
| -------------- | ----------------------------------- |
| Promise\<bool> | Promise used to return the focused status of the component.|
E
add doc  
ester.zhou 已提交
773 774 775

**Example**

E
ester.zhou 已提交
776
```js
E
ester.zhou 已提交
777 778 779 780 781 782 783 784 785 786
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')
	}
}
E
add doc  
ester.zhou 已提交
787 788 789 790
```

### UiComponent.isSelected

E
ester.zhou 已提交
791
isSelected(): Promise\<bool>
E
add doc  
ester.zhou 已提交
792 793 794 795 796 797 798

Obtains the selected status of this component.

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

**Return value**

E
ester.zhou 已提交
799 800 801
| Type          | Description                |
| -------------- | -------------------- |
| Promise\<bool> | Promise used to return the selected status of the component.|
E
add doc  
ester.zhou 已提交
802 803 804

**Example**

E
ester.zhou 已提交
805
```js
E
ester.zhou 已提交
806 807 808 809 810 811 812 813 814 815
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')
    }
}
E
add doc  
ester.zhou 已提交
816 817 818 819
```

### UiComponent.inputText

E
ester.zhou 已提交
820
inputText(text: string): Promise\<void>
E
add doc  
ester.zhou 已提交
821 822 823 824 825 826 827

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

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

**Parameters**

E
ester.zhou 已提交
828 829 830
| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| text   | string | Yes  | Text to enter.|
E
add doc  
ester.zhou 已提交
831 832 833

**Example**

E
ester.zhou 已提交
834 835 836 837 838 839
```js
async function demo() {
    let driver = UiDriver.create()
    let text = await driver.findComponent(BY.text('hello world'))
    await text.inputText('123')
}
E
add doc  
ester.zhou 已提交
840
```
E
ester.zhou 已提交
841 842 843 844 845 846 847 848 849 850 851 852

### UiComponent.clearText<sup>9+</sup>

clearText(): Promise\<void>

Clears text in this component (available for text boxes).

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

**Example**

```js
E
ester.zhou 已提交
853 854
async function demo() {
    let driver = UiDriver.create()
E
ester.zhou 已提交
855 856
    let text = await driver.findComponent(BY.text('hello world'))
    await text.clearText()
E
ester.zhou 已提交
857
}
E
add doc  
ester.zhou 已提交
858 859 860 861
```

### UiComponent.scrollSearch

E
ester.zhou 已提交
862
scrollSearch(by: By): Promise\<UiComponent>
E
add doc  
ester.zhou 已提交
863

E
ester.zhou 已提交
864
Scrolls on this component to search for the target component (applicable to component that support scrolling, such as **\<List>**).
E
add doc  
ester.zhou 已提交
865 866 867 868 869 870 871 872 873 874 875

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

**Parameters**

| Name| Type| Mandatory| Description                |
| ------ | ---- | ---- | -------------------- |
| by     | By   | Yes  | Attributes of the target component.|

**Return value**

E
ester.zhou 已提交
876 877 878
| Type                 | Description                                 |
| --------------------- | ------------------------------------- |
| Promise\<UiComponent> | Promise used to return the target component.|
E
add doc  
ester.zhou 已提交
879 880 881

**Example**

E
ester.zhou 已提交
882
```js
E
ester.zhou 已提交
883 884
async function demo() {
    let driver = UiDriver.create()
E
ester.zhou 已提交
885
    let scrollBar = await driver.findComponent(BY.type('Scroll'))
E
ester.zhou 已提交
886 887
    let button = await scrollBar.scrollSearch(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
888 889
```

E
ester.zhou 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
### UiComponent.scrollToTop<sup>9+</sup>

scrollToTop(): Promise\<void>

Scrolls to the top of this a component (applicable to component that support scrolling, such as **\<List>**).

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

**Example**

```js
async function demo() {
    let driver = UiDriver.create()
    let scrollBar = await driver.findComponent(BY.type('Scroll'))
    await scrollBar.scrollToTop()
}
```

### UiComponent.scrollToBottom<sup>9+</sup>

scrollToBottom(): Promise\<void>

Scrolls to the bottom of this a component (applicable to component that support scrolling, such as **\<List>**).

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

**Example**

```js
async function demo() {
    let driver = UiDriver.create()
    let scrollBar = await driver.findComponent(BY.type('Scroll'))
    await scrollBar.scrollToBottom()
}
```

### UiComponent.dragTo<sup>9+</sup>

dragTo(target: UiComponent): Promise\<void>

Drags this component to the target component.

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

**Parameters**

| Name| Type       | Mandatory| Description      |
| ------ | ----------- | ---- | ---------- |
| target | UiComponent | Yes  | Target component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    let text = await driver.findComponent(BY.text('hello world'))
    await button.dragTo(text)
    }
```

E
add doc  
ester.zhou 已提交
951 952 953 954 955 956 957
## UiDriver

The **UiDriver** class is the main entry to the **uitest** test framework. It provides APIs for features such as component matching/search, key injection, coordinate clicking/sliding, and screenshot.
All APIs provided by this class, except for **UiDriver.create()**, use a promise to return the result and must be invoked using **await**.

### UiDriver.create

E
ester.zhou 已提交
958
static create(): UiDriver
E
add doc  
ester.zhou 已提交
959 960 961 962 963 964 965

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

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

**Return value**

E
ester.zhou 已提交
966 967
| Type   | Description                    |
| ------- | ------------------------ |
E
add doc  
ester.zhou 已提交
968 969 970 971
| UiDrive | Returns the **UiDriver** object created.|

**Example**

E
ester.zhou 已提交
972
```js
E
ester.zhou 已提交
973 974 975
async function demo() {
    let driver = UiDriver.create()
}
E
add doc  
ester.zhou 已提交
976 977 978 979
```

### UiDriver.delayMs

E
ester.zhou 已提交
980
delayMs(duration: number): Promise\<void>
E
add doc  
ester.zhou 已提交
981 982 983 984 985 986 987

Delays this **UiDriver** object within the specified duration.

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

**Parameters**

E
ester.zhou 已提交
988 989
| Name  | Type  | Mandatory| Description        |
| -------- | ------ | ---- | ------------ |
E
add doc  
ester.zhou 已提交
990 991 992 993
| duration | number | Yes  | Duration of time.|

**Example**

E
ester.zhou 已提交
994
```js
E
ester.zhou 已提交
995 996 997 998
async function demo() {
    let driver = UiDriver.create()
    await driver.delayMs(1000)
}
E
add doc  
ester.zhou 已提交
999 1000 1001 1002
```

### UiDriver.findComponent

E
ester.zhou 已提交
1003
findComponent(by: By): Promise\<UiComponent>
E
add doc  
ester.zhou 已提交
1004

E
ester.zhou 已提交
1005
Searches this **UiDriver** object for the target component that matches the given attributes.
E
add doc  
ester.zhou 已提交
1006 1007 1008 1009 1010

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

**Parameters**

E
ester.zhou 已提交
1011 1012
| Name| Type| Mandatory| Description                |
| ------ | ---- | ---- | -------------------- |
E
add doc  
ester.zhou 已提交
1013 1014 1015 1016
| by     | By   | Yes  | Attributes of the target component.|

**Return value**

E
ester.zhou 已提交
1017 1018 1019
| Type                 | Description                             |
| --------------------- | --------------------------------- |
| Promise\<UiComponent> | Promise used to return the found component.|
E
add doc  
ester.zhou 已提交
1020 1021 1022

**Example**

E
ester.zhou 已提交
1023
```js
E
ester.zhou 已提交
1024 1025 1026 1027
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
1028 1029 1030 1031
```

### UiDriver.findComponents

E
ester.zhou 已提交
1032
findComponents(by: By): Promise\<Array\<UiComponent>>
E
add doc  
ester.zhou 已提交
1033 1034 1035 1036 1037 1038 1039

Searches this **UiDriver** object for all components that match the given attributes.

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

**Parameters**

E
ester.zhou 已提交
1040 1041
| Name| Type| Mandatory| Description                |
| ------ | ---- | ---- | -------------------- |
E
ester.zhou 已提交
1042
| by     | By   | Yes  | Attributes of the target component.|
E
add doc  
ester.zhou 已提交
1043 1044 1045

**Return value**

E
ester.zhou 已提交
1046 1047 1048
| Type                         | Description                                   |
| ----------------------------- | --------------------------------------- |
| Promise\<Array\<UiComponent>> | Promise used to return a list of found components.|
E
add doc  
ester.zhou 已提交
1049 1050 1051

**Example**

E
ester.zhou 已提交
1052
```js
E
ester.zhou 已提交
1053 1054 1055 1056
async function demo() {
    let driver = UiDriver.create()
    let buttonList = await driver.findComponents(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
1057 1058
```

E
ester.zhou 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
### UiDriver.waitForComponent<sup>9+</sup>

waitForComponent(by: By, time: number): Promise\<UiComponent>

Searches this **UiDriver** object for the target component that matches the given attributes within the specified duration.

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

**Parameters**

| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
| by     | By     | Yes  | Attributes of the target component.            |
| time   | number | Yes  | Duration for searching for the target component, in ms.|

**Return value**

| Type                 | Description                             |
| --------------------- | --------------------------------- |
| Promise\<UiComponent> | Promise used to return the found component.|

**Example**

```js
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.waitForComponent(BY.text('next page'),500)
}
```

E
add doc  
ester.zhou 已提交
1089 1090
### UiDriver.assertComponentExist   

E
ester.zhou 已提交
1091
assertComponentExist(by: By): Promise\<void>
E
add doc  
ester.zhou 已提交
1092 1093 1094 1095 1096 1097 1098

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.

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

**Parameters**

E
ester.zhou 已提交
1099 1100
| Name| Type| Mandatory| Description                |
| ------ | ---- | ---- | -------------------- |
E
add doc  
ester.zhou 已提交
1101 1102 1103 1104
| by     | By   | Yes  | Attributes of the target component.|

**Example**

E
ester.zhou 已提交
1105
```js
E
ester.zhou 已提交
1106 1107 1108 1109
async function demo() {
    let driver = UiDriver.create()
    await driver.assertComponentExist(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
1110 1111 1112 1113
```

### UiDriver.pressBack

E
ester.zhou 已提交
1114
pressBack(): Promise\<void>
E
add doc  
ester.zhou 已提交
1115 1116 1117 1118 1119 1120 1121

Presses the Back button on this **UiDriver** object.

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

**Example**

E
ester.zhou 已提交
1122
```js
E
ester.zhou 已提交
1123 1124 1125 1126
async function demo() {
    let driver = UiDriver.create()
    await driver.pressBack()
}
E
add doc  
ester.zhou 已提交
1127 1128 1129 1130
```

### UiDriver.triggerKey

E
ester.zhou 已提交
1131
triggerKey(keyCode: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1132 1133 1134 1135 1136 1137 1138

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

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

**Parameters**

E
ester.zhou 已提交
1139 1140
| Name | Type  | Mandatory| Description         |
| ------- | ------ | ---- | ------------- |
E
add doc  
ester.zhou 已提交
1141 1142 1143 1144
| keyCode | number | Yes  | Key code.|

**Example**

E
ester.zhou 已提交
1145
```js
E
ester.zhou 已提交
1146 1147 1148 1149
async function demo() {
    let driver = UiDriver.create()
    await driver.triggerKey(123)
}
E
add doc  
ester.zhou 已提交
1150 1151 1152 1153
```

### UiDriver.click

E
ester.zhou 已提交
1154
click(x: number, y: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1155 1156 1157 1158 1159 1160 1161

Clicks a specific point of this **UiDriver** object based on the given coordinates.

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

**Parameters**

E
ester.zhou 已提交
1162 1163 1164 1165
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| x      | number | Yes  | X coordinate of the target point.|
| y      | number | Yes  | Y coordinate of the target point.|
E
add doc  
ester.zhou 已提交
1166 1167 1168

**Example**

E
ester.zhou 已提交
1169
```js
E
ester.zhou 已提交
1170 1171 1172 1173
async function demo() {
    let driver = UiDriver.create()
    await driver.click(100,100)
}
E
add doc  
ester.zhou 已提交
1174 1175 1176 1177
```

### UiDriver.doubleClick

E
ester.zhou 已提交
1178
doubleClick(x: number, y: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1179 1180 1181 1182 1183 1184 1185

Double-clicks a specific point of this **UiDriver** object based on the given coordinates.

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

**Parameters**

E
ester.zhou 已提交
1186 1187 1188 1189
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| x      | number | Yes  | X coordinate of the target point.|
| y      | number | Yes  | Y coordinate of the target point.|
E
add doc  
ester.zhou 已提交
1190 1191 1192

**Example**

E
ester.zhou 已提交
1193
```js
E
ester.zhou 已提交
1194 1195 1196 1197
async function demo() {
    let driver = UiDriver.create()
    await driver.doubleClick(100,100)
}
E
add doc  
ester.zhou 已提交
1198 1199 1200 1201
```

### UiDriver.longClick

E
ester.zhou 已提交
1202
longClick(x: number, y: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1203 1204 1205 1206 1207 1208 1209

Long-clicks a specific point of this **UiDriver** object based on the given coordinates.

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

**Parameters**

E
ester.zhou 已提交
1210 1211 1212 1213
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
| x      | number | Yes  | X coordinate of the target point.|
| y      | number | Yes  | Y coordinate of the target point.|
E
add doc  
ester.zhou 已提交
1214 1215 1216

**Example**

E
ester.zhou 已提交
1217
```js
E
ester.zhou 已提交
1218 1219 1220 1221
async function demo() {
    let driver = UiDriver.create()
    await driver.longClick(100,100)
}
E
add doc  
ester.zhou 已提交
1222 1223 1224 1225
```

### UiDriver.swipe

E
ester.zhou 已提交
1226
swipe(startx: number, starty: number, endx: number, endy: number): Promise\<void>
E
add doc  
ester.zhou 已提交
1227

E
ester.zhou 已提交
1228
Swipes on this **UiDriver** object from the start point to the end point based on the given coordinates.
E
add doc  
ester.zhou 已提交
1229 1230 1231 1232 1233

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

**Parameters**

E
ester.zhou 已提交
1234 1235 1236 1237 1238 1239
| 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.|
E
add doc  
ester.zhou 已提交
1240 1241 1242

**Example**

E
ester.zhou 已提交
1243
```js
E
ester.zhou 已提交
1244 1245 1246 1247
async function demo() {
    let driver = UiDriver.create()
    await driver.swipe(100,100,200,200)
}
E
add doc  
ester.zhou 已提交
1248 1249
```

E
ester.zhou 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
### UiDriver.drag<sup>9+</sup>

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

Drags this **UiDriver** 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.|

**Example**

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

E
add doc  
ester.zhou 已提交
1276 1277
### UiDriver.screenCap

E
ester.zhou 已提交
1278
screenCap(savePath: string): Promise\<bool>
E
add doc  
ester.zhou 已提交
1279 1280 1281 1282 1283 1284 1285

Captures the current screen of this **UiDriver** object and saves it as a PNG image to the given save path.

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

**Parameters**

E
ester.zhou 已提交
1286 1287
| Name  | Type  | Mandatory| Description          |
| -------- | ------ | ---- | -------------- |
E
add doc  
ester.zhou 已提交
1288 1289
| savePath | string | Yes  | File save path.|

E
ester.zhou 已提交
1290 1291 1292 1293 1294 1295
**Return value**

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

E
add doc  
ester.zhou 已提交
1296 1297
**Example**

E
ester.zhou 已提交
1298
```js
E
ester.zhou 已提交
1299 1300 1301 1302
async function demo() {
    let driver = UiDriver.create()
    await driver.screenCap('/local/tmp/')
}
E
add doc  
ester.zhou 已提交
1303
```
E
ester.zhou 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

## MatchPattern

Enumerates the match patterns supported for component attributes.

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

| Name       | Value  | Description          |
| ----------- | ---- | -------------- |
| EQUALS      | 0    | Equal to the given value.  |
| CONTAINS    | 1    | Containing the given value.  |
| STARTS_WITH | 2    | Starting from the given value.|
| ENDS_WITH   | 3    | Ending with the given value.|

###