Skip to content

Commit dd2abf7

Browse files
Address review comments
Co-authored-by: SrinivasanTarget <srinivasan.sekar1990@gmail.com>
1 parent 65cc86e commit dd2abf7

File tree

6 files changed

+56
-21
lines changed

6 files changed

+56
-21
lines changed

docs/reference/execute-methods.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,32 @@ Reads the battery information from the device under test. This endpoint only ret
484484

485485
#### Returned Result
486486

487-
The actual battery info map, which consists of the following entries:
487+
The returned object always includes at least the following entries:
488488

489-
- level: Battery level in range [0.0, 1.0], where 1.0 means 100% charge.
490-
- state: Battery state as an integer number. The following values are possible:
489+
- `level`: Battery level in range [0.0, 1.0], where 1.0 means 100% charge.
490+
- `state`: Battery state as an integer number. The following values are possible:
491491
* UIDeviceBatteryStateUnknown = 0
492492
* UIDeviceBatteryStateUnplugged = 1 // on battery, discharging
493493
* UIDeviceBatteryStateCharging = 2 // plugged in, less than 100%
494494
* UIDeviceBatteryStateFull = 3 // plugged in, at 100%
495495

496+
On iOS 18 and newer real devices, the returned object may also include many additional advanced battery information fields, such as capacity, health metrics, temperature, and more. For a full list of possible advanced fields, see the [BatteryInfo](../../lib/commands/advanced-battery-types.ts).
497+
498+
The returned object is a superset of the basic battery info, and may look like:
499+
500+
```json
501+
{
502+
"level": 0.85,
503+
"state": 2,
504+
"AbsoluteCapacity": 1234,
505+
"CycleCount": 456,
506+
"Temperature": 29.5,
507+
"...": "other advanced fields"
508+
}
509+
```
510+
511+
If advanced fields are not available (e.g., on older iOS versions or simulators), only `level` and `state` will be present.
512+
496513
### mobile: deviceInfo
497514

498515
Returns the miscellaneous information about the device under test.
@@ -1777,7 +1794,7 @@ The above three extensions are available since the driver version 4.9.0.
17771794

17781795
You can create a condition on a connected device to test your app under adverse conditions, such as poor network connectivity or thermal constraints.
17791796

1780-
When you start a device condition, the operating system on the device behaves as if its environment has changed. The device condition remains active until you stop the device condition or disconnect the device. For example, you can start a device condition, run your app, monitor your apps energy usage, and then stop the condition.
1797+
When you start a device condition, the operating system on the device behaves as if its environment has changed. The device condition remains active until you stop the device condition or disconnect the device. For example, you can start a device condition, run your app, monitor your app's energy usage, and then stop the condition.
17811798

17821799
Reference: [Test under adverse device conditions (iOS)](https://help.apple.com/xcode/mac/current/#/dev308429d42)
17831800

lib/commands/battery.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
import { isIos18OrNewer } from '../utils.js';
2+
import log from '../logger.js';
23

34
export default {
45
/**
56
* Reads the battery information from the device under test.
67
*
78
* This endpoint only returns reliable result on real devices.
89
*
9-
* @returns {Promise<BatteryInfo>} The battery info
10+
* @returns {Promise<import('./types').BatteryInfo & Partial<import('./advanced-battery-types').BatteryInfo>>}
1011
* @this {import('../driver').XCUITestDriver}
1112
*/
1213
async mobileGetBatteryInfo() {
1314
let batteryInfoFromShimService;
14-
if (isIos18OrNewer(this.opts)) {
15-
// TODO: Remove this once we publish the appium-ios-remotexpc package to npm
16-
/* eslint-disable import/no-unresolved */
17-
// @ts-expect-error
18-
const {Services} = await import('appium-ios-remotexpc');
19-
const diagService = await Services.startDiagnosticsService(this.device.udid);
20-
batteryInfoFromShimService = await diagService.ioregistry({
21-
ioClass: 'IOPMPowerSource',
22-
returnRawJson: true,
23-
});
15+
if (isIos18OrNewer(this.opts) && this.isRealDevice()) {
16+
let diagService;
17+
try {
18+
const {Services} = await import('appium-ios-remotexpc');
19+
diagService = await Services.startDiagnosticsService(this.device.udid);
20+
batteryInfoFromShimService = await diagService.ioregistry({
21+
ioClass: 'IOPMPowerSource',
22+
returnRawJson: true,
23+
});
24+
} catch (err) {
25+
log.error(`Failed to get battery info from DiagnosticsService: ${err.message}`);
26+
}
2427
}
2528

26-
const batteryInfoFromWda = /** @type {import('./battery-types.js').BatteryInfo} */ (
29+
const batteryInfoFromWda = /** @type {import('./types').BatteryInfo}} */ (
2730
await this.proxyCommand('/wda/batteryInfo', 'GET')
2831
);
29-
return {...batteryInfoFromWda, ...batteryInfoFromShimService};
32+
return {...batteryInfoFromWda, ...batteryInfoFromShimService || {}};
3033
},
3134
};
3235

3336
/**
34-
* @typedef {import('./battery-types.js').BatteryInfo} BatteryInfo
37+
* @typedef {import('./types').BatteryInfo} BatteryInfo
3538
*/

lib/commands/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ import type B from 'bluebird';
33
import type {EventEmitter} from 'node:events';
44
import type {LiteralUnion, SetOptional, SetRequired} from 'type-fest';
55
import type {Page} from '../types';
6-
import type {AuthorizationStatus, ThermalState} from './enum';
6+
import type {AuthorizationStatus, BatteryState, ThermalState} from './enum';
77

88
export type Direction = 'up' | 'down' | 'left' | 'right';
99

1010
export type LocationWithAltitude = SetRequired<Location, 'altitude'>;
1111

12+
13+
/**
14+
* Battery information. Returned by the `mobile: getBatteryInfo` execute method.
15+
*/
16+
export interface BatteryInfo {
17+
/**
18+
* Battery level in range `[0.0, 1.0]`, where `1.0` means 100% charge.
19+
*/
20+
level: number;
21+
/**
22+
* Battery state
23+
*/
24+
state: BatteryState;
25+
}
26+
27+
1228
/**
1329
* Options for `stopRecordingScreen` command
1430
*/

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"appium-ios-device": "^2.8.0",
8686
"appium-ios-simulator": "^6.2.2",
8787
"appium-remote-debugger": "^12.2.0",
88+
"appium-ios-remotexpc": "0.0.3",
8889
"appium-webdriveragent": "^9.11.0",
8990
"appium-xcode": "^5.1.4",
9091
"async-lock": "^1.4.0",

scripts/tunnel-creation.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import {logger, node} from '@appium/support';
1010
import _ from 'lodash';
1111

12-
/* eslint-disable import/no-unresolved */
13-
// TODO: Remove this once we publish the appium-ios-remotexpc package to npm
1412
import {
1513
PacketStreamServer,
1614
TunnelManager,

0 commit comments

Comments
 (0)