1import TestRunner from '@ohos.application.testRunner' 2import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' 3 4var abilityDelegator = undefined 5var abilityDelegatorArguments = undefined 6 7function translateParamsToString(parameters) { 8 const keySet = new Set([ 9 '-s class', '-s notClass', '-s suite', '-s itName', 10 '-s level', '-s testType', '-s size', '-s timeout', 11 '-s package' 12 ]) 13 let targetParams = ''; 14 for (const key in parameters) { 15 if (keySet.has(key)) { 16 targetParams += ' ' + key + ' ' + parameters[key] 17 } 18 } 19 return targetParams.trim() 20} 21 22async function onAbilityCreateCallback() { 23 console.log('onAbilityCreateCallback'); 24} 25 26async function addAbilityMonitorCallback(err: any) { 27 console.info('addAbilityMonitorCallback : ' + JSON.stringify(err)) 28} 29 30export default class OpenHarmonyTestRunner implements TestRunner { 31 constructor() { 32 } 33 34 onPrepare() { 35 console.info('OpenHarmonyTestRunner OnPrepare') 36 } 37 38 onRun() { 39 console.log('OpenHarmonyTestRunner onRun run') 40 abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() 41 abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() 42 43 let lMonitor = { 44 abilityName: testAbilityName, 45 onAbilityCreate: onAbilityCreateCallback, 46 }; 47 var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility' 48 abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) 49 var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName 50 cmd += ' '+translateParamsToString(abilityDelegatorArguments.parameters) 51 console.info('cmd : '+cmd) 52 abilityDelegator.executeShellCommand(cmd, 53 (err: any, d: any) => { 54 console.info('executeShellCommand : err : ' + JSON.stringify(err)); 55 console.info('executeShellCommand : data : ' + d.stdResult); 56 console.info('executeShellCommand : data : ' + d.exitCode); 57 }) 58 console.info('OpenHarmonyTestRunner onRun call abilityDelegator.getAppContext') 59 var context = abilityDelegator.getAppContext() 60 console.info('getAppContext : ' + JSON.stringify(context)) 61 console.info('OpenHarmonyTestRunner onRun end') 62 } 63};