目录

ShellCmdResult

ShellCmdResult is a data object used to encapsulate the execution result of a shell command in the test framework. It contains two key attributes: stdResult (standard output content) and exitCode (exit code). This object provides a structured way for test scripts to access the command execution result.

This module is applicable to test scenarios where the execution result of a shell command needs to be verified or the data returned by the command needs to be processed. It is obtained by calling executeShellCommand of abilityDelegator. Note that the APIs of this module can be used only in the test framework and should not be called in formal service code.

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs of this module can be used only in JsUnit User Guide.

Modules to Import

import { abilityDelegatorRegistry } from '@kit.TestKit';

Usage

Call executeShellCommand in abilityDelegator to obtain a ShellCmdResult instance. After the call is successful, a ShellCmdResult object is returned, containing the standard result (stdResult) and exit code (exitCode) of the shell command.

Example

import { abilityDelegatorRegistry } from '@kit.TestKit';
import { BusinessError } from '@kit.BasicServicesKit';

let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
let cmd = 'cmd';

// Obtain the AbilityDelegator instance.
abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator();
// Execute the shell command and obtain the execution result through the callback.
abilityDelegator.executeShellCommand(cmd, (error: BusinessError, data) => {
  if (error) {
    console.error(`executeShellCommand fail, error: ${JSON.stringify(error)}`);
  } else {
    console.info(`executeShellCommand success, data: ${JSON.stringify(data)}`);
  }
});

ShellCmdResult

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Type Read-Only Optional Description
stdResult string No No Standard output of the shell command.
exitCode number No No Exit code of the Shell command. The value 0 indicates that the command execution succeeds, and a non-zero value indicates that the command execution fails.