js-apis-uitest.md 25.3 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# UiTest

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


## Modules to Import

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

## By

The UiTest framework provides a wide range of UI component feature description APIs in the **By** class to filter and match components.
The API capabilities provided by the **By** class exhibit the following features:

E
ester.zhou 已提交
19
- Allows 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
add doc  
ester.zhou 已提交
20

E
ester.zhou 已提交
21
- Provides multiple match patterns for component attributes.
E
add doc  
ester.zhou 已提交
22

E
ester.zhou 已提交
23
- Supports absolute positioning and relative positioning for components. APIs such as **isBefore** and **isAfter** can be used to specify the features of adjacent components to assist positioning.
E
add doc  
ester.zhou 已提交
24

E
ester.zhou 已提交
25
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, for example, **BY.text('123').type('button')**.
E
add doc  
ester.zhou 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

### enum 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    | Contain the given value.  |
| STARTS_WITH | 2    | Start with the given value.|
| ENDS_WITH   | 3    | End with the given value.|

### By.text

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

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

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
66 67
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 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
```


### By.key

key(key:string):By;

Specifies the key attribute of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
96
let by = BY.key('123') // Use the static constructor BY to create a By object and specify the key attribute
E
ester.zhou 已提交
97
                          of the target component.
E
add doc  
ester.zhou 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
```


### By.id

id(id:number):By;

Specifies the ID property of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
126
let by = BY.id(123) // Use the static constructor BY to create a By object and specify the ID attribute
E
ester.zhou 已提交
127
                       of the target component.
E
add doc  
ester.zhou 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
```


### By.type

type(tp:string):By;

Specifies the type property of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

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


### By.clickable

clickable(b?:bool):By;

Specifies the clickable attribute of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
186 187
let by = BY.clickable(true) // Use the static constructor BY to create a By object and specify the clickable attribute 
                               of the target component.
E
add doc  
ester.zhou 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
```


### By.scrollable

scrollable(b?:bool):By;

Specifies the scrollable attribute of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
216
let by = BY.scrollable(true) // Use the static constructor BY to create a By object and specify the scrollable attribute 
E
ester.zhou 已提交
217
                                of the target component.
E
add doc  
ester.zhou 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
```

### By.enabled

enabled(b?:bool):By;

Specifies the enable attribute of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
245
let by = BY.enabled(true) // Use the static constructor BY to create a By object and specify the enable attribute 
E
ester.zhou 已提交
246
                             of the target component.
E
add doc  
ester.zhou 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
```

### By.focused

focused(b?:bool):By;

Specifies the focused attribute of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
274
let by = BY.enabled(true) // Use the static constructor BY to create a By object and specify the focused attribute 
E
ester.zhou 已提交
275
                             of the target component.
E
add doc  
ester.zhou 已提交
276 277 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
```

### By.selected

selected(b?:bool):By;

Specifies the selected attribute of the target component.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
303
let by = BY.selected(true) // Use the static constructor BY to create a By object and specify the selected attribute 
E
ester.zhou 已提交
304
                              of the target component.
E
add doc  
ester.zhou 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
```

### By.isBefore

isBefore(by:By):By;

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

**Required permissions**: none

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

**Parameters**

| Name| Type| Mandatory| Description          |
| ------ | ---- | ---- | -------------- |
| by     | By   | Yes  | Attributes of the component before which the target component is located.|

**Return value**

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

**Example**

```
E
ester.zhou 已提交
332
let by = BY.isBefore(BY.text('123')) // Use the static constructor BY to create a By object and specify the attributes 
E
ester.zhou 已提交
333
                                        of the component before which the target component is located.
E
add doc  
ester.zhou 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
```

### By.isAfter

isAfter(by:By):By;

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

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
361
let by = BY.isAfter(BY.text('123')) // Use the static constructor BY to create a By object and specify the attributes 
E
ester.zhou 已提交
362
                                       of the component after which the target component is located.
