appium-xcuitest-driver
Version:
Appium driver for iOS using XCUITest for backend
90 lines • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkMonitorSession = void 0;
const models_1 = require("../commands/bidi/models");
const constants_1 = require("../commands/bidi/constants");
const remotexpc_utils_1 = require("./remotexpc-utils");
/**
* Active DVT NetworkMonitor session: streams instrument events to the driver BiDi event bus.
*/
class NetworkMonitorSession {
log;
udid;
dvt = null;
runPromise = null;
stopped = false;
/**
* @param log - Logger for this session (typically the driver logger).
* @param udid - Target device UDID for `startDVTService`.
*/
constructor(log, udid) {
this.log = log;
this.udid = udid;
}
/**
* @returns `true` only while the consume loop may still be receiving events (`this.dvt` is set).
* After normal completion, error, or {@link interrupt}, this becomes `false`.
*/
isRunning() {
return this.dvt !== null;
}
/**
* Opens `startDVTService` and begins iterating `networkMonitor.events()`, emitting each payload on `eventEmitter`
* using `BIDI_EVENT_NAME` and `makeNetworkMonitorEvent` (BiDi `appium:xcuitest.networkMonitor`).
*
* @param eventEmitter - Typically the session driver's `eventEmitter` (WebDriver BiDi bus).
*/
async start(eventEmitter) {
this.stopped = false;
const Services = await (0, remotexpc_utils_1.getRemoteXPCServices)();
const dvt = await Services.startDVTService(this.udid);
this.dvt = dvt;
this.runPromise = this.consumeEvents(dvt, eventEmitter);
}
/**
* Stops monitoring: closes Remote XPC (same pattern as other `startDVTService` call sites in this driver).
* Safe to call more than once.
*/
async interrupt() {
if (this.stopped) {
return;
}
this.stopped = true;
if (this.dvt) {
const dvt = this.dvt;
this.dvt = null;
try {
await dvt.networkMonitor.stop();
}
catch (err) {
this.log.debug(`Error stopping network monitor: ${err?.message ?? err}`);
}
// Same as other `startDVTService` paths in this driver (condition inducer, terminateAppRemoteXPC):
// close RemoteXPC only. Remotexpc's DVT integration tests often call `dvtService.close()` first for
// explicit channel teardown; we can add that if shutdown issues show up in the field.
try {
await dvt.remoteXPC.close();
}
catch (err) {
this.log.debug(`Error closing RemoteXPC for network monitor: ${err?.message ?? err}`);
}
}
if (this.runPromise) {
this.runPromise = null;
}
}
async consumeEvents(dvt, eventEmitter) {
try {
for await (const event of dvt.networkMonitor.events()) {
eventEmitter.emit(constants_1.BIDI_EVENT_NAME, (0, models_1.makeNetworkMonitorEvent)(event));
}
}
catch (err) {
if (!this.stopped) {
this.log.error('Network monitor stream ended unexpectedly', err);
}
}
}
}
exports.NetworkMonitorSession = NetworkMonitorSession;
//# sourceMappingURL=network-monitor-session.js.map