HarmonyOS Next UI测试入门

141 阅读2分钟

HarmonyOS Next UI测试入门

HarmonyOS Next提供的自动化测试框架arkxtest,支持JS/TS语言的单元测试框架(JsUnit)及UI测试框架(UiTest)。UiTest通过简洁易用的API提供查找和操作界面控件能力,支持用户开发基于界面操作的自动化测试脚本。本文介绍HarmonyOS Next

UI自动化框架实现原理

HarmonyOS Next中UI测试框架主要功能包括:

image.png

UI测试脚本编写

UI测试基于单元测试,UI测试脚本在单元测试脚本上增加了对UiTest接口。下面示例代码是实现的场景是:在启动的应用页面上进行点击操作,然后检测当前页面变化是否为预期变化。

  1. 编写Index.ets页面代码, 作为被测示例demo。
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text("Next")
          .fontSize(50)
          .margin({top:20})
          .fontWeight(FontWeight.Bold)
        Text("after click")
          .fontSize(50)
          .margin({top:20})
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

2. 在ohosTest > ets > test文件夹下.test.ets文件中编写具体测试代码。

import { describe, it, expect } from '@ohos/hypium';
// 导入测试依赖kit
import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit';
import { UIAbility, 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('ActsAbilityTest', () => {
     it('testUiExample',0, async (done: Function) => {
        console.info("uitest: TestUiExample begin");
        //start tested ability
        const want: Want = {
           bundleName: bundleName,
           abilityName: 'EntryAbility'
        }
        await delegator.startAbility(want);
        await sleep(1000);
        //check top display ability
        const ability: UIAbility = await delegator.getCurrentTopAbility();
        console.info("get top ability");
        expect(ability.context.abilityInfo.name).assertEqual('EntryAbility');
        //ui test code
        //init driver
        const driver = Driver.create();
        await driver.delayMs(1000);
        //find button on text 'Next'
        const button = await driver.findComponent(ON.text('Next'));
        //click button
        await button.click();
        await driver.delayMs(1000);
        //check text
        await driver.assertComponentExist(ON.text('after click'));
        await driver.pressBack();
        done();
     })
  })
}

UI测试能力总结

一般来说,在UI测试中,不管是网页还是Android还是iOS,实现的效果不外乎获取控件、操作控件、模拟点击、模拟触摸,模拟双击、长按、滑动等,一般对应有具体API做对应操作。

HarmonyOS Next中@ohos.UiTest提供了以下能力:

  • On:提供控件特征描述能力,用于控件筛选匹配查找。
  • Component:代表UI界面上的指定控件,提供控件属性获取,控件点击,滑动查找,文本注入等能力。
  • Driver:入口类,提供控件匹配/查找,按键注入,坐标点击/滑动,截图等能力。
  • UiWindow:入口类,提供窗口属性获取,窗口拖动、调整窗口大小等能力。
  • By(deprecated):提供控件特征描述能力,用于控件筛选匹配查找。从API version 9开始不再维护,建议使用On。
  • UiComponent(deprecated):代表UI界面上的指定控件,提供控件属性获取,控件点击,滑动查找,文本注入等能力。从API version 9开始不再维护,建议使用Component。
  • UiDriver(deprecated):入口类,提供控件匹配/查找,按键注入,坐标点击/滑动,截图等能力。从API version 9开始不再维护,建议使用Driver。

总结

本文介绍了HarmonyOS Next中UI自动化测试的能力,操作步骤等具体使用方式。