appium-xcuitest-driver
Version:
Appium driver for iOS using XCUITest for backend
105 lines • 3.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.proxyCommand = proxyCommand;
const driver_1 = require("appium/driver");
const bluebird_1 = __importDefault(require("bluebird"));
const GET = 'GET';
const POST = 'POST';
const DELETE = 'DELETE';
const SUPPORTED_METHODS = Object.freeze(new Set([GET, POST, DELETE]));
const WDA_ROUTES = {
'/wda/screen': {
GET: 'getScreenInfo',
},
'/wda/alert/buttons': {
GET: 'getAlertButtons',
},
'/wda/apps/launch': {
POST: 'mobileLaunchApp',
},
'/wda/apps/terminate': {
POST: 'mobileTerminateApp',
},
'/wda/apps/activate': {
POST: 'mobileActivateApp',
},
'/wda/apps/state': {
POST: 'mobileQueryAppState',
},
'/wda/keys': {
POST: 'keys',
},
'/wda/touch_id': {
POST: 'touchId',
},
'/wda/keyboard/dismiss': {
POST: 'hideKeyboard',
},
'/wda/lock': {
POST: 'lock',
},
'/wda/unlock': {
POST: 'unlock',
},
'/wda/locked': {
GET: 'isLocked',
},
};
/**
* Proxies a command to WebDriverAgent
*
* @template TReq - Request body type
* @template TRes - Response type
* @param url - The endpoint URL
* @param method - HTTP method to use
* @param body - Optional request body
* @param isSessionCommand - Whether this is a session command (default: true)
* @returns Promise resolving to the response
*/
async function proxyCommand(url, method, body, isSessionCommand = true) {
if (this.shutdownUnexpectedly) {
return undefined;
}
if (!url) {
throw this.log.errorWithException('Proxying requires an endpoint');
}
else if (!SUPPORTED_METHODS.has(method)) {
throw this.log.errorWithException(`Proxying only works for the following HTTP methods: ${[...SUPPORTED_METHODS].join(', ')}`);
}
const proxy = isSessionCommand ? this.wda.jwproxy : this.wda.noSessionProxy;
if (!proxy) {
throw new Error('Cannot call proxyCommand without WDA proxy active');
}
let cmdName = wdaRouteToCommandName(url, method) || (0, driver_1.routeToCommandName)(url, method);
const timeout = this._getCommandTimeout(cmdName);
if (!cmdName) {
// this should never happen except when adding new routes
cmdName = 'Unknown'; // just for logging purposes below
this.log.info(`Proxying to WDA with an unknown route: ${method} ${url}`);
}
if (!timeout) {
return await proxy.command(url, method, body);
}
this.log.debug(`Setting custom timeout to ${timeout} ms for '${cmdName}' command`);
try {
return await bluebird_1.default.resolve(proxy.command(url, method, body)).timeout(timeout);
}
catch (e) {
if (!(e instanceof bluebird_1.default.Promise.TimeoutError)) {
throw e;
}
proxy.cancelActiveRequests();
const error = new driver_1.errors.TimeoutError(`Appium did not get any response from '${cmdName}' command in ${timeout} ms`);
await this.startUnexpectedShutdown(error);
throw error;
}
}
function wdaRouteToCommandName(endpoint, method) {
if (endpoint in WDA_ROUTES) {
return WDA_ROUTES[endpoint]?.[method];
}
}
//# sourceMappingURL=proxy-helper.js.map