How to use this.sim.simctl.spawnSubProcess method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Sign up Free
_

ios-simulator-log.js

Source: ios-simulator-log.js Github

copy
1import _ from 'lodash';
2import { IOSLog } from './ios-log';
3import { logger } from '@appium/support';
4import { exec } from 'teen_process';
5
6const log = logger.getLogger('IOSSimulatorLog');
7
8const START_TIMEOUT = 10000;
9
10class IOSSimulatorLog extends IOSLog {
11  constructor ({sim, showLogs, xcodeVersion, iosSimulatorLogsPredicate}) {
12    super();
13    this.sim = sim;
14    this.showLogs = !!showLogs;
15    this.xcodeVersion = xcodeVersion;
16    this.predicate = iosSimulatorLogsPredicate;
17    this.proc = null;
18  }
19
20  async startCapture () {
21    if (_.isUndefined(this.sim.udid)) {
22      throw new Error(`Log capture requires a sim udid`);
23    }
24
25    if (!await this.sim.isRunning()) {
26      throw new Error(`iOS Simulator with udid '${this.sim.udid}' is not running`);
27    }
28    const spawnArgs = [
29      'log',
30      'stream',
31      '--style', 'compact',
32    ];
33    if (this.predicate) {
34      spawnArgs.push('--predicate', this.predicate);
35    }
36    log.debug(`Starting log capture for iOS Simulator with udid '${this.sim.udid}' ` +
37      `using simctl`);
38    try {
39      // cleanup existing listeners if the previous session has not been terminated properly
40      await exec('pkill', ['-f', [this.sim.udid, ...spawnArgs].join(' ')]);
41    } catch (ign) {}
42    try {
43      this.proc = await this.sim.simctl.spawnSubProcess(spawnArgs);
44      await this.finishStartingLogCapture();
45    } catch (e) {
46      throw new Error(`Simulator log capture failed. Original error: ${e.message}`);
47    }
48  }
49
50  async finishStartingLogCapture () {
51    if (!this.proc) {
52      log.errorAndThrow('Could not capture simulator log');
53    }
54    let firstLine = true;
55    let logRow = '';
56    this.proc.on('output', (stdout, stderr) => {
57      if (stdout) {
58        if (firstLine) {
59          if (stdout.endsWith('\n')) {
60            // don't store the first line of the log because it came before the sim was launched
61            firstLine = false;
62          }
63        } else {
64          logRow += stdout;
65          if (stdout.endsWith('\n')) {
66            this.onOutput(logRow);
67            logRow = '';
68          }
69        }
70      }
71      if (stderr) {
72        this.onOutput(logRow, 'STDERR');
73      }
74    });
75
76    let sd = (stdout, stderr) => {
77      if (/execvp\(\)/.test(stderr)) {
78        throw new Error('iOS log capture process failed to start');
79      }
80      return stdout || stderr;
81    };
82    await this.proc.start(sd, START_TIMEOUT);
83  }
84
85  async stopCapture () {
86    if (!this.proc) {
87      return;
88    }
89    await this.killLogSubProcess();
90    this.proc = null;
91  }
92
93  async killLogSubProcess () {
94    if (!this.proc.isRunning) {
95      return;
96    }
97    log.debug('Stopping iOS log capture');
98    try {
99      await this.proc.stop('SIGTERM', 1000);
100    } catch (e) {
101      if (!this.proc.isRunning) {
102        return;
103      }
104      log.warn('Cannot stop log capture process. Sending SIGKILL');
105      await this.proc.stop('SIGKILL');
106    }
107  }
108
109  get isCapturing () {
110    return this.proc && this.proc.isRunning;
111  }
112
113  onOutput (logRow, prefix = '') {
114    const logs = _.cloneDeep(logRow.split('\n'));
115    for (const logLine of logs) {
116      if (!logLine) continue; // eslint-disable-line curly
117      this.broadcast(logLine);
118      if (this.showLogs) {
119        const space = prefix.length > 0 ? ' ' : '';
120        log.info(`[IOS_SYSLOG_ROW${space}${prefix}] ${logLine}`);
121      }
122    }
123  }
124}
125
126export { IOSSimulatorLog };
127export default IOSSimulatorLog;
128
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Xcuitest Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)