1/* 2 * Copyright (c) 2022 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 TestRunner from '@ohos.application.testRunner' 17import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' 18import Logger from '../../../main/ets/MainAbility/util/Logger' 19 20const TAG: string = 'TestAbilityStage' 21let abilityDelegator = undefined 22let abilityDelegatorArguments = undefined 23 24function translateParamsToString(parameters) { 25 const keySet = new Set([ 26 '-s class', '-s notClass', '-s suite', '-s it', 27 '-s level', '-s testType', '-s size', '-s timeout', 28 '-s dryRun' 29 ]) 30 let targetParams = '' 31 for (const key in parameters) { 32 if (keySet.has(key)) { 33 targetParams = `${targetParams} ${key} ${parameters[key]}` 34 } 35 } 36 return targetParams.trim() 37} 38 39async function onAbilityCreateCallback() { 40 Logger.info(TAG, 'onAbilityCreateCallback') 41} 42 43async function addAbilityMonitorCallback(err: any) { 44 Logger.info(TAG, `addAbilityMonitorCallback : ${JSON.stringify(err)}`) 45} 46 47export default class OpenHarmonyTestRunner implements TestRunner { 48 constructor() { 49 } 50 51 onPrepare() { 52 Logger.info(TAG, 'OpenHarmonyTestRunner OnPrepare ') 53 } 54 55 async onRun() { 56 Logger.info(TAG, 'OpenHarmonyTestRunner onRun run') 57 abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() 58 abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() 59 let testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' 60 let lMonitor = { 61 abilityName: testAbilityName, 62 onAbilityCreate: onAbilityCreateCallback, 63 } 64 abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) 65 let cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName 66 cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters) 67 let debug = abilityDelegatorArguments.parameters["-D"] 68 if (debug == 'true') { 69 cmd += ' -D' 70 } 71 Logger.info(TAG, `cmd : ${+cmd}`) 72 abilityDelegator.executeShellCommand(cmd, (err: any, d: any) => { 73 Logger.info(TAG, `executeShellCommand : err : ${JSON.stringify(err)}`) 74 Logger.info(TAG, `executeShellCommand : data : ${d.stdResult}`) 75 Logger.info(TAG, `executeShellCommand : data : ${d.exitCode}`) 76 }) 77 Logger.info(TAG, 'OpenHarmonyTestRunner onRun end') 78 } 79}