UI测试中如何在feature模块启动当前模块的ability
===============================

#### 问题现象

使用DevEco Studio进行UI测试，在feature模块下启动ability如何实现？

#### 背景知识

UI测试框架（UITest）为开发者提供UI界面查找和模拟操作能力，可覆盖UI自动化测试的关键场景，包括界面控件精准查找、UI交互操作（如点击、滑动、文本输入等），参考：[UI测试框架使用指导](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/uitest-guidelines)。

#### 解决方案

1.  在项目工程中新建feature模块：
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/c4/v3/BZrTzMaOTXyFMhaDRays0A/zh-cn_image_0000002658808887.png?HW-CC-KV=V1&HW-CC-Date=20260709T134420Z&HW-CC-Expire=86400&HW-CC-Sign=C3487EEB24DD33CF67D4D8DEFA3CEFB3D9BC624742FB523D641E1617978FDB80 "点击放大")
    
2.  打开feature\\src\\main\\module.json5，查看abilities标签的name属性值，确认需要启动的abilityName：
    
    "abilities": \[
      {
      "name": "FeatureAbility",
      "srcEntry": "./ets/featureability/FeatureAbility.ets",
      "description": "$string:FeatureAbility\_desc",
      "icon": "$media:layered\_image",
      "label": "$string:FeatureAbility\_label",
      "startWindowIcon": "$media:startIcon",
      "startWindowBackground": "$color:start\_window\_background",
      "exported": true
      }
    \],
    
3.  在feature\\src\\ohosTest\\ets\\test\\Ability.test.ets编写测试用例启动测试页面，代码示例如下：
    
    import { describe, it, expect } from '@ohos/hypium';
    import { abilityDelegatorRegistry } from '@kit.TestKit';
    import { Want } from '@kit.AbilityKit';
    
    const delegator: abilityDelegatorRegistry.AbilityDelegator \= abilityDelegatorRegistry.getAbilityDelegator();
    const bundleName \= abilityDelegatorRegistry.getArguments().bundleName;
    
    function sleep(time: number) {
      return new Promise<void\>((resolve: Function) \=\> setTimeout(resolve, time));
    }
    
    export default function abilityTest() {
      describe('AbilityTest', () \=\> {
    
        it('testStartFeatureTest', 0, async (done: Function) \=\> {
          const want: Want \= {
            bundleName: bundleName,
            abilityName: 'FeatureAbility'
          };
          try {
            await delegator.startAbility(want);
            await sleep(1000);
    
            const ability \= await delegator.getCurrentTopAbility();
            expect(ability.context.abilityInfo.name).assertEqual('FeatureAbility');
    
            done();
          } catch (error) {
            console.error(\`startAbility error. Code is ${error.code}, message is ${error.message}.\`);
          }
        });
      });
    }