From bd60874bd800134bd1225902332f237aa5503af4 Mon Sep 17 00:00:00 2001 From: saikrishna321 Date: Mon, 20 Jan 2025 22:17:40 +0530 Subject: [PATCH] Fixed review comments Co-authored-by: SrinivasanTarget --- lib/commands/recordscreen.js | 2 +- lib/driver.js | 22 ++++++++++++++++------ test/unit/driver-specs.js | 23 ++++++++++++++++++++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/commands/recordscreen.js b/lib/commands/recordscreen.js index 108dff973..4b0e15b73 100644 --- a/lib/commands/recordscreen.js +++ b/lib/commands/recordscreen.js @@ -1,7 +1,7 @@ import _ from 'lodash'; import {fs, tempDir, logger, util} from 'appium/support'; import {SubProcess} from 'teen_process'; -import {encodeBase64OrUpload, isLocalHost} from '../utils'; +import {encodeBase64OrUpload} from '../utils'; import {WDA_BASE_URL} from 'appium-webdriveragent'; import {waitForCondition} from 'asyncbox'; import url from 'url'; diff --git a/lib/driver.js b/lib/driver.js index 557ac15ea..4953ac867 100644 --- a/lib/driver.js +++ b/lib/driver.js @@ -482,6 +482,10 @@ export class XCUITestDriver extends BaseDriver { } } + /** + * Handles MJPEG server-related capabilities + * @returns {Promise} + */ async handleMjpegOptions() { await this.allocateMjpegServerPort(); // turn on mjpeg stream reading if requested @@ -502,12 +506,18 @@ export class XCUITestDriver extends BaseDriver { devicePort: mjpegServerPort, usePortForwarding: this.isRealDevice(), }); - } catch (error) { - throw new Error( - `Cannot ensure MJPEG broadcast functionality by forwarding the local port ${mjpegServerPort} ` + - `requested by the 'mjpegServerPort' capability to the device port ${mjpegServerPort}. ` + - `Original error: ${error}` - ); + } catch (error) { + if (_.isUndefined(this.opts.mjpegServerPort)) { + this.log.warn(`Cannot ensure MJPEG broadcast functionality by forwarding the ${DEFAULT_MJPEG_SERVER_PORT} ` + + `use mjpegServerPort capability to provide alternative port number` + ); + } else { + throw new Error( + `Cannot ensure MJPEG broadcast functionality by forwarding the local port ${mjpegServerPort} ` + + `requested by the 'mjpegServerPort' capability to the device port ${mjpegServerPort}. ` + + `Original error: ${error}` + ); + } } } diff --git a/test/unit/driver-specs.js b/test/unit/driver-specs.js index 47dfde8d0..1b55323bb 100644 --- a/test/unit/driver-specs.js +++ b/test/unit/driver-specs.js @@ -8,7 +8,6 @@ import * as utils from '../../lib/utils'; import {MOCHA_LONG_TIMEOUT} from './helpers'; import {RealDevice} from '../../lib/real-device'; - const caps = { fistMatch: [{}], alwaysMatch: { @@ -244,6 +243,28 @@ describe('XCUITestDriver', function () { ); spy.notCalled.should.be.true; }); + + it('should throw an error if mjpegServerPort is occupied', async function () { + this.timeout(MOCHA_LONG_TIMEOUT); + delete device.simctl; + device.devicectl = true; + const net = require('net'); + const server = net.createServer(); + await new Promise((resolve) => server.listen(9100, resolve)); + try { + await driver.createSession( + null, + null, + _.merge({}, caps, { + alwaysMatch: {'appium:mjpegServerPort': 9100}, + }), + ).should.be.rejectedWith( + 'Cannot ensure MJPEG broadcast functionality by forwarding the local port 9100 requested by the \'mjpegServerPort\' capability to the device port 9100. Original error: Error: The port #9100 is occupied by an other process' + ); + } finally { + await new Promise((resolve) => server.close(resolve)); + } + }); }); describe('execute', function () {