OpenHarmonyTestRunner.ts 2.7 KB
Newer Older
1
/**
Q
qu-xianfei 已提交
2
 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Q
qu-xianfei 已提交
15 16 17
import hilog from '@ohos.hilog';
import TestRunner from '@ohos.application.testRunner';
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
18 19 20 21 22

var abilityDelegator = undefined
var abilityDelegatorArguments = undefined

async function onAbilityCreateCallback() {
Q
qu-xianfei 已提交
23
    hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback');
24 25 26
}

async function addAbilityMonitorCallback(err: any) {
Q
qu-xianfei 已提交
27 28
    hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s',
        JSON.stringify(err) ?? '');
29 30 31 32 33 34 35
}

export default class OpenHarmonyTestRunner implements TestRunner {
    constructor() {
    }

    onPrepare() {
Q
qu-xianfei 已提交
36
        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare');
37 38 39
    }

    onRun() {
Q
qu-xianfei 已提交
40
        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run');
41 42
        abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
Q
qu-xianfei 已提交
43
        var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility'
44 45 46 47 48 49
        let lMonitor = {
            abilityName: testAbilityName,
            onAbilityCreate: onAbilityCreateCallback,
        };
        abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
        var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName
Q
qu-xianfei 已提交
50 51 52 53 54 55
        var debug = abilityDelegatorArguments.parameters['-D']
        if (debug == 'true')
        {
            cmd += ' -D'
        }
        hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd);
56 57
        abilityDelegator.executeShellCommand(cmd,
            (err: any, d: any) => {
Q
qu-xianfei 已提交
58 59 60 61 62 63
                hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s',
                    JSON.stringify(err) ?? '');
                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s',
                    d.stdResult ?? '');
                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s',
                    d.exitCode ?? '');
64
            })
Q
qu-xianfei 已提交
65
        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end');
66 67
    }
};