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 TestRunner from '@ohos.application.testRunner'; 17import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'; 18 19let abilityDelegator = undefined; 20let abilityDelegatorArguments = undefined; 21 22function translateParamsToString(parameters): string { 23 const keySet = new Set([ 24 '-s class', '-s notClass', '-s suite', '-s it', 25 '-s level', '-s testType', '-s size', '-s timeout' 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(): Promise<void> { 37 console.log('onAbilityCreateCallback'); 38} 39 40async function addAbilityMonitorCallback(err): Promise<void> { 41 console.info('addAbilityMonitorCallback : ' + JSON.stringify(err)); 42} 43 44export default class OpenHarmonyTestRunner implements TestRunner { 45 constructor() { 46 } 47 48 onPrepare(): void { 49 console.info('OpenHarmonyTestRunner OnPrepare '); 50 } 51 52 async onRun(): Promise<void> { 53 console.log('OpenHarmonyTestRunner onRun run'); 54 abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments(); 55 abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); 56 let testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'; 57 let lMonitor = { 58 abilityName: testAbilityName, 59 onAbilityCreate: onAbilityCreateCallback, 60 }; 61 abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback); 62 let cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName; 63 cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters); 64 console.info('cmd : ' + cmd); 65 abilityDelegator.executeShellCommand(cmd, 66 (err, d) => { 67 console.info('executeShellCommand : err : ' + JSON.stringify(err)); 68 console.info('executeShellCommand : data : ' + d.stdResult); 69 console.info('executeShellCommand : data : ' + d.exitCode); 70 }); 71 console.info('OpenHarmonyTestRunner onRun end'); 72 } 73};