• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import TestRunner from '@ohos.application.testRunner'
16import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
17
18var abilityDelegator = undefined
19var 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    ])
26    let targetParams = '';
27    for (const key in parameters) {
28        if (keySet.has(key)) {
29            targetParams = `${targetParams} ${key} ${parameters[key]}`
30        }
31    }
32    return targetParams.trim()
33}
34
35async function onAbilityCreateCallback() {
36    console.log("onAbilityCreateCallback");
37}
38
39async function addAbilityMonitorCallback(err: any) {
40    console.info("addAbilityMonitorCallback : " + JSON.stringify(err))
41}
42
43export default class OpenHarmonyTestRunner implements TestRunner {
44    constructor() {
45    }
46
47    onPrepare() {
48        console.info("OpenHarmonyTestRunner OnPrepare ")
49    }
50
51    async onRun() {
52        console.log('OpenHarmonyTestRunner onRun run')
53        abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
54        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
55        var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'
56        let lMonitor = {
57            abilityName: testAbilityName,
58            onAbilityCreate: onAbilityCreateCallback,
59        };
60        abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
61        var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
62        cmd += ' '+translateParamsToString(abilityDelegatorArguments.parameters)
63        console.info('cmd : '+cmd)
64        abilityDelegator.executeShellCommand(cmd,
65            (err: any, d: any) => {
66                console.info('executeShellCommand : err : ' + JSON.stringify(err));
67                console.info('executeShellCommand : data : ' + d.stdResult);
68                console.info('executeShellCommand : data : ' + d.exitCode);
69            })
70        console.info('OpenHarmonyTestRunner onRun end')
71    }
72};