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