E
add doc  
ester.zhou 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
```

## 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.
All APIs provided by this class use a promise to return the result and must be invoked using **await**.

### UiComponent.click

click():Promise<void>;

Clicks this component.

**Required permissions**: none

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

**Example**

```
E
ester.zhou 已提交
383 384 385 386 387
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    await button.click()
}
E
add doc  
ester.zhou 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
```

### UiComponent.doubleClick

doubleClick():Promise<void>;

Double-clicks this component.

**Required permissions**: none

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

**Example**

```
E
ester.zhou 已提交
403 404 405 406 407
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    await buttont.doubleClick()
}
E
add doc  
ester.zhou 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
```

### UiComponent.longClick

longClick():Promise<void>;

Long-clicks this component.

**Required permissions**: none

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

**Example**

```
E
ester.zhou 已提交
423 424 425 426 427
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    await button.longClick()
}
E
add doc  
ester.zhou 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
```

### UiComponent.getId

getId():Promise<number>;

Obtains the ID of this component.

**Required permissions**: none

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

**Return value**

| Type            | Description                     |
| ---------------- | ------------------------- |
| Promise<number>; | Promise used to return the component ID.|

**Example**

```
E
ester.zhou 已提交
449 450 451 452 453
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 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
```

### UiComponent.getKey

getKey():Promise<string>;

Obtains the key of this component.

**Required permissions**: none

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

**Return value**

| Type            | Description                      |
| ---------------- | -------------------------- |
| Promise<string>; | Promise used to return the component key.|

**Example**

```
E
ester.zhou 已提交
475 476 477 478 479
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 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
```

### UiComponent.getText

getText():Promise<string>;

Obtains the text information of this component.

**Required permissions**: none

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

**Return value**

| Type            | Description                           |
| ---------------- | ------------------------------- |
| Promise<string>; | Promise used to return the text information of the component.|

**Example**

```
E
ester.zhou 已提交
501 502 503 504 505
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 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
```

### UiComponent.getType

getType():Promise<string>;

Obtains the type of this component.

**Required permissions**: none

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

**Return value**

| Type            | Description                           |
| ---------------- | ------------------------------- |
| Promise<string>; | Promise used to return the component type.|

**Example**

```
E
ester.zhou 已提交
527 528 529 530 531
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 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
```

### UiComponent.isClickable

isClickable():Promise<bool>;

Obtains the clickable status of this component.

**Required permissions**: none

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
553 554 555 556 557 558 559 560 561 562
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 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
```

### UiComponent.isScrollable

isScrollable():Promise<bool>;

Obtains the scrollable status of this component.

**Required permissions**: none

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
584 585 586 587 588 589 590 591 592 593
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 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
```


### UiComponent.isEnabled

isEnabled():Promise<bool>;

Obtains the enable status of this component.

**Required permissions**: none

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
616 617 618 619 620 621 622 623 624 625 626
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 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
```

### UiComponent.isFocused

isFocused():Promise<bool>;

Obtains the focused status of this component.

**Required permissions**: none

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
648 649 650 651 652 653 654 655 656 657
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 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
```

### UiComponent.isSelected

isSelected():Promise<bool>;

Obtains the selected status of this component.

**Required permissions**: none

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
679 680 681 682 683 684 685 686 687 688
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 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
```

### UiComponent.inputText

inputText(text: string):Promise<void>;

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

**Required permissions**: none

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

**Parameters**

| Name| Type  | Mandatory| Description                     |
| ------ | ------ | ---- | ------------------------- |
| text   | string | Yes  | Text to be entered to the component.|

**Example**

```
E
ester.zhou 已提交
710 711 712 713 714
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.type('button'))
    await button.inputText('next page')
}
E
add doc  
ester.zhou 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
```

### UiComponent.scrollSearch

scrollSearch(by:By):Promise<UiComponent>;

Scrolls on this component to search for the target component (available for lists).

**Required permissions**: none

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

**Parameters**

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

**Return value**

| Type                 | Description                               |
| --------------------- | ----------------------------------- |
| Promise<UiComponent>; | Promise used to return the target component.|

**Example**

```
E
ester.zhou 已提交
742 743 744 745 746
async function demo() {
    let driver = UiDriver.create()
    let scrollBar = await driver.findComponent(BY.scrollable(true))
    let button = await scrollBar.scrollSearch(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
```

## 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

static create():UiDriver;

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

**Required permissions**: none

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

**Return value**

| Type   | Description                  |
| ------- | ---------------------- |
| UiDrive | Returns the **UiDriver** object created.|

**Example**

```
E
ester.zhou 已提交
773 774 775
async function demo() {
    let driver = UiDriver.create()
}
E
add doc  
ester.zhou 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
```

### UiDriver.delayMs

delayMs(duration:number):Promise<void>;

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

**Required permissions**: none

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

**Parameters**

| Name  | Type  | Mandatory| Description      |
| -------- | ------ | ---- | ---------- |
| duration | number | Yes  | Duration of time.|

**Example**

```
E
ester.zhou 已提交
797 798 799 800
async function demo() {
    let driver = UiDriver.create()
    await driver.delayMs(1000)
}
E
add doc  
ester.zhou 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
```

### UiDriver.findComponent

findComponent(by:By):Promise<UiComponent>;

Searches this **UiDriver** object for the target component that has the given attributes.

**Required permissions**: none

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

**Parameters**

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

**Return value**

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

**Example**

```
E
ester.zhou 已提交
828 829 830 831
async function demo() {
    let driver = UiDriver.create()
    let button = await driver.findComponent(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
```

