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