# @ohos.UiTest The **UiTest** module provides APIs that you can use to simulate UI actions during testing, such as clicks, double-clicks, long-clicks, and swipes. This module provides the following functions: - [On9+](#on9): provides UI component feature description APIs for component filtering and matching. - [Component9+](#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. - [Driver9+](#driver9): works as the entry class and provides APIs for features such as component matching/search, key injection, coordinate clicking/sliding, and screenshot. - [UiWindow9+](#uiwindow9): works as the entry class and provides APIs for obtaining window attributes, dragging windows, and adjusting window sizes. - [By(deprecated)](#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 [On9+](#on9) instead. - [UiComponent(deprecated)](#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 [Component9+](#component9) instead. - [UiDriver(deprecated)](#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 [Driver9+](#driver9) instead. > **NOTE** > - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. > - The APIs of this module can be used only in [arkxtest](../../application-test/arkxtest-guidelines.md). > - The APIs of this module do not support concurrent calls. ## Modules to Import ```ts import { UiComponent, UiDriver, Component, Driver, UiWindow, ON, BY, MatchPattern, DisplayRotation, ResizeDirection, WindowMode, PointerMatrix, UiDirection, MouseButton, UIElementInfo, UIEventObserver } from '@kit.TestKit'; ``` ## MatchPattern Enumerates the match patterns supported for component attributes. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Value | Description | | ----------- | ---- | -------------- | | EQUALS | 0 | Equals the given value. | | CONTAINS | 1 | Contains the given value. | | STARTS_WITH | 2 | Starts with the given value.| | ENDS_WITH | 3 | Ends with the given value.| ## ResizeDirection9+ Enumerates the directions in which a window can be resized. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Value | Description | | ---------- | ---- | -------- | | LEFT | 0 | Left. | | RIGHT | 1 | Right. | | UP | 2 | Up. | | DOWN | 3 | Down. | | LEFT_UP | 4 | Upper left.| | LEFT_DOWN | 5 | Lower left.| | RIGHT_UP | 6 | Upper right.| | RIGHT_DOWN | 7 | Lower right.| ## Point9+ Provides the coordinates of a point. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name| Type | Readable| Writable| Description | | ---- | ------ | ---- | ---- | ---------------- | | x | number | Yes | No | X coordinate of a point.| | y | number | Yes | No | Y coordinate of a point.| ## Rect9+ Provides bounds information of a component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Type | Readable| Writable| Description | | ------ | ------ | ---- | ---- | ------------------------- | | left | number | Yes | No | X coordinate of the upper left corner of the component bounds.| | top | number | Yes | No | Y coordinate of the upper left corner of the component bounds.| | right | number | Yes | No | X coordinate of the lower right corner of the component bounds.| | bottom | number | Yes | No | Y coordinate of the lower right corner of the component bounds.| ## WindowMode9+ Enumerates the window modes. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Value | Description | | ---------- | ---- | ---------- | | FULLSCREEN | 0 | Full-screen mode.| | PRIMARY | 1 | Primary window mode. | | SECONDARY | 2 | Secondary window mode.| | FLOATING | 3 | Floating window mode.| ## DisplayRotation9+ Describes the display rotation of the device. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Value | Description | | ------------ | ---- | ---------------------------------------- | | ROTATION_0 | 0 | The device display is not rotated and is in its original vertical orientation. | | ROTATION_90 | 1 | The device display rotates 90° clockwise and is in landscape orientation. | | ROTATION_180 | 2 | The device display rotates 180° clockwise and is in reverse vertical orientation.| | ROTATION_270 | 3 | The device display rotates 270° clockwise and is in reverse landscape orientation.| ## WindowFilter9+ Provides the flag attributes of this window. **System capability**: SystemCapability.Test.UiTest | Name | Type | Readable| Writable| Description | | -------------------- | ------- | ---- | ---- | ------------------------------------------------------------ | | bundleName | string | Yes | No | Bundle name of the application to which the window belongs.
**Atomic service API**: This API can be used in atomic services since API version 11.| | title | string | Yes | No | Title of the window.
**Atomic service API**: This API can be used in atomic services since API version 11.| | focused | boolean | Yes | No | Whether the window is in focused state.
**Atomic service API**: This API can be used in atomic services since API version 11.| | actived(deprecated) | boolean | Yes | No | Whether the window is interacting with the user.
Since API version 11, this parameter is renamed **active**.| | active11+ | boolean | Yes | No | Whether the window is interacting with the user.
**Atomic service API**: This API can be used in atomic services since API version 11.| ## UiDirection10+ Describes the direction of a UI operation such as fling. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Value | Description | | ----- | ---- | ------ | | LEFT | 0 | Leftward.| | RIGHT | 1 | Rightward.| | UP | 2 | Upward.| | DOWN | 3 | Downward.| ## MouseButton10+ Describes the injected simulated mouse button. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Value | Description | | ------------------- | ---- | ------------ | | MOUSE_BUTTON_LEFT | 0 | Left button on the mouse. | | MOUSE_BUTTON_RIGHT | 1 | Right button on the mouse. | | MOUSE_BUTTON_MIDDLE | 2 | Middle button on the mouse.| ## UIElementInfo10+ Provides information about the UI event. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest | Name | Type | Readable| Writable| Description | | ---------- | ------ | ---- | ---- | --------------------- | | bundleName | string | Yes | No | Bundle name of the home application. | | type | string | Yes | No | Component or window type. | | text | string | Yes | No | Text information of the component or window.| ## On9+ Since API version 9, the UiTest framework provides a wide range of UI component feature description APIs in the **On** class to filter and match components.
The APIs provided by the **On** class exhibit the following features:
1. Allow one or more attributes as the match conditions. For example, you can specify both the **text** and **id** attributes to find the target component.
2. Provide multiple match patterns for component attributes.
3. 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.
All APIs provided in the **On** class are synchronous. You are advised to use the static constructor **ON** to create an **On** object in chain mode. ```ts import { ON } from '@kit.TestKit'; ON.text('123').type('Button'); ``` ### text9+ text(txt: string, pattern?: MatchPattern): On Specifies the text attribute of the target component. Multiple match patterns are supported. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name | Type | Mandatory| Description | | ------- | ----------------------------- | ---- | --------------------------------------------------- | | txt | string | Yes | Component text, used to match the target component. | | pattern | [MatchPattern](#matchpattern) | No | Match pattern. The default value is [EQUALS](#matchpattern).| **Return value** | Type | Description | | ---------- | ---------------------------------- | | [On](#on9) | **On** object that matches the text attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.text('123'); // Use the static constructor ON to create an On object and specify the text attribute of the target component. ``` ### id9+ id(id: string): On Specifies the ID attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ---------------- | | id | string | Yes | Component ID.| **Return value** | Type | Description | | ---------- | -------------------------------- | | [On](#on9) | **On** object that matches the ID attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.id('123'); // Use the static constructor ON to create an On object and specify the id attribute of the target component. ``` ### type9+ type(tp: string): On Specifies the type attribute of the target component. >**NOTE** > >You can define the type of the component. In addition, you can use [DevEco Testing](https://developer.huawei.com/consumer/cn/download) to query the type information of the component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------- | | tp | string | Yes | Component type.| **Return value** | Type | Description | | ---------- | ---------------------------------------- | | [On](#on9) | **On** object that matches the type attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.type('Button'); // Use the static constructor ON to create an On object and specify the type attribute of the target component. ``` ### clickable9+ clickable(b?: boolean): On Specifies the clickable attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | ------------------------------------------------------------ | | b | boolean | No | Clickable status of the target component.
**true**: clickable.
**false**: not clickable.
Default value: **true**| **Return value** | Type | Description | | ---------- | ------------------------------------------ | | [On](#on9) | **On** object that matches the clickable attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.clickable(true); // Use the static constructor ON to create an On object and specify the clickable attribute of the target component. ``` ### longClickable9+ longClickable(b?: boolean): On Specifies the long-clickable attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | ------------------------------------------------------------ | | b | boolean | No | Long-clickable status of the target component.
**true**: long-clickable.
**false**: not long-clickable.
Default value: **true**| **Return value** | Type | Description | | ---------- | ---------------------------------------------- | | [On](#on9) | **On** object that matches the long-clickable attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.longClickable(true); // Use the static constructor ON to create an On object and specify the longClickable attribute of the target component. ``` ### scrollable9+ scrollable(b?: boolean): On Specifies the scrollable attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | ----------------------------------------------------------- | | b | boolean | No | Scrollable status of the target component.
**true**: scrollable.
**false**: not scrollable.
Default value: **true**| **Return value** | Type | Description | | ---------- | ------------------------------------------ | | [On](#on9) | **On** object that matches the scrollable attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.scrollable(true); // Use the static constructor ON to create an On object and specify the scrollable attribute of the target component. ``` ### enabled9+ enabled(b?: boolean): On Specifies the enabled attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | --------------------------------------------------------- | | b | boolean | No | Enabled status of the target component.
**true**: enabled.
**false**: not enabled.
Default value: **true**| **Return value** | Type | Description | | ---------- | ---------------------------------------- | | [On](#on9) | **On** object that matches the enabled attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.enabled(true); // Use the static constructor ON to create an On object and specify the enabled attribute of the target component. ``` ### focused9+ focused(b?: boolean): On Specifies the focused attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | ----------------------------------------------------- | | b | boolean | No | Focused status of the target component.
**true**: focused.
**false**: not focused.
Default value: **true**| **Return value** | Type | Description | | ---------- | ---------------------------------------- | | [On](#on9) | **On** object that matches the focused attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.focused(true); // Use the static constructor ON to create an On object and specify the focused attribute of the target component. ``` ### selected9+ selected(b?: boolean): On Specifies the selected attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | ------------------------------------------------------------ | | b | boolean | No | Selected status of the target component.
**true**: selected.
**false**: not selected.
Default value: **true**| **Return value** | Type | Description | | ---------- | ------------------------------------------ | | [On](#on9) | **On** object that matches the selected attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.selected(true); // Use the static constructor ON to create an On object and specify the selected attribute of the target component. ``` ### checked9+ checked(b?: boolean): On Specifies the checked attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | ------------------------------------------------------------ | | b | boolean | No | Checked status of the target component.
**true**: checked.
**false**: not checked.
Default value: **false**| **Return value** | Type | Description | | ---------- | ------------------------------------------ | | [On](#on9) | **On** object that matches the checked attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.checked(true); // Use the static constructor ON to create an On object and specify the checked attribute of the target component. ``` ### checkable9+ checkable(b?: boolean): On Specifies the checkable attribute of the target component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | ------------------------------------------------------------ | | b | boolean | No | Checkable status of the target component.
**true**: checkable.
**false**: not checkable.
Default value: **false**| **Return value** | Type | Description | | ---------- | -------------------------------------------- | | [On](#on9) | **On** object that matches the checkable attribute of the target component.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. 1. Incorrect parameter types; 2. Parameter verification failed. | **Example** ```ts import { On, ON } from '@kit.TestKit'; let on:On = ON.checkable(true); // Use the static constructor ON to create an On object and specify the checkable attribute of the target component. ``` ### isBefore9+ isBefore(on: On): On Specifies that the target component is located before the given attribute component. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Test.UiTest **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------- | ---- | -------------------- | | on | [On](#on9) | Yes | Information about the attribute component.| **Return value** | Type | Description | | ---------- | ---------------------------------------------------- | | [On](#on9) | **On** object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| **Example** ```ts import { On, ON } from '@kit.TestKit'; // Use the static constructor ON to create an On object and specify that the target component is located before the given attribute component. let on:On = ON.type('Button').isBefore(ON.text('123')); // Search for the first