### UiDriver.findComponents

findComponents(by:By):Promise<Array<UiComponent>>;

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

**Required permissions**: none

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

**Parameters**

| Name| Type| Mandatory| Description              |
| ------ | ---- | ---- | ------------------ |
E
ester.zhou 已提交
848
| by     | By   | Yes  | Attributes of the target component.|
E
add doc  
ester.zhou 已提交
849 850 851 852 853 854 855 856 857 858

**Return value**

| Type                        | Description                                 |
| ---------------------------- | ------------------------------------- |
| Promise<Array<UiComponent>>; | Promise used to return a list of found components.|

**Example**

```
E
ester.zhou 已提交
859 860 861 862
async function demo() {
    let driver = UiDriver.create()
    let buttonList = await driver.findComponents(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
```

### UiDriver.assertComponentExist   

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.

**Required permissions**: none

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

**Parameters**

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

**Example**

```
E
ester.zhou 已提交
884 885 886 887
async function demo() {
    let driver = UiDriver.create()
    await driver.assertComponentExist(BY.text('next page'))
}
E
add doc  
ester.zhou 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
```

### UiDriver.pressBack

pressBack():Promise<void>;

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

**Required permissions**: none

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

**Example**

```
E
ester.zhou 已提交
903 904 905 906
async function demo() {
    let driver = UiDriver.create()
    await driver.pressBack()
}
E
add doc  
ester.zhou 已提交
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
```

### UiDriver.triggerKey

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

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

**Required permissions**: none

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

**Parameters**

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

**Example**

```
E
ester.zhou 已提交
928 929 930 931
async function demo() {
    let driver = UiDriver.create()
    await driver.triggerKey(123)
}
E
add doc  
ester.zhou 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
```

### UiDriver.click

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

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

**Required permissions**: none

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

**Parameters**

| Name| Type         | Mandatory| Description                                       |
| ------ | ------------- | ---- | ------------------------------------------- |
| x,y    | number,number | Yes  | Coordinate information of a specific point in the (number,number) format.|

**Example**

```
E
ester.zhou 已提交
953 954 955 956
async function demo() {
    let driver = UiDriver.create()
    await driver.click(100,100)
}
E
add doc  
ester.zhou 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
```

### UiDriver.doubleClick

doubleClick(x:number,y:number):Promise<void>;

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

**Required permissions**: none

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

**Parameters**

| Name| Type         | Mandatory| Description                                       |
| ------ | ------------- | ---- | ------------------------------------------- |
| x,y    | number,number | Yes  | Coordinate information of a specific point in the (number,number) format.|

**Example**

```
E
ester.zhou 已提交
978 979 980 981
async function demo() {
    let driver = UiDriver.create()
    await driver.doubleClick(100,100)
}
E
add doc  
ester.zhou 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
```

### UiDriver.longClick

longClick(x:number,y:number):Promise<void>;

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

**Required permissions**: none

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

**Parameters**

| Name| Type         | Mandatory| Description                                       |
| ------ | ------------- | ---- | ------------------------------------------- |
| x,y    | number,number | Yes  | Coordinate information of a specific point in the (number,number) format.|

**Example**

```
E
ester.zhou 已提交
1003 1004 1005 1006
async function demo() {
    let driver = UiDriver.create()
    await driver.longClick(100,100)
}
E
add doc  
ester.zhou 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
```

### UiDriver.swipe

swipe(startx:number,starty:number,endx:number,endy:number):Promise<void>;

Swipes from the start point to the end point of this **UiDriver** object based on the given coordinates.

**Required permissions**: none

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

**Parameters**

| Name       | Type         | Mandatory| Description                                       |
| ------------- | ------------- | ---- | ------------------------------------------- |
| startx,starty | number,number | Yes  | Coordinate information of the start point in the (number,number) format.|
| endx,endy     | number,number | Yes  | Coordinate information of the end point in the (number,number) format.|

**Example**

```
E
ester.zhou 已提交
1029 1030 1031 1032
async function demo() {
    let driver = UiDriver.create()
    await driver.swipe(100,100,200,200)
}
E
add doc  
ester.zhou 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
```

### UiDriver.screenCap

screenCap(savePath:string):Promise<bool>;

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

**Required permissions**: none

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

**Parameters**

| Name  | Type  | Mandatory| Description        |
| -------- | ------ | ---- | ------------ |
| savePath | string | Yes  | File save path.|

**Example**

```
E
ester.zhou 已提交
1054 1055 1056 1057
async function demo() {
    let driver = UiDriver.create()
    await driver.screenCap('/local/tmp/')
}
E
add doc  
ester.zhou 已提交
1058
```