1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import hilog from '@ohos.hilog'; 17import TestRunner from '@ohos.application.testRunner'; 18import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; 19 20var abilityDelegator = undefined 21var abilityDelegatorArguments = undefined 22 23function translateParamsToString(parameters) { 24 const keySet = new Set([ 25 '-s class', '-s notClass', '-s suite', '-s it', 26 '-s level', '-s testType', '-s size', '-s timeout', 27 '-s dryRun' 28 ]) 29 let targetParams = ''; 30 for (const key in parameters) { 31 if (keySet.has(key)) { 32 targetParams = `${targetParams} ${key} ${parameters[key]}` 33 } 34 } 35 return targetParams.trim(); 36} 37 38async function onAbilityCreateCallback() { 39 hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); 40} 41 42async function addAbilityMonitorCallback(err: any) { 43 hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); 44} 45 46export default class OpenHarmonyTestRunner implements TestRunner { 47 constructor() { 48 } 49 50 onPrepare() { 51 hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); 52 } 53 54 async onRun() { 55 hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); 56 abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() 57 abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() 58 var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' 59 let lMonitor = { 60 abilityName: testAbilityName, 61 onAbilityCreate: onAbilityCreateCallback, 62 }; 63 abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) 64 var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName 65 cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters) 66 var debug = abilityDelegatorArguments.parameters['-D'] 67 if (debug == 'true') 68 { 69 cmd += ' -D' 70 } 71 hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); 72 abilityDelegator.executeShellCommand(cmd, 73 (err: any, d: any) => { 74 hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? ''); 75 hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? ''); 76 hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? ''); 77 }) 78 hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); 79 } 80}