1# @ohos.UiTest 2 3The **UiTest** module provides APIs that you can use to simulate UI actions during testing, such as clicks, double-clicks, long-clicks, and swipes. 4 5This module provides the following functions: 6 7- [On<sup>9+</sup>](#on9): provides UI component feature description APIs for component filtering and matching. 8- [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. 9- [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. 10- [UiWindow<sup>9+</sup>](#uiwindow9): works as the entry class and provides APIs for obtaining window attributes, dragging windows, and adjusting window sizes. 11- [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. 12- [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. 13- [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. 14 15>**NOTE** 16> 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. 18 19 20## Modules to Import 21 22```ts 23import {UiComponent, UiDriver, Component, Driver, UiWindow, ON, BY, MatchPattern, DisplayRotation, ResizeDirection, WindowMode, PointerMatrix, UiDirection, MouseButton, UIElementInfo, UIEventObserver} from '@ohos.UiTest'; 24``` 25 26## MatchPattern 27 28Enumerates the match patterns supported for component attributes. 29 30**System capability**: SystemCapability.Test.UiTest 31 32| Name | Value | Description | 33| ----------- | ---- | -------------- | 34| EQUALS | 0 | Equals the given value. | 35| CONTAINS | 1 | Contains the given value. | 36| STARTS_WITH | 2 | Starts with the given value.| 37| ENDS_WITH | 3 | Ends with the given value.| 38 39## ResizeDirection<sup>9+</sup> 40 41Enumerates the directions in which a window can be resized. 42 43**System capability**: SystemCapability.Test.UiTest 44 45| Name | Value | Description | 46| ---------- | ---- | -------- | 47| LEFT | 0 | Left. | 48| RIGHT | 1 | Right. | 49| UP | 2 | Up. | 50| DOWN | 3 | Down. | 51| LEFT_UP | 4 | Upper left.| 52| LEFT_DOWN | 5 | Lower left.| 53| RIGHT_UP | 6 | Upper right.| 54| RIGHT_DOWN | 7 | Lower right.| 55 56## Point<sup>9+</sup> 57 58Provides the coordinates of a point. 59 60**System capability**: SystemCapability.Test.UiTest 61 62| Name| Type | Readable| Writable| Description | 63| ---- | ------ | ---- | ---- | ---------------- | 64| x | number | Yes | No | X coordinate of a point.| 65| y | number | Yes | No | Y coordinate of a point.| 66 67## Rect<sup>9+</sup> 68 69Provides bounds information of a component. 70 71**System capability**: SystemCapability.Test.UiTest 72 73| Name | Type | Readable| Writable| Description | 74| ------ | ------ | ---- | ---- | ------------------------- | 75| left | number | Yes | No | X coordinate of the upper left corner of the component bounds.| 76| top | number | Yes | No | Y coordinate of the upper left corner of the component bounds.| 77| right | number | Yes | No | X coordinate of the lower right corner of the component bounds.| 78| bottom | number | Yes | No | Y coordinate of the lower right corner of the component bounds.| 79 80## WindowMode<sup>9+</sup> 81 82Enumerates the window modes. 83 84**System capability**: SystemCapability.Test.UiTest 85 86| Name | Value | Description | 87| ---------- | ---- | ---------- | 88| FULLSCREEN | 0 | Full-screen mode.| 89| PRIMARY | 1 | Primary window mode. | 90| SECONDARY | 2 | Secondary window mode.| 91| FLOATING | 3 | Floating window mode.| 92 93## DisplayRotation<sup>9+</sup> 94 95Describes the display rotation of the device. 96 97**System capability**: SystemCapability.Test.UiTest 98 99| Name | Value | Description | 100| ------------ | ---- | ---------------------------------------- | 101| ROTATION_0 | 0 | The device display is not rotated and is in its original vertical orientation. | 102| ROTATION_90 | 1 | The device display rotates 90° clockwise and is in landscape orientation. | 103| ROTATION_180 | 2 | The device display rotates 180° clockwise and is in reverse vertical orientation.| 104| ROTATION_270 | 3 | The device display rotates 270° clockwise and is in reverse landscape orientation.| 105 106## WindowFilter<sup>9+</sup> 107 108Provides the flag attributes of this window. 109 110**System capability**: SystemCapability.Test.UiTest 111 112| Name | Type | Readable| Writable| Description | 113| -------------------- | ------- | ---- | ---- | ------------------------------------------------------------ | 114| bundleName | string | Yes | No | Bundle name of the application to which the window belongs. | 115| title | string | Yes | No | Title of the window. | 116| focused | boolean | Yes | No | Whether the window is in focused state. | 117| actived(deprecated) | boolean | Yes | No | Whether the window is interacting with the user.<br>Since API version 11, this parameter is renamed **active**.| 118| active<sup>11+</sup> | boolean | Yes | No | Whether the window is interacting with the user. | 119 120## UiDirection<sup>10+</sup> 121 122Describes the direction of a UI operation such as fling. 123 124**System capability**: SystemCapability.Test.UiTest 125 126| Name | Value | Description | 127| ----- | ---- | ------ | 128| LEFT | 0 | Leftward.| 129| RIGHT | 1 | Rightward.| 130| UP | 2 | Upward.| 131| DOWN | 3 | Downward.| 132 133## MouseButton<sup>10+</sup> 134 135Describes the injected simulated mouse button. 136 137**System capability**: SystemCapability.Test.UiTest 138 139| Name | Value | Description | 140| ------------------- | ---- | ------------ | 141| MOUSE_BUTTON_LEFT | 0 | Left button on the mouse. | 142| MOUSE_BUTTON_RIGHT | 1 | Right button on the mouse. | 143| MOUSE_BUTTON_MIDDLE | 2 | Middle button on the mouse.| 144 145## UIElementInfo<sup>10+</sup> 146 147Provides information about the UI event. 148 149**System capability**: SystemCapability.Test.UiTest 150 151| Name | Type | Readable| Writable| Description | 152| ---------- | ------ | ---- | ---- | --------------------- | 153| bundleName | string | Yes | No | Bundle name of the home application. | 154| type | string | Yes | No | Component or window type. | 155| text | string | Yes | No | Text information of the component or window.| 156 157## On<sup>9+</sup> 158 159Since 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. 160 161The APIs provided by the **On** class exhibit the following features: 162 163- 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. 164- Provide multiple match patterns for component attributes. 165- 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. 166 167All 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. 168 169```ts 170import { ON } from '@ohos.UiTest'; 171ON.text('123').type('Button'); 172``` 173 174### text<sup>9+</sup> 175 176text(txt: string, pattern?: MatchPattern): On 177 178Specifies the text attribute of the target component. Multiple match patterns are supported. 179 180**System capability**: SystemCapability.Test.UiTest 181 182**Parameters** 183 184| Name | Type | Mandatory| Description | 185| ------- | ----------------------------- | ---- | --------------------------------------------------- | 186| txt | string | Yes | Component text, used to match the target component. | 187| pattern | [MatchPattern](#matchpattern) | No | Match pattern. The default value is [EQUALS](#matchpattern).| 188 189**Return value** 190 191| Type | Description | 192| ---------- | ---------------------------------- | 193| [On](#on9) | **On** object that matches the text attribute of the target component.| 194 195**Example** 196 197```ts 198import { On, ON } from '@ohos.UiTest'; 199let on:On = ON.text('123'); // Use the static constructor ON to create an On object and specify the text attribute of the target component. 200``` 201 202### id<sup>9+</sup> 203 204id(id: string): On 205 206Specifies the ID attribute of the target component. 207 208**System capability**: SystemCapability.Test.UiTest 209 210**Parameters** 211 212| Name| Type | Mandatory| Description | 213| ------ | ------ | ---- | ---------------- | 214| id | string | Yes | Component ID.| 215 216**Return value** 217 218| Type | Description | 219| ---------- | -------------------------------- | 220| [On](#on9) | **On** object that matches the ID attribute of the target component.| 221 222**Example** 223 224```ts 225import { On, ON } from '@ohos.UiTest'; 226let on:On = ON.id('123'); // Use the static constructor ON to create an On object and specify the id attribute of the target component. 227``` 228 229 230### type<sup>9+</sup> 231 232type(tp: string): On 233 234Specifies the type attribute of the target component. 235 236**System capability**: SystemCapability.Test.UiTest 237 238**Parameters** 239 240| Name| Type | Mandatory| Description | 241| ------ | ------ | ---- | -------------- | 242| tp | string | Yes | Component type.| 243 244**Return value** 245 246| Type | Description | 247| ---------- | ---------------------------------------- | 248| [On](#on9) | **On** object that matches the type attribute of the target component.| 249 250**Example** 251 252```ts 253import { On, ON } from '@ohos.UiTest'; 254let on:On = ON.type('Button'); // Use the static constructor ON to create an On object and specify the type attribute of the target component. 255``` 256 257 258### clickable<sup>9+</sup> 259 260clickable(b?: boolean): On 261 262Specifies the clickable attribute of the target component. 263 264**System capability**: SystemCapability.Test.UiTest 265 266**Parameters** 267 268| Name| Type | Mandatory| Description | 269| ------ | ------- | ---- | ------------------------------------------------------------ | 270| b | boolean | No | Clickable status of the target component.<br>**true**: clickable.<br>**false**: not clickable.<br> Default value: **true**| 271 272**Return value** 273 274| Type | Description | 275| ---------- | ------------------------------------------ | 276| [On](#on9) | **On** object that matches the clickable attribute of the target component.| 277 278**Example** 279 280```ts 281import { On, ON } from '@ohos.UiTest'; 282let on:On = ON.clickable(true); // Use the static constructor ON to create an On object and specify the clickable attribute of the target component. 283``` 284 285### longClickable<sup>9+</sup> 286 287longClickable(b?: boolean): On 288 289Specifies the long-clickable attribute of the target component. 290 291**System capability**: SystemCapability.Test.UiTest 292 293**Parameters** 294 295| Name| Type | Mandatory| Description | 296| ------ | ------- | ---- | ------------------------------------------------------------ | 297| b | boolean | No | Long-clickable status of the target component.<br>**true**: long-clickable.<br>**false**: not long-clickable.<br> Default value: **true**| 298 299**Return value** 300 301| Type | Description | 302| ---------- | ---------------------------------------------- | 303| [On](#on9) | **On** object that matches the long-clickable attribute of the target component.| 304 305**Example** 306 307```ts 308import { On, ON } from '@ohos.UiTest'; 309let on:On = ON.longClickable(true); // Use the static constructor ON to create an On object and specify the longClickable attribute of the target component. 310``` 311 312 313### scrollable<sup>9+</sup> 314 315scrollable(b?: boolean): On 316 317Specifies the scrollable attribute of the target component. 318 319**System capability**: SystemCapability.Test.UiTest 320 321**Parameters** 322 323| Name| Type | Mandatory| Description | 324| ------ | ------- | ---- | ----------------------------------------------------------- | 325| b | boolean | No | Scrollable status of the target component.<br>**true**: scrollable.<br>**false**: not scrollable.<br> Default value: **true**| 326 327**Return value** 328 329| Type | Description | 330| ---------- | ------------------------------------------ | 331| [On](#on9) | **On** object that matches the scrollable attribute of the target component.| 332 333**Example** 334 335```ts 336import { On, ON } from '@ohos.UiTest'; 337let on:On = ON.scrollable(true); // Use the static constructor ON to create an On object and specify the scrollable attribute of the target component. 338``` 339 340### enabled<sup>9+</sup> 341 342enabled(b?: boolean): On 343 344Specifies the enabled attribute of the target component. 345 346**System capability**: SystemCapability.Test.UiTest 347 348**Parameters** 349 350| Name| Type | Mandatory| Description | 351| ------ | ------- | ---- | --------------------------------------------------------- | 352| b | boolean | No | Enabled status of the target component.<br>**true**: enabled.<br>**false**: not enabled.<br> Default value: **true**| 353 354**Return value** 355 356| Type | Description | 357| ---------- | ---------------------------------------- | 358| [On](#on9) | **On** object that matches the enabled attribute of the target component.| 359 360**Example** 361 362```ts 363import { On, ON } from '@ohos.UiTest'; 364let on:On = ON.enabled(true); // Use the static constructor ON to create an On object and specify the enabled attribute of the target component. 365``` 366 367### focused<sup>9+</sup> 368 369focused(b?: boolean): On 370 371Specifies the focused attribute of the target component. 372 373**System capability**: SystemCapability.Test.UiTest 374 375**Parameters** 376 377| Name| Type | Mandatory| Description | 378| ------ | ------- | ---- | ----------------------------------------------------- | 379| b | boolean | No | Focused status of the target component.<br>**true**: focused.<br>**false**: not focused.<br> Default value: **true**| 380 381**Return value** 382 383| Type | Description | 384| ---------- | ---------------------------------------- | 385| [On](#on9) | **On** object that matches the focused attribute of the target component.| 386 387**Example** 388 389```ts 390import { On, ON } from '@ohos.UiTest'; 391let on:On = ON.focused(true); // Use the static constructor ON to create an On object and specify the focused attribute of the target component. 392``` 393 394### selected<sup>9+</sup> 395 396selected(b?: boolean): On 397 398Specifies the selected attribute of the target component. 399 400**System capability**: SystemCapability.Test.UiTest 401 402**Parameters** 403 404| Name| Type | Mandatory| Description | 405| ------ | ------- | ---- | ------------------------------------------------------------ | 406| b | boolean | No | Selected status of the target component.<br>**true**: selected.<br>**false**: not selected.<br> Default value: **true**| 407 408**Return value** 409 410| Type | Description | 411| ---------- | ------------------------------------------ | 412| [On](#on9) | **On** object that matches the selected attribute of the target component.| 413 414**Example** 415 416```ts 417import { On, ON } from '@ohos.UiTest'; 418let on:On = ON.selected(true); // Use the static constructor ON to create an On object and specify the selected attribute of the target component. 419``` 420 421### checked<sup>9+</sup> 422 423checked(b?: boolean): On 424 425Specifies the checked attribute of the target component. 426 427**System capability**: SystemCapability.Test.UiTest 428 429**Parameters** 430 431| Name| Type | Mandatory| Description | 432| ------ | ------- | ---- | ------------------------------------------------------------ | 433| b | boolean | No | Checked status of the target component.<br>**true**: checked.<br>**false**: not checked.<br> Default value: **false**| 434 435**Return value** 436 437| Type | Description | 438| ---------- | ------------------------------------------ | 439| [On](#on9) | **On** object that matches the checked attribute of the target component.| 440 441**Example** 442 443```ts 444import { On, ON } from '@ohos.UiTest'; 445let on:On = ON.checked(true); // Use the static constructor ON to create an On object and specify the checked attribute of the target component. 446``` 447 448### checkable<sup>9+</sup> 449 450checkable(b?: boolean): On 451 452Specifies the checkable attribute of the target component. 453 454**System capability**: SystemCapability.Test.UiTest 455 456**Parameters** 457 458| Name| Type | Mandatory| Description | 459| ------ | ------- | ---- | ------------------------------------------------------------ | 460| b | boolean | No | Checkable status of the target component.<br>**true**: checkable.<br>**false**: not checkable.<br> Default value: **false**| 461 462**Return value** 463 464| Type | Description | 465| ---------- | -------------------------------------------- | 466| [On](#on9) | **On** object that matches the checkable attribute of the target component.| 467 468**Example** 469 470```ts 471import { On, ON } from '@ohos.UiTest'; 472let on:On = ON.checkable(true); // Use the static constructor ON to create an On object and specify the checkable attribute of the target component. 473``` 474 475### isBefore<sup>9+</sup> 476 477isBefore(on: On): On 478 479Specifies that the target component is located before the given attribute component. 480 481**System capability**: SystemCapability.Test.UiTest 482 483**Parameters** 484 485| Name| Type | Mandatory| Description | 486| ------ | ---------- | ---- | -------------------- | 487| on | [On](#on9) | Yes | Information about the attribute component.| 488 489**Return value** 490 491| Type | Description | 492| ---------- | ---------------------------------------------------- | 493| [On](#on9) | **On** object.| 494 495**Example** 496 497```ts 498import { On, ON } from '@ohos.UiTest'; 499 500// Use the static constructor ON to create an On object and specify that the target component is located before the given attribute component. 501let on:On = ON.type('Button').isBefore(ON.text('123')); // Search for the first <Button> component located before the component whose text is 123. 502``` 503 504### isAfter<sup>9+</sup> 505 506isAfter(on: On): On 507 508Specifies that the target component is located after the given attribute component. 509 510**System capability**: SystemCapability.Test.UiTest 511 512**Parameters** 513 514| Name| Type | Mandatory| Description | 515| ------ | ---------- | ---- | -------------------- | 516| on | [On](#on9) | Yes | Information about the attribute component.| 517 518**Return value** 519 520| Type | Description | 521| ---------- | ---------------------------------------------------- | 522| [On](#on9) | **On** object.| 523 524**Example** 525 526```ts 527import { On, ON } from '@ohos.UiTest'; 528 529// Use the static constructor ON to create an On object and specify that the target component is located after the given attribute component. 530let on:On = ON.type('Text').isAfter(ON.text('123')) // Search for the first <Text> component located after the component whose text is 123. 531``` 532 533### within<sup>10+</sup> 534 535within(on: On): On 536 537Specifies that the target component is located within the given attribute component. 538 539**System capability**: SystemCapability.Test.UiTest 540 541**Parameters** 542 543| Name| Type | Mandatory| Description | 544| ------ | ---------- | ---- | -------------------- | 545| on | [On](#on9) | Yes | Information about the attribute component.| 546 547**Return value** 548 549| Type | Description | 550| ---------- | -------------------------------------------------- | 551| [On](#on9) | **On** object.| 552 553**Example** 554 555```ts 556import { On, ON } from '@ohos.UiTest'; 557// Use the static constructor ON to create an On object and specify that the target component is located within the given attribute component. 558let on:On = ON.text('java').within(ON.type('Scroll')); // Search for the child component whose text is java within the <Scroller> component. 559``` 560 561### inWindow<sup>10+</sup> 562 563inWindow(bundleName: string): On; 564 565Specifies that the target component is located within the given application window. 566 567**System capability**: SystemCapability.Test.UiTest 568 569**Parameters** 570 571| Name | Type | Mandatory| Description | 572| ---------- | ------ | ---- | ---------------- | 573| bundleName | string | Yes | Bundle name of the application window.| 574 575**Return value** 576 577| Type | Description | 578| ---------- | ---------------------------------------------- | 579| [On](#on9) | **On** object.| 580 581**Example** 582 583```ts 584import { On, ON } from '@ohos.UiTest'; 585let on:On = ON.inWindow('com.uitestScene.acts'); // Use the static constructor ON to create an On object and specify that the target component is located within the given application window. 586``` 587 588### description<sup>11+</sup> 589 590description(val: string, pattern?: MatchPattern): On 591 592Specifies the description of the target component. Multiple match patterns are supported. 593 594**System capability**: SystemCapability.Test.UiTest 595 596**Parameters** 597 598| Name | Type | Mandatory| Description | 599| ------- | ----------------------------- | ---- | --------------------------------------------------- | 600| val | string | Yes | Description of the component. | 601| pattern | [MatchPattern](#matchpattern) | No | Match pattern. The default value is [EQUALS](#matchpattern).| 602 603**Return value** 604 605| Type | Description | 606| ---------- | ----------------------------------------- | 607| [On](#on9) | **On** object.| 608 609**Example** 610 611```ts 612import { On, ON } from '@ohos.UiTest'; 613let on:On = ON.description('123'); // Use the static constructor ON to create an On object and specify the description attribute of the target component. 614``` 615 616## Component<sup>9+</sup> 617 618Represents a component on the UI and provides APIs for obtaining component attributes, clicking a component, scrolling to search for a component, and text injection. 619 620All APIs provided in this class use a promise to return the result and must be invoked using **await**. 621 622### click<sup>9+</sup> 623 624click(): Promise\<void> 625 626Clicks this component. 627 628**System capability**: SystemCapability.Test.UiTest 629 630**Error codes** 631 632For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 633 634| ID| Error Message | 635| -------- | ---------------------------------------- | 636| 17000002 | if the async function was not called with await. | 637| 17000004 | if the component is invisible or destroyed. | 638 639**Example** 640 641```ts 642import { Driver, ON, Component } from '@ohos.UiTest'; 643async function demo() { 644 let driver:Driver = Driver.create(); 645 let button: Component = await driver.findComponent(ON.type('Button')); 646 await button.click(); 647} 648``` 649 650### doubleClick<sup>9+</sup> 651 652doubleClick(): Promise\<void> 653 654Double-clicks this component. 655 656**System capability**: SystemCapability.Test.UiTest 657 658**Error codes** 659 660For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 661 662| ID| Error Message | 663| -------- | ---------------------------------------- | 664| 17000002 | if the async function was not called with await. | 665| 17000004 | if the component is invisible or destroyed. | 666 667**Example** 668 669```ts 670import {Component, Driver, ON } from '@ohos.UiTest'; 671async function demo() { 672 let driver: Driver = Driver.create(); 673 let button: Component = await driver.findComponent(ON.type('Button')); 674 await button.doubleClick(); 675} 676``` 677 678### longClick<sup>9+</sup> 679 680longClick(): Promise\<void> 681 682Long-clicks this component. 683 684**System capability**: SystemCapability.Test.UiTest 685 686**Error codes** 687 688For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 689 690| ID| Error Message | 691| -------- | ---------------------------------------- | 692| 17000002 | if the async function was not called with await. | 693| 17000004 | if the component is invisible or destroyed. | 694 695**Example** 696 697```ts 698import { Component, Driver, ON } from '@ohos.UiTest'; 699async function demo() { 700 let driver: Driver = Driver.create(); 701 let button: Component = await driver.findComponent(ON.type('Button')); 702 await button.longClick(); 703} 704``` 705 706### getId<sup>9+</sup> 707 708getId(): Promise\<string> 709 710Obtains the ID of this component. 711 712**System capability**: SystemCapability.Test.UiTest 713 714**Return value** 715 716| Type | Description | 717| ---------------- | ------------------------------- | 718| Promise\<string> | Promise used to return the ID of the component.| 719 720**Error codes** 721 722For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 723 724| ID| Error Message | 725| -------- | ---------------------------------------- | 726| 17000002 | if the async function was not called with await. | 727| 17000004 | if the component is invisible or destroyed. | 728 729**Example** 730 731```ts 732import { Component, Driver, ON } from '@ohos.UiTest'; 733async function demo() { 734 let driver: Driver = Driver.create(); 735 let button: Component = await driver.findComponent(ON.type('Button')); 736 let id = await button.getId(); 737} 738``` 739 740### getText<sup>9+</sup> 741 742getText(): Promise\<string> 743 744Obtains the text information of this component. 745 746**System capability**: SystemCapability.Test.UiTest 747 748**Return value** 749 750| Type | Description | 751| ---------------- | --------------------------------- | 752| Promise\<string> | Promise used to return the text information of the component.| 753 754**Error codes** 755 756For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 757 758| ID| Error Message | 759| -------- | ---------------------------------------- | 760| 17000002 | if the async function was not called with await. | 761| 17000004 | if the component is invisible or destroyed. | 762 763**Example** 764 765```ts 766import { Component, Driver, ON } from '@ohos.UiTest'; 767async function demo() { 768 let driver: Driver = Driver.create(); 769 let button: Component = await driver.findComponent(ON.type('Button')); 770 let text = await button.getText(); 771} 772``` 773 774### getType<sup>9+</sup> 775 776getType(): Promise\<string> 777 778Obtains the type of this component. 779 780**System capability**: SystemCapability.Test.UiTest 781 782**Return value** 783 784| Type | Description | 785| ---------------- | ----------------------------- | 786| Promise\<string> | Promise used to return the type of the component.| 787 788**Error codes** 789 790For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 791 792| ID| Error Message | 793| -------- | ---------------------------------------- | 794| 17000002 | if the async function was not called with await. | 795| 17000004 | if the component is invisible or destroyed. | 796 797**Example** 798 799```ts 800import { Component, Driver, ON } from '@ohos.UiTest'; 801async function demo() { 802 let driver: Driver = Driver.create(); 803 let button: Component = await driver.findComponent(ON.type('Button')); 804 let type = await button.getType(); 805} 806``` 807 808### getBounds<sup>9+</sup> 809 810getBounds(): Promise\<Rect> 811 812Obtains the bounds of this component. 813 814**System capability**: SystemCapability.Test.UiTest 815 816**Return value** 817 818| Type | Description | 819| ------------------------ | ------------------------------------- | 820| Promise\<[Rect](#rect9)> | Promise used to return the bounds of the component.| 821 822**Error codes** 823 824For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 825 826| ID| Error Message | 827| -------- | ---------------------------------------- | 828| 17000002 | if the async function was not called with await. | 829| 17000004 | if the component is invisible or destroyed. | 830 831**Example** 832 833```ts 834import { Component, Driver, ON } from '@ohos.UiTest'; 835async function demo() { 836 let driver: Driver = Driver.create(); 837 let button: Component = await driver.findComponent(ON.type('Button')); 838 let rect = await button.getBounds(); 839} 840``` 841 842### getBoundsCenter<sup>9+</sup> 843 844getBoundsCenter(): Promise\<Point> 845 846Obtains the information about the center of the bounding box around this component. 847 848**System capability**: SystemCapability.Test.UiTest 849 850**Return value** 851 852| Type | Description | 853| -------------------------- | ----------------------------------------------- | 854| Promise\<[Point](#point9)> | Promise used to return the information about the center of the bounding box around the component.| 855 856**Error codes** 857 858For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 859 860| ID| Error Message | 861| -------- | ---------------------------------------- | 862| 17000002 | if the async function was not called with await. | 863| 17000004 | if the component is invisible or destroyed. | 864 865**Example** 866 867```ts 868import { Component, Driver, ON } from '@ohos.UiTest'; 869async function demo() { 870 let driver: Driver = Driver.create(); 871 let button: Component = await driver.findComponent(ON.type('Button')); 872 let point = await button.getBoundsCenter(); 873} 874``` 875 876### isClickable<sup>9+</sup> 877 878isClickable(): Promise\<boolean> 879 880Obtains the clickable status of this component. 881 882**System capability**: SystemCapability.Test.UiTest 883 884**Return value** 885 886| Type | Description | 887| ----------------- | ------------------------------------------------------------ | 888| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is clickable, and **false** means the opposite.| 889 890**Error codes** 891 892For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 893 894| ID| Error Message | 895| -------- | ---------------------------------------- | 896| 17000002 | if the async function was not called with await. | 897| 17000004 | if the component is invisible or destroyed. | 898 899**Example** 900 901```ts 902import { Component, Driver, ON } from '@ohos.UiTest'; 903async function demo() { 904 let driver: Driver = Driver.create(); 905 let button: Component = await driver.findComponent(ON.type('Button')); 906 if(await button.isClickable()) { 907 console.info('This button can be Clicked'); 908 } else { 909 console.info('This button can not be Clicked'); 910 } 911} 912``` 913 914### isLongClickable<sup>9+</sup> 915 916isLongClickable(): Promise\<boolean> 917 918Obtains the long-clickable status of this component. 919 920**System capability**: SystemCapability.Test.UiTest 921 922**Return value** 923 924| Type | Description | 925| ----------------- |--------------------------------------------------| 926| 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.| 927 928**Error codes** 929 930For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 931 932| ID| Error Message | 933| -------- | ---------------------------------------- | 934| 17000002 | if the async function was not called with await. | 935| 17000004 | if the component is invisible or destroyed. | 936 937**Example** 938 939```ts 940import { Component, Driver, ON } from '@ohos.UiTest'; 941async function demo() { 942 let driver: Driver = Driver.create(); 943 let button: Component = await driver.findComponent(ON.type('Button')); 944 if(await button.isLongClickable()) { 945 console.info('This button can longClick'); 946 } else { 947 console.info('This button can not longClick'); 948 } 949} 950``` 951 952### isChecked<sup>9+</sup> 953 954isChecked(): Promise\<boolean> 955 956Obtains the checked status of this component. 957 958**System capability**: SystemCapability.Test.UiTest 959 960**Return value** 961 962| Type | Description | 963| ----------------- | ------------------------------------------------------------ | 964| 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.| 965 966**Error codes** 967 968For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 969 970| ID| Error Message | 971| -------- | ---------------------------------------- | 972| 17000002 | if the async function was not called with await. | 973| 17000004 | if the component is invisible or destroyed. | 974 975**Example** 976 977```ts 978import { Component, Driver, ON } from '@ohos.UiTest'; 979async function demo() { 980 let driver: Driver = Driver.create(); 981 let checkBox: Component = await driver.findComponent(ON.type('Checkbox')); 982 if(await checkBox.isChecked()) { 983 console.info('This checkBox is checked'); 984 } else { 985 console.info('This checkBox is not checked'); 986 } 987} 988``` 989 990### isCheckable<sup>9+</sup> 991 992isCheckable(): Promise\<boolean> 993 994Obtains the checkable status of this component. 995 996**System capability**: SystemCapability.Test.UiTest 997 998**Return value** 999 1000| Type | Description | 1001| ----------------- | ------------------------------------------------------------ | 1002| 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.| 1003 1004**Error codes** 1005 1006For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1007 1008| ID| Error Message | 1009| -------- | ---------------------------------------- | 1010| 17000002 | if the async function was not called with await. | 1011| 17000004 | if the component is invisible or destroyed. | 1012 1013**Example** 1014 1015```ts 1016import { Component, Driver, ON } from '@ohos.UiTest'; 1017async function demo() { 1018 let driver: Driver = Driver.create(); 1019 let checkBox: Component = await driver.findComponent(ON.type('Checkbox')); 1020 if(await checkBox.isCheckable()) { 1021 console.info('This checkBox is checkable'); 1022 } else { 1023 console.info('This checkBox is not checkable'); 1024 } 1025} 1026``` 1027 1028### isScrollable<sup>9+</sup> 1029 1030isScrollable(): Promise\<boolean> 1031 1032Obtains the scrollable status of this component. 1033 1034**System capability**: SystemCapability.Test.UiTest 1035 1036**Return value** 1037 1038| Type | Description | 1039| ----------------- | ------------------------------------------------------------ | 1040| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is scrollable, and **false** means the opposite.| 1041 1042**Error codes** 1043 1044For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1045 1046| ID| Error Message | 1047| -------- | ---------------------------------------- | 1048| 17000002 | if the async function was not called with await. | 1049| 17000004 | if the component is invisible or destroyed. | 1050 1051**Example** 1052 1053```ts 1054import { Component, Driver, ON } from '@ohos.UiTest'; 1055async function demo() { 1056 let driver: Driver = Driver.create(); 1057 let scrollBar: Component = await driver.findComponent(ON.scrollable(true)); 1058 if(await scrollBar.isScrollable()) { 1059 console.info('This scrollBar can be operated'); 1060 } else { 1061 console.info('This scrollBar can not be operated'); 1062 } 1063} 1064``` 1065 1066 1067### isEnabled<sup>9+</sup> 1068 1069isEnabled(): Promise\<boolean> 1070 1071Obtains the enabled status of this component. 1072 1073**System capability**: SystemCapability.Test.UiTest 1074 1075**Return value** 1076 1077| Type | Description | 1078| ----------------- | ---------------------------------------------------------- | 1079| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is enabled, and **false** means the opposite.| 1080 1081**Error codes** 1082 1083For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1084 1085| ID| Error Message | 1086| -------- | ---------------------------------------- | 1087| 17000002 | if the async function was not called with await. | 1088| 17000004 | if the component is invisible or destroyed. | 1089 1090**Example** 1091 1092```ts 1093import { Component, Driver, ON } from '@ohos.UiTest'; 1094async function demo() { 1095 let driver: Driver = Driver.create(); 1096 let button: Component = await driver.findComponent(ON.type('Button')); 1097 if(await button.isEnabled()) { 1098 console.info('This button can be operated'); 1099 } else { 1100 console.info('This button can not be operated'); 1101 } 1102} 1103``` 1104 1105### isFocused<sup>9+</sup> 1106 1107isFocused(): Promise\<boolean> 1108 1109Obtains the focused status of this component. 1110 1111**System capability**: SystemCapability.Test.UiTest 1112 1113**Return value** 1114 1115| Type | Description | 1116| ----------------- | ------------------------------------------------------------ | 1117| 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.| 1118 1119**Error codes** 1120 1121For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1122 1123| ID| Error Message | 1124| -------- | ---------------------------------------- | 1125| 17000002 | if the async function was not called with await. | 1126| 17000004 | if the component is invisible or destroyed. | 1127 1128**Example** 1129 1130```ts 1131import { Component, Driver, ON } from '@ohos.UiTest'; 1132async function demo() { 1133 let driver: Driver = Driver.create(); 1134 let button: Component = await driver.findComponent(ON.type('Button')); 1135 if(await button.isFocused()) { 1136 console.info('This button is focused'); 1137 } else { 1138 console.info('This button is not focused'); 1139 } 1140} 1141``` 1142 1143### isSelected<sup>9+</sup> 1144 1145isSelected(): Promise\<boolean> 1146 1147Obtains the selected status of this component. 1148 1149**System capability**: SystemCapability.Test.UiTest 1150 1151**Return value** 1152 1153| Type | Description | 1154| ----------------- | --------------------------------------------------- | 1155| Promise\<boolean> | Promise used to return the selected status of the component. The value **true** means that the component is selected, and **false** means the opposite.| 1156 1157**Error codes** 1158 1159For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1160 1161| ID| Error Message | 1162| -------- | ---------------------------------------- | 1163| 17000002 | if the async function was not called with await. | 1164| 17000004 | if the component is invisible or destroyed. | 1165 1166**Example** 1167 1168```ts 1169import { Component, Driver, ON } from '@ohos.UiTest'; 1170async function demo() { 1171 let driver: Driver = Driver.create(); 1172 let button: Component = await driver.findComponent(ON.type('Button')); 1173 if(await button.isSelected()) { 1174 console.info('This button is selected'); 1175 } else { 1176 console.info('This button is not selected'); 1177 } 1178} 1179``` 1180 1181### inputText<sup>9+</sup> 1182 1183inputText(text: string): Promise\<void> 1184 1185Enters text into this component (available for text boxes). 1186 1187**System capability**: SystemCapability.Test.UiTest 1188 1189**Parameters** 1190 1191| Name| Type | Mandatory| Description | 1192| ------ | ------ | ---- | ---------------------------------------- | 1193| text | string | Yes | Text to enter, which can contain English and special characters.| 1194 1195**Error codes** 1196 1197For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1198 1199| ID| Error Message | 1200| -------- | ---------------------------------------- | 1201| 17000002 | if the async function was not called with await. | 1202| 17000004 | if the component is invisible or destroyed. | 1203 1204**Example** 1205 1206```ts 1207import { Component, Driver, ON } from '@ohos.UiTest'; 1208async function demo() { 1209 let driver: Driver = Driver.create(); 1210 let text: Component = await driver.findComponent(ON.text('hello world')); 1211 await text.inputText('123'); 1212} 1213``` 1214 1215### clearText<sup>9+</sup> 1216 1217clearText(): Promise\<void> 1218 1219Clears text in this component. This API is applicable to text boxes. 1220 1221**System capability**: SystemCapability.Test.UiTest 1222 1223**Error codes** 1224For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1225 1226| ID| Error Message | 1227| -------- | ---------------------------------------- | 1228| 17000002 | if the async function was not called with await. | 1229| 17000004 | if the component is invisible or destroyed. | 1230 1231**Example** 1232 1233```ts 1234import { Component, Driver, ON } from '@ohos.UiTest'; 1235async function demo() { 1236 let driver: Driver = Driver.create(); 1237 let text: Component = await driver.findComponent(ON.text('hello world')); 1238 await text.clearText(); 1239} 1240``` 1241 1242### scrollSearch<sup>9+</sup> 1243 1244scrollSearch(on: On): Promise\<Component> 1245 1246Scrolls on this component to search for the target component. This API is applicable to components that support scrolling. 1247 1248**System capability**: SystemCapability.Test.UiTest 1249 1250**Parameters** 1251 1252| Name| Type | Mandatory| Description | 1253| ------ | ---------- | ---- | -------------------- | 1254| on | [On](#on9) | Yes | Attributes of the target component.| 1255 1256**Return value** 1257 1258| Type | Description | 1259| ---------------------------------- | ------------------------------------- | 1260| Promise\<[Component](#component9)> | Promise used to return the target component.| 1261 1262**Error codes** 1263 1264For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1265 1266| ID| Error Message | 1267| -------- | ---------------------------------------- | 1268| 17000002 | if the async function was not called with await. | 1269| 17000004 | if the component is invisible or destroyed. | 1270 1271**Example** 1272 1273```ts 1274import { Component, Driver, ON } from '@ohos.UiTest'; 1275async function demo() { 1276 let driver: Driver = Driver.create(); 1277 let scrollBar: Component = await driver.findComponent(ON.type('Scroll')); 1278 let button = await scrollBar.scrollSearch(ON.text('next page')); 1279} 1280``` 1281 1282### scrollToTop<sup>9+</sup> 1283 1284scrollToTop(speed?: number): Promise\<void> 1285 1286Scrolls to the top of this component. This API is applicable to components that support scrolling. 1287 1288**System capability**: SystemCapability.Test.UiTest 1289 1290**Parameters** 1291 1292| Name| Type | Mandatory| Description | 1293| ------ | ------ | ---- | ------------------------------------------------------------ | 1294| 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.| 1295 1296**Error codes** 1297 1298For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1299 1300| ID| Error Message | 1301| -------- | ---------------------------------------- | 1302| 17000002 | if the async function was not called with await. | 1303| 17000004 | if the component is invisible or destroyed. | 1304 1305**Example** 1306 1307```ts 1308import { Component, Driver, ON } from '@ohos.UiTest'; 1309async function demo() { 1310 let driver: Driver = Driver.create(); 1311 let scrollBar: Component = await driver.findComponent(ON.type('Scroll')); 1312 await scrollBar.scrollToTop(); 1313} 1314``` 1315 1316### scrollToBottom<sup>9+</sup> 1317 1318scrollToBottom(speed?: number): Promise\<void> 1319 1320Scrolls to the bottom of this component. This API is applicable to components that support scrolling. 1321 1322**System capability**: SystemCapability.Test.UiTest 1323 1324**Parameters** 1325 1326| Name| Type | Mandatory| Description | 1327| ------ | ------ | ---- | ------------------------------------------------------------ | 1328| 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.| 1329 1330**Error codes** 1331 1332For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1333 1334| ID| Error Message | 1335| -------- | ---------------------------------------- | 1336| 17000002 | if the async function was not called with await. | 1337| 17000004 | if the component is invisible or destroyed. | 1338 1339**Example** 1340 1341```ts 1342import { Component, Driver, ON } from '@ohos.UiTest'; 1343async function demo() { 1344 let driver: Driver = Driver.create(); 1345 let scrollBar: Component = await driver.findComponent(ON.type('Scroll')); 1346 await scrollBar.scrollToBottom(); 1347} 1348``` 1349 1350### dragTo<sup>9+</sup> 1351 1352dragTo(target: Component): Promise\<void> 1353 1354Drags this component to the target component. 1355 1356**System capability**: SystemCapability.Test.UiTest 1357 1358**Parameters** 1359 1360| Name| Type | Mandatory| Description | 1361| ------ | ------------------------ | ---- | ---------- | 1362| target | [Component](#component9) | Yes | Target component.| 1363 1364**Error codes** 1365 1366For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1367 1368| ID| Error Message | 1369| -------- | ---------------------------------------- | 1370| 17000002 | if the async function was not called with await. | 1371| 17000004 | if the component is invisible or destroyed. | 1372 1373**Example** 1374 1375```ts 1376import { Component, Driver, ON } from '@ohos.UiTest'; 1377async function demo() { 1378 let driver: Driver = Driver.create(); 1379 let button: Component = await driver.findComponent(ON.type('Button')); 1380 let text: Component = await driver.findComponent(ON.text('hello world')); 1381 await button.dragTo(text); 1382} 1383``` 1384 1385### pinchOut<sup>9+</sup> 1386 1387pinchOut(scale: number): Promise\<void> 1388 1389Pinches a component to scale it up to the specified ratio. 1390 1391**System capability**: SystemCapability.Test.UiTest 1392 1393**Parameters** 1394 1395| Name| Type | Mandatory| Description | 1396| ------ | ------ | ---- | ---------------- | 1397| scale | number | Yes | Scale factor.| 1398 1399**Error codes** 1400 1401For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1402 1403| ID| Error Message | 1404| -------- | ---------------------------------------- | 1405| 17000002 | if the async function was not called with await. | 1406| 17000004 | if the component is invisible or destroyed. | 1407 1408**Example** 1409 1410```ts 1411import { Component, Driver, ON } from '@ohos.UiTest'; 1412async function demo() { 1413 let driver: Driver = Driver.create(); 1414 let image: Component = await driver.findComponent(ON.type('Image')); 1415 await image.pinchOut(1.5); 1416} 1417``` 1418 1419### pinchIn<sup>9+</sup> 1420 1421pinchIn(scale: number): Promise\<void> 1422 1423Pinches a component to scale it down to the specified ratio. 1424 1425**System capability**: SystemCapability.Test.UiTest 1426 1427**Parameters** 1428 1429| Name| Type | Mandatory| Description | 1430| ------ | ------ | ---- | ---------------- | 1431| scale | number | Yes | Scale factor.| 1432 1433**Error codes** 1434 1435For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1436 1437| ID| Error Message | 1438| -------- | ---------------------------------------- | 1439| 17000002 | if the async function was not called with await. | 1440| 17000004 | if the component is invisible or destroyed. | 1441 1442**Example** 1443 1444```ts 1445import { Component, Driver, ON } from '@ohos.UiTest'; 1446async function demo() { 1447 let driver: Driver = Driver.create(); 1448 let image: Component = await driver.findComponent(ON.type('Image')); 1449 await image.pinchIn(0.5); 1450} 1451``` 1452 1453### getDescription<sup>11+</sup> 1454 1455getDescription(): Promise\<string> 1456 1457Obtains the description of this component. 1458 1459**System capability**: SystemCapability.Test.UiTest 1460 1461**Return value** 1462 1463| Type | Description | 1464| ---------------- | --------------------------------- | 1465| Promise\<string> | Promise used to return the description of the component.| 1466 1467**Error codes** 1468 1469For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1470 1471| ID| Error Message | 1472| -------- | ------------------------------------------------ | 1473| 17000002 | if the async function was not called with await. | 1474| 17000004 | if the component is invisible or destroyed. | 1475 1476**Example** 1477 1478```ts 1479import { Component, Driver, ON } from '@ohos.UiTest'; 1480async function demo() { 1481 let driver: Driver = Driver.create(); 1482 let button: Component = await driver.findComponent(ON.type('Button')); 1483 let description = await button.getDescription(); 1484} 1485``` 1486 1487## Driver<sup>9+</sup> 1488 1489The **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. 1490 1491All APIs provided by this class, except **Driver.create()**, use a promise to return the result and must be invoked using **await**. 1492 1493### create<sup>9+</sup> 1494 1495static create(): Driver 1496 1497Creates a **Driver** object and returns the object created. This API is a static API. 1498 1499**System capability**: SystemCapability.Test.UiTest 1500 1501**Return value** 1502 1503| Type| Description | 1504| -------- | ---------------------- | 1505| Driver | **Driver** object created.| 1506 1507**Error codes** 1508 1509For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1510 1511| ID| Error Message | 1512| -------- | ------------------ | 1513| 17000001 | if the test framework failed to initialize. | 1514 1515**Example** 1516 1517```ts 1518import { Driver } from '@ohos.UiTest'; 1519async function demo() { 1520 let driver: Driver = Driver.create(); 1521} 1522``` 1523 1524### delayMs<sup>9+</sup> 1525 1526delayMs(duration: number): Promise\<void> 1527 1528Delays this **Driver** object within the specified duration. 1529 1530**System capability**: SystemCapability.Test.UiTest 1531 1532**Parameters** 1533 1534| Name | Type | Mandatory| Description | 1535| -------- | ------ | ---- | ---------------------- | 1536| duration | number | Yes | Duration of time, in ms.| 1537 1538**Error codes** 1539 1540For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1541 1542| ID| Error Message | 1543| -------- | ---------------------------------------- | 1544| 17000002 | if the async function was not called with await. | 1545 1546**Example** 1547 1548```ts 1549import { Driver } from '@ohos.UiTest'; 1550async function demo() { 1551 let driver: Driver = Driver.create(); 1552 await driver.delayMs(1000); 1553} 1554``` 1555 1556### findComponent<sup>9+</sup> 1557 1558findComponent(on: On): Promise\<Component> 1559 1560Searches this **Driver** object for the target component that matches the given attributes. 1561 1562**System capability**: SystemCapability.Test.UiTest 1563 1564**Parameters** 1565 1566| Name| Type | Mandatory| Description | 1567| ------ | ---------- | ---- | -------------------- | 1568| on | [On](#on9) | Yes | Attributes of the target component.| 1569 1570**Return value** 1571 1572| Type | Description | 1573| ---------------------------------- | --------------------------------- | 1574| Promise\<[Component](#component9)> | Promise used to return the found component.| 1575 1576**Error codes** 1577 1578For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1579 1580| ID| Error Message | 1581| -------- | ---------------------------------------- | 1582| 17000002 | if the async function was not called with await. | 1583 1584**Example** 1585 1586```ts 1587import { Component, Driver, ON } from '@ohos.UiTest'; 1588async function demo() { 1589 let driver: Driver = Driver.create(); 1590 let button: Component = await driver.findComponent(ON.text('next page')); 1591} 1592``` 1593 1594### findComponents<sup>9+</sup> 1595 1596findComponents(on: On): Promise\<Array\<Component>> 1597 1598Searches this **Driver** object for all components that match the given attributes. 1599 1600**System capability**: SystemCapability.Test.UiTest 1601 1602**Parameters** 1603 1604| Name| Type | Mandatory| Description | 1605| ------ | ---------- | ---- | -------------------- | 1606| on | [On](#on9) | Yes | Attributes of the target component.| 1607 1608**Return value** 1609 1610| Type | Description | 1611| ------------------------------------------ | --------------------------------------- | 1612| Promise\<Array\<[Component](#component9)>> | Promise used to return a list of found components.| 1613 1614**Error codes** 1615 1616For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1617 1618| ID| Error Message | 1619| -------- | ---------------------------------------- | 1620| 17000002 | if the async function was not called with await. | 1621 1622**Example** 1623 1624```ts 1625import { Component, Driver, ON } from '@ohos.UiTest'; 1626async function demo() { 1627 let driver: Driver = Driver.create(); 1628 let buttonList: Array<Component> = await driver.findComponents(ON.text('next page')); 1629} 1630``` 1631 1632### findWindow<sup>9+</sup> 1633 1634findWindow(filter: WindowFilter): Promise\<UiWindow> 1635 1636Searches for the window that matches the specified attributes. 1637 1638**System capability**: SystemCapability.Test.UiTest 1639 1640**Parameters** 1641 1642| Name| Type | Mandatory| Description | 1643| ------ | ------------------------------ | ---- | ---------------- | 1644| filter | [WindowFilter](#windowfilter9) | Yes | Attributes of the target window.| 1645 1646**Return value** 1647 1648| Type | Description | 1649| -------------------------------- | ------------------------------------- | 1650| Promise\<[UiWindow](#uiwindow9)> | Promise used to return the target window.| 1651 1652**Error codes** 1653 1654For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1655 1656| ID| Error Message | 1657| -------- | ---------------------------------------- | 1658| 17000002 | if the async function was not called with await. | 1659 1660**Example** 1661 1662```ts 1663import { Driver, UiWindow } from '@ohos.UiTest'; 1664async function demo() { 1665 let driver: Driver = Driver.create(); 1666 let window: UiWindow = await driver.findWindow({actived: true}); 1667} 1668``` 1669 1670### waitForComponent<sup>9+</sup> 1671 1672waitForComponent(on: On, time: number): Promise\<Component> 1673 1674Searches this **Driver** object for the target component that matches the given attributes within the specified duration. 1675 1676**System capability**: SystemCapability.Test.UiTest 1677 1678**Parameters** 1679 1680| Name| Type | Mandatory| Description | 1681| ------ | ---------- | ---- | -------------------------------- | 1682| On | [On](#on9) | Yes | Attributes of the target component. | 1683| time | number | Yes | Duration for searching for the target component, in ms.| 1684 1685**Return value** 1686 1687| Type | Description | 1688| --------------------------------- | --------------------------------- | 1689| Promise\<[Component](#component9)> | Promise used to return the found component.| 1690 1691**Error codes** 1692 1693For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1694 1695| ID| Error Message | 1696| -------- | ---------------------------------------- | 1697| 17000002 | if the async function was not called with await. | 1698 1699**Example** 1700 1701```ts 1702import { Component, Driver, ON } from '@ohos.UiTest'; 1703async function demo() { 1704 let driver: Driver = Driver.create(); 1705 let button: Component = await driver.waitForComponent(ON.text('next page'),500); 1706} 1707``` 1708 1709### assertComponentExist<sup>9+</sup> 1710 1711assertComponentExist(on: On): Promise\<void> 1712 1713Asserts that a component that matches the given attributes exists on the current page. 1714 1715**System capability**: SystemCapability.Test.UiTest 1716 1717**Parameters** 1718 1719| Name| Type | Mandatory| Description | 1720| ------ | ---------- | ---- | -------------------- | 1721| on | [On](#on9) | Yes | Attributes of the target component.| 1722 1723**Error codes** 1724 1725For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1726 1727| ID| Error Message | 1728| -------- | ---------------------------------------- | 1729| 17000002 | if the async function was not called with await. | 1730| 17000003 | if the assertion failed. | 1731 1732**Example** 1733 1734```ts 1735import { Driver, ON } from '@ohos.UiTest'; 1736async function demo() { 1737 let driver: Driver = Driver.create(); 1738 await driver.assertComponentExist(ON.text('next page')); 1739} 1740``` 1741 1742### pressBack<sup>9+</sup> 1743 1744pressBack(): Promise\<void> 1745 1746Presses the Back button on this **Driver** object. 1747 1748**System capability**: SystemCapability.Test.UiTest 1749 1750**Error codes** 1751 1752For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1753 1754| ID| Error Message | 1755| -------- | ---------------------------------------- | 1756| 17000002 | if the async function was not called with await. | 1757 1758**Example** 1759 1760```ts 1761import { Driver } from '@ohos.UiTest'; 1762async function demo() { 1763 let driver: Driver = Driver.create(); 1764 await driver.pressBack(); 1765} 1766``` 1767 1768### triggerKey<sup>9+</sup> 1769 1770triggerKey(keyCode: number): Promise\<void> 1771 1772Triggers the key of this **Driver** object that matches the given key code. 1773 1774**System capability**: SystemCapability.Test.UiTest 1775 1776**Parameters** 1777 1778| Name | Type | Mandatory| Description | 1779| ------- | ------ | ---- | ------------- | 1780| keyCode | number | Yes | Key code.| 1781 1782**Error codes** 1783 1784For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1785 1786| ID| Error Message | 1787| -------- | ---------------------------------------- | 1788| 17000002 | if the async function was not called with await. | 1789 1790**Example** 1791 1792```ts 1793import { Driver } from '@ohos.UiTest'; 1794async function demo() { 1795 let driver: Driver = Driver.create(); 1796 await driver.triggerKey(123); 1797} 1798``` 1799 1800### triggerCombineKeys<sup>9+</sup> 1801 1802triggerCombineKeys(key0: number, key1: number, key2?: number): Promise\<void> 1803 1804Triggers 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**. 1805 1806**System capability**: SystemCapability.Test.UiTest 1807 1808**Parameters** 1809 1810| Name| Type | Mandatory| Description | 1811| ------ | ------ | ---- | ------------------- | 1812| key0 | number | Yes | The first key value.| 1813| key1 | number | Yes | The second key value.| 1814| key2 | number | No | The third key value.| 1815 1816**Error codes** 1817 1818For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1819 1820| ID| Error Message | 1821| -------- | ---------------------------------------- | 1822| 17000002 | if the async function was not called with await. | 1823 1824**Example** 1825 1826```ts 1827import { Driver } from '@ohos.UiTest'; 1828async function demo() { 1829 let driver: Driver = Driver.create(); 1830 await driver.triggerCombineKeys(2072, 2047, 2035); 1831} 1832``` 1833 1834 1835### click<sup>9+</sup> 1836 1837click(x: number, y: number): Promise\<void> 1838 1839Clicks a specific point of this **Driver** object based on the given coordinates. 1840 1841**System capability**: SystemCapability.Test.UiTest 1842 1843**Parameters** 1844 1845| Name| Type | Mandatory| Description | 1846| ------ | ------ | ---- | -------------------------------------- | 1847| x | number | Yes | X coordinate of the target point.| 1848| y | number | Yes | Y coordinate of the target point.| 1849 1850**Error codes** 1851 1852For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1853 1854| ID| Error Message | 1855| -------- | ---------------------------------------- | 1856| 17000002 | if the async function was not called with await. | 1857 1858**Example** 1859 1860```ts 1861import { Driver } from '@ohos.UiTest'; 1862async function demo() { 1863 let driver: Driver = Driver.create(); 1864 await driver.click(100,100); 1865} 1866``` 1867 1868### doubleClick<sup>9+</sup> 1869 1870doubleClick(x: number, y: number): Promise\<void> 1871 1872Double-clicks a specific point of this **Driver** object based on the given coordinates. 1873 1874**System capability**: SystemCapability.Test.UiTest 1875 1876**Parameters** 1877 1878| Name| Type | Mandatory| Description | 1879| ------ | ------ | ---- | -------------------------------------- | 1880| x | number | Yes | X coordinate of the target point.| 1881| y | number | Yes | Y coordinate of the target point.| 1882 1883**Error codes** 1884 1885For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1886 1887| ID| Error Message | 1888| -------- | ---------------------------------------- | 1889| 17000002 | if the async function was not called with await. | 1890 1891**Example** 1892 1893```ts 1894import { Driver } from '@ohos.UiTest'; 1895async function demo() { 1896 let driver: Driver = Driver.create(); 1897 await driver.doubleClick(100,100); 1898} 1899``` 1900 1901### longClick<sup>9+</sup> 1902 1903longClick(x: number, y: number): Promise\<void> 1904 1905Long-clicks a specific point of this **Driver** object based on the given coordinates. 1906 1907**System capability**: SystemCapability.Test.UiTest 1908 1909**Parameters** 1910 1911| Name| Type | Mandatory| Description | 1912| ------ | ------ | ---- | -------------------------------------- | 1913| x | number | Yes | X coordinate of the target point.| 1914| y | number | Yes | Y coordinate of the target point.| 1915 1916**Error codes** 1917 1918For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1919 1920| ID| Error Message | 1921| -------- | ---------------------------------------- | 1922| 17000002 | if the async function was not called with await. | 1923 1924**Example** 1925 1926```ts 1927import { Driver } from '@ohos.UiTest'; 1928async function demo() { 1929 let driver: Driver = Driver.create(); 1930 await driver.longClick(100,100); 1931} 1932``` 1933 1934### swipe<sup>9+</sup> 1935 1936swipe(startx: number, starty: number, endx: number, endy: number, speed?: number): Promise\<void> 1937 1938Swipes on this **Driver** object from the given start point to the given end point. 1939 1940**System capability**: SystemCapability.Test.UiTest 1941 1942**Parameters** 1943 1944| Name| Type | Mandatory| Description | 1945| ------ | ------ | ---- | ------------------------------------------------------------ | 1946| startx | number | Yes | X coordinate of the start point. | 1947| starty | number | Yes | Y coordinate of the start point. | 1948| endx | number | Yes | X coordinate of the end point. | 1949| endy | number | Yes | Y coordinate of the end point. | 1950| 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.| 1951 1952**Error codes** 1953 1954For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1955 1956| ID| Error Message | 1957| -------- | ---------------------------------------- | 1958| 17000002 | if the async function was not called with await. | 1959 1960**Example** 1961 1962```ts 1963import { Driver } from '@ohos.UiTest'; 1964async function demo() { 1965 let driver: Driver = Driver.create(); 1966 await driver.swipe(100,100,200,200,600); 1967} 1968``` 1969 1970### drag<sup>9+</sup> 1971 1972drag(startx: number, starty: number, endx: number, endy: number, speed?: number): Promise\<void> 1973 1974Drags this **Driver** object from the given start point to the given end point. 1975 1976**System capability**: SystemCapability.Test.UiTest 1977 1978**Parameters** 1979 1980| Name| Type | Mandatory| Description | 1981| ------ | ------ | ---- | ------------------------------------------------------------ | 1982| startx | number | Yes | X coordinate of the start point. | 1983| starty | number | Yes | Y coordinate of the start point. | 1984| endx | number | Yes | X coordinate of the end point. | 1985| endy | number | Yes | Y coordinate of the end point. | 1986| 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.| 1987 1988**Error codes** 1989 1990For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 1991 1992| ID| Error Message | 1993| -------- | ---------------------------------------- | 1994| 17000002 | if the async function was not called with await. | 1995 1996**Example** 1997 1998```ts 1999import { Driver } from '@ohos.UiTest'; 2000async function demo() { 2001 let driver: Driver = Driver.create(); 2002 await driver.drag(100,100,200,200,600); 2003} 2004``` 2005 2006### screenCap<sup>9+</sup> 2007 2008screenCap(savePath: string): Promise\<boolean> 2009 2010Captures the current screen of this **Driver** object and saves it as a PNG image to the given save path. 2011 2012**System capability**: SystemCapability.Test.UiTest 2013 2014**Parameters** 2015 2016| Name | Type | Mandatory| Description | 2017| -------- | ------ | ---- | -------------- | 2018| savePath | string | Yes | File save path.| 2019 2020**Return value** 2021 2022| Type | Description | 2023| ----------------- | -------------------------------------- | 2024| Promise\<boolean> | Promise used to return the result. The value **true** means that the operation is successful.| 2025 2026**Error codes** 2027 2028For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2029 2030| ID| Error Message | 2031| -------- | ---------------------------------------- | 2032| 17000002 | if the async function was not called with await. | 2033 2034**Example** 2035 2036```ts 2037import { Driver } from '@ohos.UiTest'; 2038async function demo() { 2039 let driver: Driver = Driver.create(); 2040 await driver.screenCap('/data/storage/el2/base/cache/1.png'); 2041} 2042``` 2043 2044### setDisplayRotation<sup>9+</sup> 2045 2046setDisplayRotation(rotation: DisplayRotation): Promise\<void> 2047 2048Sets the display rotation of the device. 2049 2050**System capability**: SystemCapability.Test.UiTest 2051 2052**Parameters** 2053 2054| Name | Type | Mandatory| Description | 2055| -------- | ------------------------------------ | ---- | ---------------- | 2056| rotation | [DisplayRotation](#displayrotation9) | Yes | Display rotation of the device.| 2057 2058**Error codes** 2059 2060For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2061 2062| ID| Error Message | 2063| -------- | ---------------------------------------- | 2064| 17000002 | if the async function was not called with await. | 2065 2066**Example** 2067 2068```ts 2069import { Driver, DisplayRotation } from '@ohos.UiTest'; 2070async function demo() { 2071 let driver: Driver = Driver.create(); 2072 await driver.setDisplayRotation(DisplayRotation.ROTATION_180); 2073} 2074``` 2075 2076### getDisplayRotation<sup>9+</sup> 2077 2078getDisplayRotation(): Promise\<DisplayRotation> 2079 2080Obtains the display rotation of the current device. 2081 2082**System capability**: SystemCapability.Test.UiTest 2083 2084**Return value** 2085 2086| Type | Description | 2087| ---------------------------------------------- | --------------------------------------- | 2088| Promise\<[DisplayRotation](#displayrotation9)> | Promise used to return the display rotation of the current device.| 2089 2090**Error codes** 2091 2092For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2093 2094| ID| Error Message | 2095| -------- | ---------------------------------------- | 2096| 17000002 | if the async function was not called with await. | 2097 2098**Example** 2099 2100```ts 2101import { DisplayRotation, Driver } from '@ohos.UiTest'; 2102async function demo() { 2103 let driver: Driver = Driver.create(); 2104 let rotation: DisplayRotation = await driver.getDisplayRotation(); 2105} 2106``` 2107 2108### setDisplayRotationEnabled<sup>9+</sup> 2109 2110setDisplayRotationEnabled(enabled: boolean): Promise\<void> 2111 2112Enables or disables display rotation. 2113 2114**System capability**: SystemCapability.Test.UiTest 2115 2116**Parameters** 2117 2118| Name | Type | Mandatory| Description | 2119| ------- | ------- | ---- | ------------------------------------------------------- | 2120| enabled | boolean | Yes | Whether to enable display rotation. The value **true** means to enable display rotation, and **false** means the opposite.| 2121 2122**Error codes** 2123 2124For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2125 2126| ID| Error Message | 2127| -------- | ---------------------------------------- | 2128| 17000002 | if the async function was not called with await. | 2129 2130**Example** 2131 2132```ts 2133import { Driver } from '@ohos.UiTest'; 2134async function demo() { 2135 let driver: Driver = Driver.create(); 2136 await driver.setDisplayRotationEnabled(false); 2137} 2138``` 2139 2140### getDisplaySize<sup>9+</sup> 2141 2142getDisplaySize(): Promise\<Point> 2143 2144Obtains the display size of the current device. 2145 2146**System capability**: SystemCapability.Test.UiTest 2147 2148**Return value** 2149 2150| Type | Description | 2151| -------------------------- | --------------------------------------- | 2152| Promise\<[Point](#point9)> | Promise used to return the display size of the current device.| 2153 2154**Error codes** 2155 2156For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2157 2158| ID| Error Message | 2159| -------- | ---------------------------------------- | 2160| 17000002 | if the async function was not called with await. | 2161 2162**Example** 2163 2164```ts 2165import { Driver, } from '@ohos.UiTest'; 2166 2167async function demo() { 2168 let driver: Driver = Driver.create(); 2169 let size = await driver.getDisplaySize(); 2170} 2171``` 2172 2173### getDisplayDensity<sup>9+</sup> 2174 2175getDisplayDensity(): Promise\<Point> 2176 2177Obtains the display density of the current device. 2178 2179**System capability**: SystemCapability.Test.UiTest 2180 2181**Return value** 2182 2183| Type | Description | 2184| -------------------------- | ----------------------------------------- | 2185| Promise\<[Point](#point9)> | Promise used to return the display density of the current device.| 2186 2187**Error codes** 2188 2189For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2190 2191| ID| Error Message | 2192| -------- | ---------------------------------------- | 2193| 17000002 | if the async function was not called with await. | 2194 2195**Example** 2196 2197```ts 2198import { Driver } from '@ohos.UiTest'; 2199async function demo() { 2200 let driver: Driver = Driver.create(); 2201 let density = await driver.getDisplayDensity(); 2202} 2203``` 2204 2205### wakeUpDisplay<sup>9+</sup> 2206 2207wakeUpDisplay(): Promise\<void> 2208 2209Wakes up the device display. 2210 2211**System capability**: SystemCapability.Test.UiTest 2212 2213**Error codes** 2214 2215For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2216 2217| ID| Error Message | 2218| -------- | ---------------------------------------- | 2219| 17000002 | if the async function was not called with await. | 2220 2221**Example** 2222 2223```ts 2224import { Driver } from '@ohos.UiTest'; 2225async function demo() { 2226 let driver: Driver = Driver.create(); 2227 await driver.wakeUpDisplay(); 2228} 2229``` 2230 2231### pressHome<sup>9+</sup> 2232 2233pressHome(): Promise\<void> 2234 2235Returns to the home screen. 2236 2237**System capability**: SystemCapability.Test.UiTest 2238 2239**Error codes** 2240 2241For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2242 2243| ID| Error Message | 2244| -------- | ---------------------------------------- | 2245| 17000002 | if the async function was not called with await. | 2246 2247**Example** 2248 2249```ts 2250import { Driver } from '@ohos.UiTest'; 2251async function demo() { 2252 let driver: Driver = Driver.create(); 2253 await driver.pressHome(); 2254} 2255``` 2256 2257### waitForIdle<sup>9+</sup> 2258 2259waitForIdle(idleTime: number, timeout: number): Promise\<boolean> 2260 2261Checks whether all components on the current page are idle. 2262 2263**System capability**: SystemCapability.Test.UiTest 2264 2265**Parameters** 2266 2267| Name | Type | Mandatory| Description | 2268| -------- | ------ | ---- | ------------------------------------------------------------ | 2269| 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.| 2270| timeout | number | Yes | Maximum idle waiting time, in milliseconds. | 2271 2272**Return value** 2273 2274| Type | Description | 2275| ----------------- | --------------------------------------------------- | 2276| Promise\<boolean> | Promise used to return the result.| 2277 2278**Error codes** 2279 2280For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2281 2282| ID| Error Message | 2283| -------- | ---------------------------------------- | 2284| 17000002 | if the async function was not called with await. | 2285 2286**Example** 2287 2288```ts 2289import { Driver } from '@ohos.UiTest'; 2290async function demo() { 2291 let driver: Driver = Driver.create(); 2292 let idled:boolean = await driver.waitForIdle(4000,5000); 2293} 2294``` 2295 2296### fling<sup>9+</sup> 2297 2298fling(from: Point, to: Point, stepLen: number, speed: number): Promise\<void> 2299 2300Simulates a fling operation on the screen. 2301 2302**System capability**: SystemCapability.Test.UiTest 2303 2304**Parameters** 2305 2306| Name | Type | Mandatory| Description | 2307| ------- | ---------------- | ---- | ------------------------------------------------------------ | 2308| from | [Point](#point9) | Yes | Coordinates of the point where the finger touches the screen. | 2309| to | [Point](#point9) | Yes | Coordinates of the point where the finger leaves the screen. | 2310| stepLen | number | Yes | Fling step length, in pixels. | 2311| 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.| 2312 2313**Error codes** 2314 2315For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2316 2317| ID| Error Message | 2318| -------- | ---------------------------------------- | 2319| 17000002 | if the async function was not called with await. | 2320 2321**Example** 2322 2323```ts 2324import { Driver } from '@ohos.UiTest'; 2325async function demo() { 2326 let driver: Driver = Driver.create(); 2327 await driver.fling({x: 500, y: 480},{x: 450, y: 480},5,600); 2328} 2329``` 2330 2331### injectMultiPointerAction<sup>9+</sup> 2332 2333injectMultiPointerAction(pointers: PointerMatrix, speed?: number): Promise\<boolean> 2334 2335Injects a multi-touch operation to the device. 2336 2337**System capability**: SystemCapability.Test.UiTest 2338 2339**Parameters** 2340 2341| Name | Type | Mandatory| Description | 2342| -------- | -------------------------------- | ---- | ------------------------------------------------------------ | 2343| pointers | [PointerMatrix](#pointermatrix9) | Yes | Scroll trajectory, including the number of fingers and an array of coordinates along the trajectory. | 2344| 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.| 2345 2346**Return value** 2347 2348| Type | Description | 2349| ----------------- | ------------------------------------- | 2350| Promise\<boolean> | Promise used to return the result.| 2351 2352**Error codes** 2353 2354For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2355 2356| ID| Error Message | 2357| -------- | ---------------------------------------- | 2358| 17000002 | if the async function was not called with await. | 2359 2360**Example** 2361 2362```ts 2363import { Driver, PointerMatrix } from '@ohos.UiTest'; 2364async function demo() { 2365 let driver: Driver = Driver.create(); 2366 let pointers: PointerMatrix = PointerMatrix.create(2,3); 2367 pointers.setPoint(0,0,{x:230,y:480}); 2368 pointers.setPoint(0,1,{x:250,y:380}); 2369 pointers.setPoint(0,2,{x:270,y:280}); 2370 pointers.setPoint(1,0,{x:230,y:680}); 2371 pointers.setPoint(1,1,{x:240,y:580}); 2372 pointers.setPoint(1,2,{x:250,y:480}); 2373 await driver.injectMultiPointerAction(pointers); 2374} 2375``` 2376 2377### fling<sup>10+</sup> 2378 2379fling(direction: UiDirection, speed: number): Promise\<void>; 2380 2381Simulates a fling operation on the screen, in the specified direction and speed. 2382 2383**System capability**: SystemCapability.Test.UiTest 2384 2385**Parameters** 2386 2387| Name | Type | Mandatory| Description | 2388| --------- | ----------------------------- | ---- | ------------------------------------------------------------ | 2389| direction | [UiDirection](#uidirection10) | Yes | Direction of the fling operation. | 2390| 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.| 2391 2392**Error codes** 2393 2394For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2395 2396| ID| Error Message | 2397| -------- | ---------------------------------------- | 2398| 17000002 | if the async function was not called with await. | 2399 2400**Example** 2401 2402```ts 2403import { Driver, UiDirection } from '@ohos.UiTest'; 2404async function demo() { 2405 let driver: Driver = Driver.create(); 2406 await driver.fling(UiDirection.DOWN, 10000); 2407} 2408``` 2409 2410### screenCapture<sup>10+</sup> 2411 2412screenCapture(savePath: string, rect?: Rect): Promise\<boolean>; 2413 2414Captures the specified area of the current screen and saves the captured screenshot as a PNG image to the specified path. 2415 2416**System capability**: SystemCapability.Test.UiTest 2417 2418**Parameters** 2419 2420| Name | Type | Mandatory| Description | 2421| -------- | -------------- | ---- | ---------------------- | 2422| savePath | string | Yes | File save path. | 2423| rect | [Rect](#rect9) | No | Area of the screen to capture. The default value is the entire screen.| 2424 2425**Return value** 2426 2427| Type | Description | 2428| ----------------- | -------------------------------------- | 2429| Promise\<boolean> | Promise used to return the result. The value **true** means that the operation is successful.| 2430 2431**Error codes** 2432 2433For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2434 2435| ID| Error Message | 2436| -------- | ---------------------------------------- | 2437| 17000002 | if the async function was not called with await. | 2438 2439**Example** 2440 2441```ts 2442import { Driver } from '@ohos.UiTest'; 2443async function demo() { 2444 let driver: Driver = Driver.create(); 2445 await driver.screenCapture('/data/storage/el2/base/cache/1.png', {left: 0, top: 0, right: 100, bottom: 100}); 2446} 2447``` 2448 2449### mouseClick<sup>10+</sup> 2450 2451mouseClick(p: Point, btnId: MouseButton, key1?: number, key2?: number): Promise\<void>; 2452 2453Injects 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. 2454 2455**System capability**: SystemCapability.Test.UiTest 2456 2457**Parameters** 2458 2459| Name| Type | Mandatory| Description | 2460| ------ | ----------------------------- | ---- | ------------------- | 2461| p | [Point](#point9) | Yes | Coordinates of the mouse click. | 2462| btnId | [MouseButton](#mousebutton10) | Yes | Mouse button pressed. | 2463| key1 | number | No | The first key value.| 2464| key2 | number | No | The second key value.| 2465 2466**Error codes** 2467 2468For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2469 2470| ID| Error Message | 2471| -------- | ---------------------------------------- | 2472| 17000002 | if the async function was not called with await. | 2473 2474**Example** 2475 2476```ts 2477import { Driver,MouseButton } from '@ohos.UiTest'; 2478async function demo() { 2479 let driver: Driver = Driver.create(); 2480 await driver.mouseClick({x:248, y:194}, MouseButton.MOUSE_BUTTON_LEFT, 2072); 2481} 2482``` 2483 2484### mouseScroll<sup>10+</sup> 2485 2486mouseScroll(p: Point, down: boolean, d: number, key1?: number, key2?: number): Promise\<void>; 2487 2488Injects 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. 2489 2490**System capability**: SystemCapability.Test.UiTest 2491 2492**Parameters** 2493 2494| Name| Type | Mandatory| Description | 2495| ------ | ---------------- | ---- | ----------------------------------------------------------- | 2496| p | [Point](#point9) | Yes | Coordinates of the mouse click. | 2497| down | boolean | Yes | Whether the mouse wheel scrolls downward.<br>**true**: The mouse wheel scrolls downward.<br>**false**: The mouse wheel scrolls upward.| 2498| d | number | Yes | Number of cells by which the mouse wheel scrolls. Scrolling by one cell means a 120-pixel offset of the target point. | 2499| key1 | number | No | The first key value. | 2500| key2 | number | No | The second key value. | 2501 2502**Error codes** 2503 2504For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2505 2506| ID| Error Message | 2507| -------- | ---------------------------------------- | 2508| 17000002 | if the async function was not called with await. | 2509 2510**Example** 2511 2512```ts 2513import { Driver } from '@ohos.UiTest'; 2514async function demo() { 2515 let driver: Driver = Driver.create(); 2516 await driver.mouseScroll({x:360, y:640}, true, 30, 2072) 2517} 2518``` 2519 2520### mouseMoveTo<sup>10+</sup> 2521 2522mouseMoveTo(p: Point): Promise\<void>; 2523 2524Moves the cursor to the target point. 2525 2526**System capability**: SystemCapability.Test.UiTest 2527 2528**Parameters** 2529 2530| Name| Type | Mandatory| Description | 2531| ------ | ---------------- | ---- | -------------- | 2532| p | [Point](#point9) | Yes | Coordinates of the target point.| 2533 2534**Error codes** 2535 2536For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2537 2538| ID| Error Message | 2539| -------- | ---------------------------------------- | 2540| 17000002 | if the async function was not called with await. | 2541 2542**Example** 2543 2544```ts 2545import { Driver } from '@ohos.UiTest'; 2546async function demo() { 2547 let driver: Driver = Driver.create(); 2548 await driver.mouseMoveTo({x:100, y:100}) 2549} 2550``` 2551 2552### createUIEventObserver<sup>10+</sup> 2553 2554createUIEventObserver(): UIEventObserver; 2555 2556Creates a UI event listener. 2557 2558**System capability**: SystemCapability.Test.UiTest 2559 2560**Return value** 2561 2562| Type | Description | 2563| ------------------------------------ | ------------------------------------- | 2564|[UIEventObserver](#uieventobserver10) | UI event listener.| 2565 2566**Error codes** 2567 2568For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2569 2570| ID| Error Message | 2571| -------- | ---------------------------------------- | 2572| 17000002 | if the async function was not called with await. | 2573 2574**Example** 2575 2576```ts 2577import { Driver, UIEventObserver } from '@ohos.UiTest'; 2578async function demo() { 2579 let driver: Driver = Driver.create(); 2580 let observer: UIEventObserver = await driver.createUIEventObserver() 2581} 2582``` 2583 2584### mouseScroll<sup>11+</sup> 2585 2586mouseScroll(p: Point, down: boolean, d: number, key1?: number, key2?: number, speed?: number): Promise\<void> 2587 2588Injects a mouse scroll action at the specified coordinates. You can specify the key or key combination to work with the action, as well as the scroll speed. 2589 2590**System capability**: SystemCapability.Test.UiTest 2591 2592**Parameters** 2593 2594| Name| Type | Mandatory| Description | 2595| ------ | ---------------- | ---- | ------------------------------------------------------------ | 2596| p | [Point](#point9) | Yes | Coordinates of the mouse click. | 2597| down | boolean | Yes | Whether the mouse wheel scrolls downward.<br>**true**: The mouse wheel scrolls downward.<br>**false**: The mouse wheel scrolls upward. | 2598| d | number | Yes | Number of cells by which the mouse wheel scrolls. Scrolling by one cell means a 120-pixel offset of the target point. | 2599| key1 | number | No | The first key value. | 2600| key2 | number | No | The second key value. | 2601| speed | number | No | Scroll speed of the mouse wheel, in cells/second.<br>Value range: 1 to 500<br>If the value is not within the range, the default value **20** will be used.| 2602 2603**Error codes** 2604 2605For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2606 2607| ID| Error Message | 2608| -------- | ------------------------------------------------ | 2609| 17000002 | if the async function was not called with await. | 2610 2611**Example** 2612 2613```ts 2614import { Driver } from '@ohos.UiTest'; 2615async function demo() { 2616 let driver: Driver = Driver.create(); 2617 await driver.mouseScroll({x:360, y:640}, true, 30, 2072,20) 2618} 2619``` 2620 2621### mouseDoubleClick<sup>11+</sup> 2622 2623mouseDoubleClick(p: Point, btnId: MouseButton, key1?: number, key2?: number): Promise\<void> 2624 2625Injects a double-click action at the specified coordinates, with the optional key or key combination. For example, if the value of **key** is **2072**, the **Ctrl** button is pressed with the double-click. 2626 2627**System capability**: SystemCapability.Test.UiTest 2628 2629**Parameters** 2630 2631| Name| Type | Mandatory| Description | 2632| ------ | ----------------------------- | ---- | ------------------- | 2633| p | [Point](#point9) | Yes | Coordinates of the double-click. | 2634| btnId | [MouseButton](#mousebutton10) | Yes | Mouse button pressed. | 2635| key1 | number | No | The first key value.| 2636| key2 | number | No | The second key value.| 2637 2638**Error codes** 2639 2640For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2641 2642| ID| Error Message | 2643| -------- | ------------------------------------------------ | 2644| 17000002 | if the async function was not called with await. | 2645 2646**Example** 2647 2648```ts 2649import { Driver,MouseButton } from '@ohos.UiTest'; 2650async function demo() { 2651 let driver: Driver = Driver.create(); 2652 await driver.mouseDoubleClick({x:248, y:194}, MouseButton.MOUSE_BUTTON_LEFT, 2072); 2653} 2654``` 2655 2656### mouseLongClick<sup>11+</sup> 2657 2658mouseLongClick(p: Point, btnId: MouseButton, key1?: number, key2?: number): Promise\<void> 2659 2660Injects a long-click action of the mouse device at the specified coordinates, with the optional key or key combination. For example, if the value of **Key** is **2072**, the **Ctrl** button is long-clicked with the mouse device. 2661 2662**System capability**: SystemCapability.Test.UiTest 2663 2664**Parameters** 2665 2666| Name| Type | Mandatory| Description | 2667| ------ | ----------------------------- | ---- | ------------------- | 2668| p | [Point](#point9) | Yes | Coordinates of the long-click of the mouse device. | 2669| btnId | [MouseButton](#mousebutton10) | Yes | Mouse button pressed. | 2670| key1 | number | No | The first key value.| 2671| key2 | number | No | The second key value.| 2672 2673**Error codes** 2674 2675For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2676 2677| ID| Error Message | 2678| -------- | ------------------------------------------------ | 2679| 17000002 | if the async function was not called with await. | 2680 2681**Example** 2682 2683```ts 2684import { Driver,MouseButton } from '@ohos.UiTest'; 2685async function demo() { 2686 let driver:Driver = Driver.create(); 2687 await driver.mouseLongClick({x:248, y:194}, MouseButton.MOUSE_BUTTON_LEFT, 2072); 2688} 2689``` 2690 2691### mouseMoveWithTrack<sup>11+</sup> 2692 2693mouseMoveWithTrack(from: Point, to: Point, speed?: number): Promise\<void> 2694 2695Moves the mouse pointer from the start point to the end point. 2696 2697**System capability**: SystemCapability.Test.UiTest 2698 2699**Parameters** 2700 2701| Name| Type | Mandatory| Description | 2702| ------ | ---------------- | ---- | ------------------------------------------------------------ | 2703| from | [Point](#point9) | Yes | Coordinates of the start point. | 2704| to | [Point](#point9) | Yes | Coordinates of the end point. | 2705| 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.| 2706 2707**Error codes** 2708 2709For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2710 2711| ID| Error Message | 2712| -------- | ------------------------------------------------ | 2713| 17000002 | if the async function was not called with await. | 2714 2715**Example** 2716 2717```ts 2718import { Driver } from '@ohos.UiTest'; 2719async function demo() { 2720 let driver: Driver = Driver.create(); 2721 await driver.mouseMoveWithTrack(100,100,200,200,600); 2722} 2723``` 2724 2725### mouseDrag<sup>11+</sup> 2726 2727mouseDrag(from: Point, to: Point, speed?: number): Promise\<void> 2728 2729Drags the mouse pointer from the start point to the end point. 2730 2731**System capability**: SystemCapability.Test.UiTest 2732 2733**Parameters** 2734 2735| Name| Type | Mandatory| Description | 2736| ------ | ---------------- | ---- | ------------------------------------------------------------ | 2737| from | [Point](#point9) | Yes | Coordinates of the start point. | 2738| to | [Point](#point9) | Yes | Coordinates of the end point. | 2739| 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.| 2740 2741**Error codes** 2742 2743For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2744 2745| ID| Error Message | 2746| -------- | ------------------------------------------------ | 2747| 17000002 | if the async function was not called with await. | 2748 2749**Example** 2750 2751```ts 2752import { Driver } from '@ohos.UiTest'; 2753async function demo() { 2754 let driver: Driver = Driver.create(); 2755 await driver.mouseDrag(100,100,200,200,600); 2756} 2757``` 2758 2759### inputText<sup>11+</sup> 2760 2761inputText(p: Point, text: string): Promise\<void> 2762 2763Enters text at the specified point. 2764 2765**System capability**: SystemCapability.Test.UiTest 2766 2767**Parameters** 2768 2769| Name| Type | Mandatory| Description | 2770| ------ | ---------------- | ---- | ------------------ | 2771| p | [Point](#point9) | Yes | Coordinates of the target point.| 2772| text | string | Yes | Text to enter. | 2773 2774**Error codes** 2775 2776For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2777 2778| ID| Error Message | 2779| -------- | ------------------------------------------------ | 2780| 17000002 | if the async function was not called with await. | 2781 2782**Example** 2783 2784```ts 2785import { Component, Driver, ON } from '@ohos.UiTest'; 2786async function demo() { 2787 let driver:Driver = Driver.create(); 2788 let text: Component = await driver.findComponent(ON.type('TextInput')); 2789 let point = await text.getBoundsCenter(); 2790 await driver.inputText(point, '123'); 2791} 2792``` 2793 2794## PointerMatrix<sup>9+</sup> 2795 2796Implements a **PointerMatrix** object that stores coordinates and behaviors of each action of each finger in a multi-touch operation. 2797 2798### create<sup>9+</sup> 2799 2800static create(fingers: number, steps: number): PointerMatrix 2801 2802Creates a **PointerMatrix** object and returns the object created. This API is a static API. 2803 2804**System capability**: SystemCapability.Test.UiTest 2805 2806**Parameters** 2807 2808| Name | Type | Mandatory| Description | 2809| ------- | ------ | ---- | ------------------------------------------ | 2810| fingers | number | Yes | Number of fingers in the multi-touch operation. Value range: [1,10].| 2811| steps | number | Yes | Number of steps operated by each finger. Value range: [1,1000].| 2812 2813**Return value** 2814 2815| Type | Description | 2816| -------------------------------- | ----------------------------- | 2817| [PointerMatrix](#pointermatrix9) | **PointerMatrix** object created.| 2818 2819**Example** 2820 2821```ts 2822import { PointerMatrix } from '@ohos.UiTest'; 2823async function demo() { 2824 let pointerMatrix: PointerMatrix = PointerMatrix.create(2,3); 2825} 2826``` 2827 2828### setPoint<sup>9+</sup> 2829 2830setPoint(finger: number, step: number, point: Point): void 2831 2832Sets the coordinates for the action corresponding to the specified finger and step in the **PointerMatrix** object. 2833 2834**System capability**: SystemCapability.Test.UiTest 2835 2836**Parameters** 2837 2838| Name| Type | Mandatory| Description | 2839| ------ | ---------------- | ---- | ---------------- | 2840| finger | number | Yes | Sequence number of the finger. | 2841| step | number | Yes | Sequence number of the step. | 2842| point | [Point](#point9) | Yes | Coordinates of the action.| 2843 2844**Example** 2845 2846```ts 2847import { PointerMatrix } from '@ohos.UiTest'; 2848async function demo() { 2849 let pointers: PointerMatrix = PointerMatrix.create(2,3); 2850 pointers.setPoint(0,0,{x:230,y:480}); 2851 pointers.setPoint(0,1,{x:250,y:380}); 2852 pointers.setPoint(0,2,{x:270,y:280}); 2853 pointers.setPoint(1,0,{x:230,y:680}); 2854 pointers.setPoint(1,1,{x:240,y:580}); 2855 pointers.setPoint(1,2,{x:250,y:480}); 2856} 2857``` 2858 2859## UiWindow<sup>9+</sup> 2860 2861The **UiWindow** class represents a window on the UI and provides APIs for obtaining window attributes, dragging a window, and adjusting the window size. 2862 2863All APIs provided in this class use a promise to return the result and must be invoked using **await**. 2864 2865### getBundleName<sup>9+</sup> 2866 2867getBundleName(): Promise\<string> 2868 2869Obtains the bundle name of the application to which this window belongs. 2870 2871**System capability**: SystemCapability.Test.UiTest 2872 2873**Return value** 2874 2875| Type | Description | 2876| ---------------- | ----------------------------------------- | 2877| Promise\<string> | Promise used to return the bundle name.| 2878 2879**Error codes** 2880 2881For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2882 2883| ID| Error Message | 2884| -------- | ---------------------------------------- | 2885| 17000002 | if the async function was not called with await. | 2886| 17000004 | if the window is invisible or destroyed. | 2887 2888**Example** 2889 2890```ts 2891import { Driver, UiWindow } from '@ohos.UiTest'; 2892 2893async function demo() { 2894 let driver: Driver = Driver.create(); 2895 let window: UiWindow = await driver.findWindow({actived: true}); 2896 let name: string = await window.getBundleName(); 2897} 2898``` 2899 2900### getBounds<sup>9+</sup> 2901 2902getBounds(): Promise\<Rect> 2903 2904Obtains the bounds information of this window. 2905 2906**System capability**: SystemCapability.Test.UiTest 2907 2908**Return value** 2909 2910| Type | Description | 2911| ------------------------ | --------------------------------- | 2912| Promise\<[Rect](#rect9)> | Promise used to return the bounds information of the window.| 2913 2914**Error codes** 2915 2916For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2917 2918| ID| Error Message | 2919| -------- | ---------------------------------------- | 2920| 17000002 | if the async function was not called with await. | 2921| 17000004 | if the window is invisible or destroyed. | 2922 2923**Example** 2924 2925```ts 2926import { Driver, UiWindow } from '@ohos.UiTest'; 2927async function demo() { 2928 let driver: Driver = Driver.create(); 2929 let window: UiWindow = await driver.findWindow({actived: true}); 2930 let rect = await window.getBounds(); 2931} 2932``` 2933 2934### getTitle<sup>9+</sup> 2935 2936getTitle(): Promise\<string> 2937 2938Obtains the title of this window. 2939 2940**System capability**: SystemCapability.Test.UiTest 2941 2942**Return value** 2943 2944| Type | Description | 2945| ---------------- | --------------------------------- | 2946| Promise\<string> | Promise used to return the title of the window.| 2947 2948**Error codes** 2949 2950For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2951 2952| ID| Error Message | 2953| -------- | ---------------------------------------- | 2954| 17000002 | if the async function was not called with await. | 2955| 17000004 | if the window is invisible or destroyed. | 2956 2957**Example** 2958 2959```ts 2960import { Driver, UiWindow } from '@ohos.UiTest'; 2961async function demo() { 2962 let driver: Driver = Driver.create(); 2963 let window: UiWindow = await driver.findWindow({actived: true}); 2964 let rect = await window.getTitle(); 2965} 2966``` 2967 2968### getWindowMode<sup>9+</sup> 2969 2970getWindowMode(): Promise\<WindowMode> 2971 2972Obtains the window mode of this window. 2973 2974**System capability**: SystemCapability.Test.UiTest 2975 2976**Return value** 2977 2978| Type | Description | 2979| ------------------------------------ | ------------------------------------- | 2980| Promise\<[WindowMode](#windowmode9)> | Promise used to return the window mode of the window.| 2981 2982**Error codes** 2983 2984For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 2985 2986| ID| Error Message | 2987| -------- | ---------------------------------------- | 2988| 17000002 | if the async function was not called with await. | 2989| 17000004 | if the window is invisible or destroyed. | 2990 2991**Example** 2992 2993```ts 2994import { Driver, UiWindow } from '@ohos.UiTest'; 2995async function demo() { 2996 let driver: Driver = Driver.create(); 2997 let window: UiWindow = await driver.findWindow({actived: true}); 2998 let mode = await window.getWindowMode(); 2999} 3000``` 3001 3002### isFocused<sup>9+</sup> 3003 3004isFocused(): Promise\<boolean> 3005 3006Checks whether this window is in focused state. 3007 3008**System capability**: SystemCapability.Test.UiTest 3009 3010**Return value** 3011 3012| Type | Description | 3013| ----------------- | ------------------------------------------------------------ | 3014| Promise\<boolean> | Promise used to return the result. The value **true** means that the window is in focused state, and **false** means the opposite.| 3015 3016**Error codes** 3017 3018For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3019 3020| ID| Error Message | 3021| -------- | ---------------------------------------- | 3022| 17000002 | if the async function was not called with await. | 3023| 17000004 | if the window is invisible or destroyed. | 3024 3025**Example** 3026 3027```ts 3028import { Driver, UiWindow } from '@ohos.UiTest'; 3029async function demo() { 3030 let driver: Driver = Driver.create(); 3031 let window: UiWindow = await driver.findWindow({actived: true}); 3032 let focused = await window.isFocused(); 3033} 3034``` 3035 3036### isActived<sup>(deprecated)</sup> 3037 3038isActived(): Promise\<boolean> 3039 3040Checks whether this window is active. 3041 3042This API is supported since API version 9 and deprecated since API version 11. You are advised to use [isActive<sup>11+</sup>](#isactive11) instead. 3043 3044**System capability**: SystemCapability.Test.UiTest 3045 3046**Return value** 3047 3048| Type | Description | 3049| ----------------- | ------------------------------------------------------------ | 3050| Promise\<boolean> | Promise used to return the result. The value **true** means that the window is active, and **false** means the opposite.| 3051 3052**Error codes** 3053 3054For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3055 3056| ID| Error Message | 3057| -------- | ---------------------------------------- | 3058| 17000002 | if the async function was not called with await. | 3059| 17000004 | if the window is invisible or destroyed. | 3060 3061**Example** 3062 3063```ts 3064import { Driver, UiWindow } from '@ohos.UiTest'; 3065async function demo() { 3066 let driver: Driver = Driver.create(); 3067 let window: UiWindow = await driver.findWindow({actived: true}); 3068 let focused = await window.isActived(); 3069} 3070``` 3071 3072### focus<sup>9+</sup> 3073 3074focus(): Promise\<void> 3075 3076Moves the focus to this window. 3077 3078**System capability**: SystemCapability.Test.UiTest 3079 3080**Error codes** 3081 3082For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3083 3084| ID| Error Message | 3085| -------- | ---------------------------------------- | 3086| 17000002 | if the async function was not called with await. | 3087| 17000004 | if the window is invisible or destroyed. | 3088 3089**Example** 3090 3091```ts 3092import { Driver, UiWindow } from '@ohos.UiTest'; 3093async function demo() { 3094 let driver: Driver = Driver.create(); 3095 let window: UiWindow = await driver.findWindow({actived: true}); 3096 await window.focus(); 3097} 3098``` 3099 3100### moveTo<sup>9+</sup> 3101 3102moveTo(x: number, y: number): Promise\<void> 3103 3104Moves this window to the target point. This API is applicable to moveable windows. 3105 3106**System capability**: SystemCapability.Test.UiTest 3107 3108**Parameters** 3109 3110| Name| Type | Mandatory| Description | 3111| ------ | ------ | ---- | -------------------------------------- | 3112| x | number | Yes | X coordinate of the target point.| 3113| y | number | Yes | Y coordinate of the target point.| 3114 3115**Error codes** 3116 3117For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3118 3119| ID| Error Message | 3120| -------- | ---------------------------------------- | 3121| 17000002 | if the async function was not called with await. | 3122| 17000004 | if the window is invisible or destroyed. | 3123| 17000005 | if the action is not supported on this window. | 3124 3125**Example** 3126 3127```ts 3128import { Driver, UiWindow } from '@ohos.UiTest'; 3129async function demo() { 3130 let driver: Driver = Driver.create(); 3131 let window: UiWindow = await driver.findWindow({actived: true}); 3132 await window.moveTo(100, 100); 3133} 3134``` 3135 3136### resize<sup>9+</sup> 3137 3138resize(wide: number, height: number, direction: ResizeDirection): Promise\<void> 3139 3140Resizes this window based on the specified width, height, and resize direction. This API is applicable to resizable windows. 3141 3142**System capability**: SystemCapability.Test.UiTest 3143 3144**Parameters** 3145 3146| Name | Type | Mandatory| Description | 3147| --------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 3148| wide | number | Yes | Target width. | 3149| height | number | Yes | Target height. | 3150| direction | [ResizeDirection](#resizedirection9) | Yes | Resize direction.| 3151 3152**Error codes** 3153 3154For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3155 3156| ID| Error Message | 3157| -------- | ---------------------------------------- | 3158| 17000002 | if the async function was not called with await. | 3159| 17000004 | if the window is invisible or destroyed. | 3160| 17000005 | if the action is not supported on this window. | 3161 3162**Example** 3163 3164```ts 3165import { Driver, ResizeDirection, UiWindow } from '@ohos.UiTest'; 3166async function demo() { 3167 let driver: Driver = Driver.create(); 3168 let window: UiWindow = await driver.findWindow({actived: true}); 3169 await window.resize(100, 100, ResizeDirection.LEFT); 3170} 3171``` 3172 3173### split<sup>9+</sup> 3174 3175split(): Promise\<void> 3176 3177Switches the window to split-screen mode. This API is applicable to windows that support screen splitting. 3178 3179**System capability**: SystemCapability.Test.UiTest 3180 3181**Error codes** 3182 3183For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3184 3185| ID| Error Message | 3186| -------- | ---------------------------------------- | 3187| 17000002 | if the async function was not called with await. | 3188| 17000004 | if the window is invisible or destroyed. | 3189| 17000005 | if the action is not supported on this window. | 3190 3191**Example** 3192 3193```ts 3194import { Driver, UiWindow } from '@ohos.UiTest'; 3195async function demo() { 3196 let driver: Driver = Driver.create(); 3197 let window: UiWindow = await driver.findWindow({actived: true}); 3198 await window.split(); 3199} 3200``` 3201 3202### maximize<sup>9+</sup> 3203 3204maximize(): Promise\<void> 3205 3206Maximizes this window. This API is applicable to windows that can be maximized. 3207 3208**System capability**: SystemCapability.Test.UiTest 3209 3210**Error codes** 3211 3212For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3213 3214| ID| Error Message | 3215| -------- | ---------------------------------------- | 3216| 17000002 | if the async function was not called with await. | 3217| 17000004 | if the window is invisible or destroyed. | 3218| 17000005 | if the action is not supported on this window. | 3219 3220**Example** 3221 3222```ts 3223import { Driver, UiWindow } from '@ohos.UiTest'; 3224async function demo() { 3225 let driver: Driver = Driver.create(); 3226 let window: UiWindow = await driver.findWindow({actived: true}); 3227 await window.maximize(); 3228} 3229``` 3230 3231### minimize<sup>9+</sup> 3232 3233minimize(): Promise\<void> 3234 3235Minimizes this window. This API is applicable to windows that can be minimized. 3236 3237**System capability**: SystemCapability.Test.UiTest 3238 3239**Error codes** 3240 3241For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3242 3243| ID| Error Message | 3244| -------- | ---------------------------------------- | 3245| 17000002 | if the async function was not called with await. | 3246| 17000004 | if the window is invisible or destroyed. | 3247| 17000005 | if the action is not supported on this window. | 3248 3249**Example** 3250 3251```ts 3252import { Driver, UiWindow } from '@ohos.UiTest'; 3253async function demo() { 3254 let driver: Driver = Driver.create(); 3255 let window: UiWindow = await driver.findWindow({actived: true}); 3256 await window.minimize(); 3257} 3258``` 3259 3260### resume<sup>9+</sup> 3261 3262resume(): Promise\<void> 3263 3264Restores this window to the previous window mode. 3265 3266**System capability**: SystemCapability.Test.UiTest 3267 3268**Error codes** 3269 3270For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3271 3272| ID| Error Message | 3273| -------- | ---------------------------------------- | 3274| 17000002 | if the async function was not called with await. | 3275| 17000004 | if the window is invisible or destroyed. | 3276| 17000005 | if the action is not supported on this window. | 3277 3278**Example** 3279 3280```ts 3281import { Driver, UiWindow } from '@ohos.UiTest'; 3282async function demo() { 3283 let driver: Driver = Driver.create(); 3284 let window: UiWindow = await driver.findWindow({actived: true}); 3285 await window.resume(); 3286} 3287``` 3288 3289### close<sup>9+</sup> 3290 3291close(): Promise\<void> 3292 3293Closes this window. 3294 3295**System capability**: SystemCapability.Test.UiTest 3296 3297**Error codes** 3298 3299For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3300 3301| ID| Error Message | 3302| -------- | ---------------------------------------- | 3303| 17000002 | if the async function was not called with await. | 3304| 17000004 | if the window is invisible or destroyed. | 3305| 17000005 | if the action is not supported on this window. | 3306 3307**Example** 3308 3309```ts 3310import { Driver, UiWindow } from '@ohos.UiTest'; 3311async function demo() { 3312 let driver:Driver = Driver.create(); 3313 let window: UiWindow = await driver.findWindow({actived: true}); 3314 await window.close(); 3315} 3316``` 3317 3318### isActive<sup>11+</sup> 3319 3320isActive(): Promise\<boolean> 3321 3322Checks whether this window is active. 3323 3324**System capability**: SystemCapability.Test.UiTest 3325 3326**Return value** 3327 3328| Type | Description | 3329| ----------------- | ------------------------------------------------------------ | 3330| Promise\<boolean> | Promise used to return the result. The value **true** means that the window is active, and **false** means the opposite.| 3331 3332**Error codes** 3333 3334For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 3335 3336| ID| Error Message | 3337| -------- | ------------------------------------------------ | 3338| 17000002 | if the async function was not called with await. | 3339| 17000004 | if the window is invisible or destroyed. | 3340 3341**Example** 3342 3343```ts 3344import { Driver, UiWindow } from '@ohos.UiTest'; 3345async function demo() { 3346 let driver: Driver = Driver.create(); 3347 let window: UiWindow = await driver.findWindow({active: true}); 3348 let focused = await window.isActive(); 3349} 3350``` 3351 3352## UIEventObserver<sup>10+</sup> 3353 3354UI event listener. 3355 3356### once('toastShow') 3357 3358once(type: 'toastShow', callback: Callback\<UIElementInfo>): void; 3359 3360Subscribes to events of the toast component. This API uses a callback to return the result. 3361 3362**System capability**: SystemCapability.Test.UiTest 3363 3364**Parameters** 3365 3366| Name | Type | Mandatory| Description | 3367| -------- | -------------------------------------------- | ---- | --------------------------------- | 3368| type | string | Yes | Event type. The value is fixed at **'toastShow'**.| 3369| callback | Callback\<[UIElementInfo](#uielementinfo10)> | Yes | Callback used to return the result. | 3370 3371**Example** 3372 3373```ts 3374import { Driver, UIElementInfo, UIEventObserver } from '@ohos.UiTest'; 3375async function demo() { 3376 let driver: Driver = Driver.create(); 3377 let observer: UIEventObserver = await driver.createUIEventObserver() 3378 let callback = (UIElementInfo: UIElementInfo)=>{ 3379 console.info(UIElementInfo.bundleName) 3380 console.info(UIElementInfo.text) 3381 console.info(UIElementInfo.type) 3382 } 3383 observer.once('toastShow', callback) 3384} 3385``` 3386 3387### once('dialogShow') 3388 3389once(type: 'dialogShow', callback: Callback\<UIElementInfo>): void; 3390 3391Subscribes to events of the dialog component. This API uses a callback to return the result. 3392 3393**System capability**: SystemCapability.Test.UiTest 3394 3395**Parameters** 3396 3397| Name | Type | Mandatory| Description | 3398| -------- | -------------------------------------------- | ---- | ---------------------------------- | 3399| type | string | Yes | Event type. The value is fixed at **'dialogShow'**.| 3400| callback | Callback\<[UIElementInfo](#uielementinfo10)> | Yes | Callback used to return the result. | 3401 3402**Example** 3403 3404```ts 3405import { Driver, UIElementInfo, UIEventObserver } from '@ohos.UiTest'; 3406async function demo() { 3407 let driver: Driver = Driver.create(); 3408 let observer: UIEventObserver = await driver.createUIEventObserver() 3409 let callback = (UIElementInfo: UIElementInfo)=>{ 3410 console.info(UIElementInfo.bundleName) 3411 console.info(UIElementInfo.text) 3412 console.info(UIElementInfo.type) 3413 } 3414 observer.once('dialogShow', callback) 3415} 3416``` 3417 3418## By<sup>(deprecated)</sup> 3419 3420The UiTest framework provides a wide range of UI component feature description APIs in the **By** class to filter and match components. 3421 3422The APIs provided by the **By** class exhibit the following features: 3423 3424- 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. 3425- Provide multiple match patterns for component attributes. 3426- 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. 3427 3428All 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. 3429 3430This class is deprecated since API version 9. You are advised to use [On<sup>9+</sup>](#on9) instead. 3431 3432```ts 3433import { BY } from '@ohos.UiTest'; 3434BY.text('123').type('Button'); 3435``` 3436 3437### text<sup>(deprecated)</sup> 3438 3439text(txt: string, pattern?: MatchPattern): By 3440 3441Specifies the text attribute of the target component. Multiple match patterns are supported. 3442 3443This API is deprecated since API version 9. You are advised to use [text<sup>9+</sup>](#text9) instead. 3444 3445**System capability**: SystemCapability.Test.UiTest 3446 3447**Parameters** 3448 3449| Name | Type | Mandatory| Description | 3450| ------- | ----------------------------- | ---- | --------------------------------------------------- | 3451| txt | string | Yes | Component text, used to match the target component. | 3452| pattern | [MatchPattern](#matchpattern) | No | Match pattern. The default value is [EQUALS](#matchpattern).| 3453 3454**Return value** 3455 3456| Type | Description | 3457| ------------------- | ---------------------------------- | 3458| [By](#bydeprecated) | **By** object that matches the text attribute of the target component.| 3459 3460**Example** 3461 3462```ts 3463import { BY, By } from '@ohos.UiTest'; 3464let by: By = BY.text('123'); // Use the static constructor BY to create a By object and specify the text attribute of the target component. 3465``` 3466 3467 3468### key<sup>(deprecated)</sup> 3469 3470key(key: string): By 3471 3472Specifies the key attribute of the target component. 3473 3474This API is deprecated since API version 9. You are advised to use [id<sup>9+</sup>](#id9) instead. 3475 3476**System capability**: SystemCapability.Test.UiTest 3477 3478**Parameters** 3479 3480| Name| Type | Mandatory| Description | 3481| ------ | ------ | ---- | ----------------- | 3482| key | string | Yes | Component key.| 3483 3484**Return value** 3485 3486| Type | Description | 3487| ------------------- | ----------------------------------- | 3488| [By](#bydeprecated) | **By** object that matches the key attribute of the target component.| 3489 3490**Example** 3491 3492```ts 3493import { By, BY } from '@ohos.UiTest'; 3494let by: By = BY.key('123'); // Use the static constructor BY to create a By object and specify the key attribute of the target component. 3495``` 3496 3497 3498### id<sup>(deprecated)</sup> 3499 3500id(id: number): By 3501 3502Specifies the ID attribute of the target component. 3503 3504This API is deprecated since API version 9. 3505 3506**System capability**: SystemCapability.Test.UiTest 3507 3508**Parameters** 3509 3510| Name| Type | Mandatory| Description | 3511| ------ | ------ | ---- | ---------------- | 3512| id | number | Yes | Component ID.| 3513 3514**Return value** 3515 3516| Type | Description | 3517| ------------------- | -------------------------------- | 3518| [By](#bydeprecated) | **By** object that matches the ID attribute of the target component.| 3519 3520**Example** 3521 3522```ts 3523import { By, BY } from '@ohos.UiTest'; 3524let by: By = BY.id(123); // Use the static constructor BY to create a By object and specify the id attribute of the target component. 3525``` 3526 3527 3528### type<sup>(deprecated)</sup> 3529 3530type(tp: string): By 3531 3532Specifies the type attribute of the target component. 3533 3534This API is deprecated since API version 9. You are advised to use [type<sup>9+</sup>](#type9) instead. 3535 3536**System capability**: SystemCapability.Test.UiTest 3537 3538**Parameters** 3539 3540| Name| Type | Mandatory| Description | 3541| ------ | ------ | ---- | -------------- | 3542| tp | string | Yes | Component type.| 3543 3544**Return value** 3545 3546| Type | Description | 3547| ------------------- | ---------------------------------------- | 3548| [By](#bydeprecated) | **By** object that matches the type attribute of the target component.| 3549 3550**Example** 3551 3552```ts 3553import { By, BY } from '@ohos.UiTest'; 3554let by: By = BY.type('Button'); // Use the static constructor BY to create a By object and specify the type attribute of the target component. 3555``` 3556 3557 3558### clickable<sup>(deprecated)</sup> 3559 3560clickable(b?: boolean): By 3561 3562Specifies the clickable attribute of the target component. 3563 3564This API is deprecated since API version 9. You are advised to use [clickable<sup>9+</sup>](#clickable9) instead. 3565 3566**System capability**: SystemCapability.Test.UiTest 3567 3568**Parameters** 3569 3570| Name| Type | Mandatory| Description | 3571| ------ | ------- | ---- | ------------------------------------------------------------ | 3572| b | boolean | No | Clickable status of the target component.<br>**true**: clickable.<br>**false**: not clickable.<br> Default value: **true**| 3573 3574**Return value** 3575 3576| Type | Description | 3577| ------------------- | ------------------------------------------ | 3578| [By](#bydeprecated) | **By** object that matches the clickable attribute of the target component.| 3579 3580**Example** 3581 3582```ts 3583import { By, BY } from '@ohos.UiTest'; 3584let by: By = BY.clickable(true); // Use the static constructor BY to create a By object and specify the clickable attribute of the target component. 3585``` 3586 3587 3588### scrollable<sup>(deprecated)</sup> 3589 3590scrollable(b?: boolean): By 3591 3592Specifies the scrollable attribute of the target component. 3593 3594This API is deprecated since API version 9. You are advised to use [scrollable<sup>9+</sup>](#scrollable9) instead. 3595 3596**System capability**: SystemCapability.Test.UiTest 3597 3598**Parameters** 3599 3600| Name| Type | Mandatory| Description | 3601| ------ | ------- | ---- | ----------------------------------------------------------- | 3602| b | boolean | No | Scrollable status of the target component.<br>**true**: scrollable.<br>**false**: not scrollable.<br> Default value: **true**| 3603 3604**Return value** 3605 3606| Type | Description | 3607| ------------------- | ------------------------------------------ | 3608| [By](#bydeprecated) | **By** object that matches the scrollable attribute of the target component.| 3609 3610**Example** 3611 3612```ts 3613import { By, BY } from '@ohos.UiTest'; 3614let by: By = BY.scrollable(true); // Use the static constructor BY to create a By object and specify the scrollable attribute of the target component. 3615``` 3616 3617### enabled<sup>(deprecated)</sup> 3618 3619enabled(b?: boolean): By 3620 3621Specifies the enabled attribute of the target component. 3622 3623This API is deprecated since API version 9. You are advised to use [enabled<sup>9+</sup>](#enabled9) instead. 3624 3625**System capability**: SystemCapability.Test.UiTest 3626 3627**Parameters** 3628 3629| Name| Type | Mandatory| Description | 3630| ------ | ------- | ---- | --------------------------------------------------------- | 3631| b | boolean | No | Enabled status of the target component.<br>**true**: enabled.<br>**false**: not enabled.<br> Default value: **true**| 3632 3633**Return value** 3634 3635| Type | Description | 3636| ------------------- | ---------------------------------------- | 3637| [By](#bydeprecated) | **By** object that matches the enabled attribute of the target component.| 3638 3639**Example** 3640 3641```ts 3642import { By, BY } from '@ohos.UiTest'; 3643let by: By = BY.enabled(true); // Use the static constructor BY to create a By object and specify the enabled attribute of the target component. 3644``` 3645 3646### focused<sup>(deprecated)</sup> 3647 3648focused(b?: boolean): By 3649 3650Specifies the focused attribute of the target component. 3651 3652This API is deprecated since API version 9. You are advised to use [focused<sup>9+</sup>](#focused9) instead. 3653 3654**System capability**: SystemCapability.Test.UiTest 3655 3656**Parameters** 3657 3658| Name| Type | Mandatory| Description | 3659| ------ | ------- | ---- | ----------------------------------------------------- | 3660| b | boolean | No | Focused status of the target component.<br>**true**: focused.<br>**false**: not focused.<br> Default value: **true**| 3661 3662**Return value** 3663 3664| Type | Description | 3665| ------------------- | ---------------------------------------- | 3666| [By](#bydeprecated) | **By** object that matches the focused attribute of the target component.| 3667 3668**Example** 3669 3670```ts 3671import { By, BY } from '@ohos.UiTest'; 3672let by: By = BY.focused(true); // Use the static constructor BY to create a By object and specify the focused attribute of the target component. 3673``` 3674 3675### selected<sup>(deprecated)</sup> 3676 3677selected(b?: boolean): By 3678 3679Specifies the selected status of the target component. 3680 3681This API is deprecated since API version 9. You are advised to use [selected<sup>9+</sup>](#selected9) instead. 3682 3683**System capability**: SystemCapability.Test.UiTest 3684 3685**Parameters** 3686 3687| Name| Type | Mandatory| Description | 3688| ------ | ------- | ---- | ------------------------------------------------------------ | 3689| b | boolean | No | Selected status of the target component.<br>**true**: selected.<br>**false**: not selected.<br> Default value: **true**| 3690 3691**Return value** 3692 3693| Type | Description | 3694| ------------------- | ------------------------------------------ | 3695| [By](#bydeprecated) | **By** object that matches the selected attribute of the target component.| 3696 3697**Example** 3698 3699```ts 3700import { By, BY } from '@ohos.UiTest'; 3701let by: By = BY.selected(true); // Use the static constructor BY to create a By object and specify the selected attribute of the target component. 3702``` 3703 3704### isBefore<sup>(deprecated)</sup> 3705 3706isBefore(by: By): By 3707 3708Specifies that the target component is located before the given attribute component. 3709 3710This API is deprecated since API version 9. You are advised to use [isBefore<sup>9+</sup>](#isbefore9) instead. 3711 3712**System capability**: SystemCapability.Test.UiTest 3713 3714**Parameters** 3715 3716| Name| Type | Mandatory| Description | 3717| ------ | ------------------- | ---- | ---------------- | 3718| by | [By](#bydeprecated) | Yes | Information about the attribute component.| 3719 3720**Return value** 3721 3722| Type | Description | 3723| ------------------- | ---------------------------------------------------- | 3724| [By](#bydeprecated) | **By** object.| 3725 3726**Example** 3727 3728```ts 3729import { By, BY } from '@ohos.UiTest'; 3730 3731// Use the static constructor BY to create a by object and specify that the target component is located before the given attribute component. 3732let by: By = BY.type('Button').isBefore(BY.text('123')); // Search for the first <Button> component located before the component whose text is 123. 3733``` 3734 3735### isAfter<sup>(deprecated)</sup> 3736 3737isAfter(by: By): By 3738 3739Specifies that the target component is located after the given attribute component. 3740 3741This API is deprecated since API version 9. You are advised to use [isAfter<sup>9+</sup>](#isafter9) instead. 3742 3743**System capability**: SystemCapability.Test.UiTest 3744 3745**Parameters** 3746 3747| Name| Type | Mandatory| Description | 3748| ------ | ------------------- | ---- | ---------------- | 3749| by | [By](#bydeprecated) | Yes | Information about the attribute component.| 3750 3751**Return value** 3752 3753| Type | Description | 3754| ------------------- | ---------------------------------------------------- | 3755| [By](#bydeprecated) | **By** object.| 3756 3757**Example** 3758 3759```ts 3760import { By, BY } from '@ohos.UiTest'; 3761 3762// Use the static constructor BY to create a by object and specify that the target component is located after the given attribute component. 3763let by: By = BY.type('Text').isAfter(BY.text('123')); // Search for the first <Text> component located after the component whose text is 123. 3764``` 3765 3766## UiComponent<sup>(deprecated)</sup> 3767 3768In **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. 3769All APIs provided in this class use a promise to return the result and must be invoked using **await**. 3770 3771This class is deprecated since API version 9. You are advised to use [Component<sup>9+</sup>](#component9) instead. 3772 3773### click<sup>(deprecated)</sup> 3774 3775click(): Promise\<void> 3776 3777Clicks this component. 3778 3779This API is deprecated since API version 9. You are advised to use [click<sup>9+</sup>](#click9) instead. 3780 3781**System capability**: SystemCapability.Test.UiTest 3782 3783**Example** 3784 3785```ts 3786import { UiDriver, BY, Driver, UiComponent } from '@ohos.UiTest'; 3787async function demo() { 3788 let driver: UiDriver = UiDriver.create(); 3789 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3790 await button.click(); 3791} 3792``` 3793 3794### doubleClick<sup>(deprecated)</sup> 3795 3796doubleClick(): Promise\<void> 3797 3798Double-clicks this component. 3799 3800This API is deprecated since API version 9. You are advised to use [doubleClick<sup>9+</sup>](#doubleclick9) instead. 3801 3802**System capability**: SystemCapability.Test.UiTest 3803 3804**Example** 3805 3806```ts 3807import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3808async function demo() { 3809 let driver: UiDriver = UiDriver.create(); 3810 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3811 await button.doubleClick(); 3812} 3813``` 3814 3815### longClick<sup>(deprecated)</sup> 3816 3817longClick(): Promise\<void> 3818 3819Long-clicks this component. 3820 3821This API is deprecated since API version 9. You are advised to use [longClick<sup>9+</sup>](#longclick9) instead. 3822 3823**System capability**: SystemCapability.Test.UiTest 3824 3825**Example** 3826 3827```ts 3828import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3829async function demo() { 3830 let driver: UiDriver = UiDriver.create(); 3831 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3832 await button.longClick(); 3833} 3834``` 3835 3836### getId<sup>(deprecated)</sup> 3837 3838getId(): Promise\<number> 3839 3840Obtains the ID of this component. 3841 3842This API is deprecated since API version 9. 3843 3844**System capability**: SystemCapability.Test.UiTest 3845 3846**Return value** 3847 3848| Type | Description | 3849| ---------------- | ------------------------------- | 3850| Promise\<number> | Promise used to return the ID of the component.| 3851 3852**Example** 3853 3854```ts 3855import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3856async function demo() { 3857 let driver: UiDriver = UiDriver.create(); 3858 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3859 let id = await button.getId(); 3860} 3861``` 3862 3863### getKey<sup>(deprecated)</sup> 3864 3865getKey(): Promise\<string> 3866 3867Obtains the key of this component. 3868 3869This API is deprecated since API version 9. You are advised to use [getId<sup>9+</sup>](#getid9) instead. 3870 3871**System capability**: SystemCapability.Test.UiTest 3872 3873**Return value** 3874 3875| Type | Description | 3876| ---------------- | ------------------------------ | 3877| Promise\<string> | Promise used to return the key of the component.| 3878 3879**Example** 3880 3881```ts 3882import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3883async function demo() { 3884 let driver: UiDriver = UiDriver.create(); 3885 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3886 let str_key = await button.getKey(); 3887} 3888``` 3889 3890### getText<sup>(deprecated)</sup> 3891 3892getText(): Promise\<string> 3893 3894Obtains the text information of this component. 3895 3896This API is deprecated since API version 9. You are advised to use [getText<sup>9+</sup>](#gettext9) instead. 3897 3898**System capability**: SystemCapability.Test.UiTest 3899 3900**Return value** 3901 3902| Type | Description | 3903| ---------------- | --------------------------------- | 3904| Promise\<string> | Promise used to return the text information of the component.| 3905 3906**Example** 3907 3908```ts 3909import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3910async function demo() { 3911 let driver: UiDriver = UiDriver.create(); 3912 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3913 let text = await button.getText(); 3914} 3915``` 3916 3917### getType<sup>(deprecated)</sup> 3918 3919getType(): Promise\<string> 3920 3921Obtains the type of this component. 3922 3923This API is deprecated since API version 9. You are advised to use [getType<sup>9+</sup>](#gettype9) instead. 3924 3925**System capability**: SystemCapability.Test.UiTest 3926 3927**Return value** 3928 3929| Type | Description | 3930| ---------------- | ----------------------------- | 3931| Promise\<string> | Promise used to return the type of the component.| 3932 3933**Example** 3934 3935```ts 3936import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3937async function demo() { 3938 let driver: UiDriver = UiDriver.create(); 3939 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3940 let type = await button.getType(); 3941} 3942``` 3943 3944### isClickable<sup>(deprecated)</sup> 3945 3946isClickable(): Promise\<boolean> 3947 3948Obtains the clickable status of this component. 3949 3950This API is deprecated since API version 9. You are advised to use [isClickable<sup>9+</sup>](#isclickable9) instead. 3951 3952**System capability**: SystemCapability.Test.UiTest 3953 3954**Return value** 3955 3956| Type | Description | 3957| ----------------- | ------------------------------------------------------------ | 3958| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is clickable, and **false** means the opposite.| 3959 3960**Example** 3961 3962```ts 3963import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3964async function demo() { 3965 let driver: UiDriver = UiDriver.create(); 3966 let button: UiComponent = await driver.findComponent(BY.type('Button')); 3967 if(await button.isClickable()) { 3968 console.info('This button can be Clicked'); 3969 } else { 3970 console.info('This button can not be Clicked'); 3971 } 3972} 3973``` 3974 3975### isScrollable<sup>(deprecated)</sup> 3976 3977isScrollable(): Promise\<boolean> 3978 3979Obtains the scrollable status of this component. 3980 3981This API is deprecated since API version 9. You are advised to use [isScrollable<sup>9+</sup>](#isscrollable9) instead. 3982 3983**System capability**: SystemCapability.Test.UiTest 3984 3985**Return value** 3986 3987| Type | Description | 3988| ----------------- | ------------------------------------------------------------ | 3989| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is scrollable, and **false** means the opposite.| 3990 3991**Example** 3992 3993```ts 3994import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 3995async function demo() { 3996 let driver: UiDriver = UiDriver.create(); 3997 let scrollBar: UiComponent = await driver.findComponent(BY.scrollable(true)); 3998 if(await scrollBar.isScrollable()) { 3999 console.info('This scrollBar can be operated'); 4000 } else { 4001 console.info('This scrollBar can not be operated'); 4002 } 4003} 4004``` 4005 4006 4007### isEnabled<sup>(deprecated)</sup> 4008 4009isEnabled(): Promise\<boolean> 4010 4011Obtains the enabled status of this component. 4012 4013This API is deprecated since API version 9. You are advised to use [isEnabled<sup>9+</sup>](#isenabled9) instead. 4014 4015**System capability**: SystemCapability.Test.UiTest 4016 4017**Return value** 4018 4019| Type | Description | 4020| ----------------- | ---------------------------------------------------------- | 4021| Promise\<boolean> | Promise used to return the result. The value **true** means that the component is enabled, and **false** means the opposite.| 4022 4023**Example** 4024 4025```ts 4026import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 4027async function demo() { 4028 let driver: UiDriver = UiDriver.create(); 4029 let button: UiComponent = await driver.findComponent(BY.type('Button')); 4030 if(await button.isEnabled()) { 4031 console.info('This button can be operated'); 4032 } else { 4033 console.info('This button can not be operated'); 4034 } 4035} 4036 4037``` 4038 4039### isFocused<sup>(deprecated)</sup> 4040 4041isFocused(): Promise\<boolean> 4042 4043Obtains the focused status of this component. 4044 4045This API is deprecated since API version 9. You are advised to use [isFocused<sup>9+</sup>](#isfocused9) instead. 4046 4047**System capability**: SystemCapability.Test.UiTest 4048 4049**Return value** 4050 4051| Type | Description | 4052| ----------------- | ------------------------------------------------------------ | 4053| Promise\<boolean> | Promise used to return the result. The value **true** means that the target component is focused, and **false** means the opposite.| 4054 4055**Example** 4056 4057```ts 4058import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 4059async function demo() { 4060 let driver: UiDriver = UiDriver.create(); 4061 let button: UiComponent = await driver.findComponent(BY.type('Button')); 4062 if(await button.isFocused()) { 4063 console.info('This button is focused'); 4064 } else { 4065 console.info('This button is not focused'); 4066 } 4067} 4068``` 4069 4070### isSelected<sup>(deprecated)</sup> 4071 4072isSelected(): Promise\<boolean> 4073 4074Obtains the selected status of this component. 4075 4076This API is deprecated since API version 9. You are advised to use [isSelected<sup>9+</sup>](#isselected9) instead. 4077 4078**System capability**: SystemCapability.Test.UiTest 4079 4080**Return value** 4081 4082| Type | Description | 4083| ----------------- | ----------------------------------------------------- | 4084| Promise\<boolean> | Promise used to return the selected status of the component. The value **true** means that the component is selected, and **false** means the opposite.| 4085 4086**Example** 4087 4088```ts 4089import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 4090async function demo() { 4091 let driver: UiDriver = UiDriver.create(); 4092 let button: UiComponent = await driver.findComponent(BY.type('Button')); 4093 if(await button.isSelected()) { 4094 console.info('This button is selected'); 4095 } else { 4096 console.info('This button is not selected'); 4097 } 4098} 4099``` 4100 4101### inputText<sup>(deprecated)</sup> 4102 4103inputText(text: string): Promise\<void> 4104 4105Enters text into this component (available for text boxes). 4106 4107This API is deprecated since API version 9. You are advised to use [inputText<sup>9+</sup>](#inputtext9) instead. 4108 4109**System capability**: SystemCapability.Test.UiTest 4110 4111**Parameters** 4112 4113| Name| Type | Mandatory| Description | 4114| ------ | ------ | ---- | ---------------- | 4115| text | string | Yes | Text to enter.| 4116 4117**Example** 4118 4119```ts 4120import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 4121async function demo() { 4122 let driver: UiDriver = UiDriver.create(); 4123 let text: UiComponent = await driver.findComponent(BY.text('hello world')); 4124 await text.inputText('123'); 4125} 4126``` 4127 4128### scrollSearch<sup>(deprecated)</sup> 4129 4130scrollSearch(by: By): Promise\<UiComponent> 4131 4132Scrolls on this component to search for the target component (applicable to components that support scrolling, such as **\<List>**). 4133 4134This API is deprecated since API version 9. You are advised to use [scrollSearch<sup>9+</sup>](#scrollsearch9) instead. 4135 4136**System capability**: SystemCapability.Test.UiTest 4137 4138**Parameters** 4139 4140| Name| Type | Mandatory| Description | 4141| ------ | ------------------- | ---- | -------------------- | 4142| by | [By](#bydeprecated) | Yes | Attributes of the target component.| 4143 4144**Return value** 4145 4146| Type | Description | 4147| ----------------------------------------------- | ------------------------------------- | 4148| Promise\<[UiComponent](#uicomponentdeprecated)> | Promise used to return the target component.| 4149 4150**Example** 4151 4152```ts 4153import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 4154async function demo() { 4155 let driver: UiDriver = UiDriver.create(); 4156 let scrollBar: UiComponent = await driver.findComponent(BY.type('Scroll')); 4157 let button = await scrollBar.scrollSearch(BY.text('next page')); 4158} 4159``` 4160 4161## UiDriver<sup>(deprecated)</sup> 4162 4163The **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. 4164All APIs provided by this class, except **UiDriver.create()**, use a promise to return the result and must be invoked using **await**. 4165 4166This class is deprecated since API version 9. You are advised to use [Driver<sup>9+</sup>](#driver9) instead. 4167 4168### create<sup>(deprecated)</sup> 4169 4170static create(): UiDriver 4171 4172Creates a **UiDriver** object and returns the object created. This API is a static API. 4173 4174This API is deprecated since API version 9. You are advised to use [create<sup>9+</sup>](#create9) instead. 4175 4176**System capability**: SystemCapability.Test.UiTest 4177 4178**Return value** 4179 4180| Type | Description | 4181| -------- | ------------------------ | 4182| UiDriver | Returns the **UiDriver** object created.| 4183 4184**Example** 4185 4186```ts 4187import { UiDriver } from '@ohos.UiTest'; 4188async function demo() { 4189 let driver: UiDriver = UiDriver.create(); 4190} 4191``` 4192 4193### delayMs<sup>(deprecated)</sup> 4194 4195delayMs(duration: number): Promise\<void> 4196 4197Delays this **UiDriver** object within the specified duration. 4198 4199This API is deprecated since API version 9. You are advised to use [delayMs<sup>9+</sup>](#delayms9) instead. 4200 4201**System capability**: SystemCapability.Test.UiTest 4202 4203**Parameters** 4204 4205| Name | Type | Mandatory| Description | 4206| -------- | ------ | ---- | ------------ | 4207| duration | number | Yes | Duration of time.| 4208 4209**Example** 4210 4211```ts 4212import { UiDriver } from '@ohos.UiTest'; 4213async function demo() { 4214 let driver: UiDriver = UiDriver.create(); 4215 await driver.delayMs(1000); 4216} 4217``` 4218 4219### findComponent<sup>(deprecated)</sup> 4220 4221findComponent(by: By): Promise\<UiComponent> 4222 4223Searches this **UiDriver** object for the target component that matches the given attributes. 4224 4225This API is deprecated since API version 9. You are advised to use [findComponent<sup>9+</sup>](#findcomponent9) instead. 4226 4227**System capability**: SystemCapability.Test.UiTest 4228 4229**Parameters** 4230 4231| Name| Type | Mandatory| Description | 4232| ------ | ------------------- | ---- | -------------------- | 4233| by | [By](#bydeprecated) | Yes | Attributes of the target component.| 4234 4235**Return value** 4236 4237| Type | Description | 4238| ----------------------------------------------- | --------------------------------- | 4239| Promise\<[UiComponent](#uicomponentdeprecated)> | Promise used to return the found component.| 4240 4241**Example** 4242 4243```ts 4244import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 4245async function demo() { 4246 let driver: UiDriver = UiDriver.create(); 4247 let button: UiComponent = await driver.findComponent(BY.text('next page')); 4248} 4249``` 4250 4251### findComponents<sup>(deprecated)</sup> 4252 4253findComponents(by: By): Promise\<Array\<UiComponent>> 4254 4255Searches this **UiDriver** object for all components that match the given attributes. 4256 4257This API is deprecated since API version 9. You are advised to use [findComponents<sup>9+</sup>](#findcomponents9) instead. 4258 4259**System capability**: SystemCapability.Test.UiTest 4260 4261**Parameters** 4262 4263| Name| Type | Mandatory| Description | 4264| ------ | ------------------- | ---- | -------------------- | 4265| by | [By](#bydeprecated) | Yes | Attributes of the target component.| 4266 4267**Return value** 4268 4269| Type | Description | 4270| ------------------------------------------------------- | --------------------------------------- | 4271| Promise\<Array\<[UiComponent](#uicomponentdeprecated)>> | Promise used to return a list of found components.| 4272 4273**Example** 4274 4275```ts 4276import { UiDriver, BY, UiComponent } from '@ohos.UiTest'; 4277async function demo() { 4278 let driver: UiDriver = UiDriver.create(); 4279 let buttonList: Array<UiComponent> = await driver.findComponents(BY.text('next page')); 4280} 4281``` 4282 4283### assertComponentExist<sup>(deprecated)</sup> 4284 4285assertComponentExist(by: By): Promise\<void> 4286 4287Asserts 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. 4288 4289This API is deprecated since API version 9. You are advised to use [assertComponentExist<sup>9+</sup>](#assertcomponentexist9) instead. 4290 4291**System capability**: SystemCapability.Test.UiTest 4292 4293**Parameters** 4294 4295| Name| Type | Mandatory| Description | 4296| ------ | ------------------- | ---- | -------------------- | 4297| by | [By](#bydeprecated) | Yes | Attributes of the target component.| 4298 4299**Error codes** 4300 4301For details about the error codes, see [UiTest Error Codes](../errorcodes/errorcode-uitest.md). 4302 4303| ID| Error Message | 4304| -------- | ------------------------------------------------ | 4305| 17000002 | if the async function was not called with await. | 4306| 17000003 | if the assertion failed. | 4307 4308**Example** 4309 4310```ts 4311import { UiDriver, BY } from '@ohos.UiTest'; 4312async function demo() { 4313 let driver: UiDriver = UiDriver.create(); 4314 await driver.assertComponentExist(BY.text('next page')); 4315} 4316``` 4317 4318### pressBack<sup>(deprecated)</sup> 4319 4320pressBack(): Promise\<void> 4321 4322Presses the Back button on this **UiDriver** object. 4323 4324This API is deprecated since API version 9. You are advised to use [pressBack<sup>9+</sup>](#pressback9) instead. 4325 4326**System capability**: SystemCapability.Test.UiTest 4327 4328**Example** 4329 4330```ts 4331import { UiDriver } from '@ohos.UiTest'; 4332async function demo() { 4333 let driver: UiDriver = UiDriver.create(); 4334 await driver.pressBack(); 4335} 4336``` 4337 4338### triggerKey<sup>(deprecated)</sup> 4339 4340triggerKey(keyCode: number): Promise\<void> 4341 4342Triggers the key of this **UiDriver** object that matches the given key code. 4343 4344This API is deprecated since API version 9. You are advised to use [triggerKey<sup>9+</sup>](#triggerkey9) instead. 4345 4346**System capability**: SystemCapability.Test.UiTest 4347 4348**Parameters** 4349 4350| Name | Type | Mandatory| Description | 4351| ------- | ------ | ---- | ------------- | 4352| keyCode | number | Yes | Key code.| 4353 4354**Example** 4355 4356```ts 4357import { Driver, UiDriver } from '@ohos.UiTest'; 4358async function demo() { 4359 let driver: UiDriver = UiDriver.create(); 4360 await driver.triggerKey(123); 4361} 4362``` 4363 4364 4365### click<sup>(deprecated)</sup> 4366 4367click(x: number, y: number): Promise\<void> 4368 4369Clicks a specific point of this **UiDriver** object based on the given coordinates. 4370 4371This API is deprecated since API version 9. You are advised to use [click<sup>9+</sup>](#click9) instead. 4372 4373**System capability**: SystemCapability.Test.UiTest 4374 4375**Parameters** 4376 4377| Name| Type | Mandatory| Description | 4378| ------ | ------ | ---- | -------------------------------------- | 4379| x | number | Yes | X coordinate of the target point.| 4380| y | number | Yes | Y coordinate of the target point.| 4381 4382**Example** 4383 4384```ts 4385import { UiDriver } from '@ohos.UiTest'; 4386async function demo() { 4387 let driver: UiDriver = UiDriver.create(); 4388 await driver.click(100,100); 4389} 4390``` 4391 4392### doubleClick<sup>(deprecated)</sup> 4393 4394doubleClick(x: number, y: number): Promise\<void> 4395 4396Double-clicks a specific point of this **UiDriver** object based on the given coordinates. 4397 4398This API is deprecated since API version 9. You are advised to use [doubleClick<sup>9+</sup>](#doubleclick9) instead. 4399 4400**System capability**: SystemCapability.Test.UiTest 4401 4402**Parameters** 4403 4404| Name| Type | Mandatory| Description | 4405| ------ | ------ | ---- | -------------------------------------- | 4406| x | number | Yes | X coordinate of the target point.| 4407| y | number | Yes | Y coordinate of the target point.| 4408 4409**Example** 4410 4411```ts 4412import { UiDriver } from '@ohos.UiTest'; 4413async function demo() { 4414 let driver: UiDriver = UiDriver.create(); 4415 await driver.doubleClick(100,100); 4416} 4417``` 4418 4419### longClick<sup>(deprecated)</sup> 4420 4421longClick(x: number, y: number): Promise\<void> 4422 4423Long-clicks a specific point of this **UiDriver** object based on the given coordinates. 4424 4425This API is deprecated since API version 9. You are advised to use [longClick<sup>9+</sup>](#longclick9) instead. 4426 4427**System capability**: SystemCapability.Test.UiTest 4428 4429**Parameters** 4430 4431| Name| Type | Mandatory| Description | 4432| ------ | ------ | ---- | -------------------------------------- | 4433| x | number | Yes | X coordinate of the target point.| 4434| y | number | Yes | Y coordinate of the target point.| 4435 4436**Example** 4437 4438```ts 4439import { UiDriver } from '@ohos.UiTest'; 4440async function demo() { 4441 let driver: UiDriver = UiDriver.create(); 4442 await driver.longClick(100,100); 4443} 4444``` 4445 4446### swipe<sup>(deprecated)</sup> 4447 4448swipe(startx: number, starty: number, endx: number, endy: number): Promise\<void> 4449 4450Swipes on this **UiDriver** object from the start point to the end point based on the given coordinates. 4451 4452This API is deprecated since API version 9. You are advised to use [swipe<sup>9+</sup>](#swipe9) instead. 4453 4454**System capability**: SystemCapability.Test.UiTest 4455 4456**Parameters** 4457 4458| Name| Type | Mandatory| Description | 4459| ------ | ------ | ---- | -------------------------------------- | 4460| startx | number | Yes | X coordinate of the start point.| 4461| starty | number | Yes | Y coordinate of the start point.| 4462| endx | number | Yes | X coordinate of the end point.| 4463| endy | number | Yes | Y coordinate of the end point.| 4464 4465**Example** 4466 4467```ts 4468import { UiDriver } from '@ohos.UiTest'; 4469async function demo() { 4470 let driver: UiDriver = UiDriver.create(); 4471 await driver.swipe(100,100,200,200); 4472} 4473``` 4474 4475### screenCap<sup>(deprecated)</sup> 4476 4477screenCap(savePath: string): Promise\<boolean> 4478 4479Captures the current screen of this **UiDriver** object and saves it as a PNG image to the given save path. 4480 4481This API is deprecated since API version 9. You are advised to use [screenCap<sup>9+</sup>](#screencap9) instead. 4482 4483**System capability**: SystemCapability.Test.UiTest 4484 4485**Parameters** 4486 4487| Name | Type | Mandatory| Description | 4488| -------- | ------ | ---- | -------------- | 4489| savePath | string | Yes | File save path.| 4490 4491**Return value** 4492 4493| Type | Description | 4494| ----------------- | -------------------------------------- | 4495| Promise\<boolean> | Promise used to return the result. The value **true** means that the operation is successful.| 4496 4497**Example** 4498 4499```ts 4500import { UiDriver } from '@ohos.UiTest'; 4501async function demo() { 4502 let driver: UiDriver = UiDriver.create(); 4503 await driver.screenCap('/data/storage/el2/base/cache/1.png'); 4504} 4505``` 4506