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 AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' 17import TestRunner from '@ohos.application.testRunner' 18import { Logger } from '@ohos/base' 19 20var abilityDelegator = undefined 21var abilityDelegatorArguments = undefined 22const TAG: string = 'OpenHarmonyTestRunner' 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 var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' 60 let lMonitor = { 61 abilityName: testAbilityName, 62 onAbilityCreate: onAbilityCreateCallback, 63 }; 64 abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) 65 var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName 66 cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters) 67 var debug = abilityDelegatorArguments.parameters["-D"] 68 if (debug == 'true') { 69 cmd += ' -D' 70 } 71 Logger.info(TAG, `cmd : ${cmd}`) 72 abilityDelegator.executeShellCommand(cmd, 73 (err: any, d: any) => { 74 console.info('executeShellCommand : err : ' + JSON.stringify(err)); 75 console.info('executeShellCommand : data : ' + d.stdResult); 76 console.info('executeShellCommand : data : ' + d.exitCode); 77 }) 78 Logger.info(TAG, 'OpenHarmonyTestRunner onRun end') 79 } 80}