2 回复
-
在DevEco Studio中新建应用开发工程,其中ohos目录即为测试脚本所在的目录。
-
在工程目录下打开待测试模块下的ets文件,将光标置于代码中任意位置,单击右键 > Show Context Actions > Create Ohos Test或快捷键Alt+enter > Create Ohos Test创建测试类,更多指导请参考DevEco Studio中指导。
import { describe, it, expect } from '@ohos/hypium';
import abilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
import { BusinessError } from '@ohos.base';
import UIAbility from '@ohos.app.ability.UIAbility';
const delegator = abilityDelegatorRegistry.getAbilityDelegator()
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
await delegator.executeShellCommand('aa start -b com.ohos.uitest -a EntryAbility').then((result: abilityDelegatorRegistry.ShellCmdResult) =>{
console.info('Uitest, start ability finished:' + result)
}).catch((err: BusinessError) =>{
console.info('Uitest, start ability failed: ' + err)
})
await sleep(1000);
//check top display ability
await delegator.getCurrentTopAbility().then((Ability: UIAbility) =>{
console.info("get top ability");
expect(Ability.context.abilityInfo.name).assertEqual('EntryAbility');
})
done();
})
})
}
参考链接:自动化测试框架使用指导(基于ArkTS)-单元测试和UI测试-应用测试 - 华为HarmonyOS开发者 (huawei.com)
更多关于HarmonyOS 鸿蒙应用如何自动化测试的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html