2015-05-22 19:49:05 +03:00
|
|
|
import _ from 'lodash';
|
2015-05-30 04:25:13 +03:00
|
|
|
import log from './logger';
|
2018-08-28 16:42:33 +03:00
|
|
|
import { getBuildInfo, updateBuildInfo } from './config';
|
2018-03-23 09:39:15 +03:00
|
|
|
import { BaseDriver, errors, isSessionCommand } from 'appium-base-driver';
|
2015-05-30 04:25:13 +03:00
|
|
|
import { FakeDriver } from 'appium-fake-driver';
|
2015-08-11 03:25:53 +03:00
|
|
|
import { AndroidDriver } from 'appium-android-driver';
|
2015-08-20 09:47:30 +03:00
|
|
|
import { IosDriver } from 'appium-ios-driver';
|
2016-09-08 13:33:04 +03:00
|
|
|
import { AndroidUiautomator2Driver } from 'appium-uiautomator2-driver';
|
2015-11-03 19:55:01 +03:00
|
|
|
import { SelendroidDriver } from 'appium-selendroid-driver';
|
2016-07-08 20:52:08 +03:00
|
|
|
import { XCUITestDriver } from 'appium-xcuitest-driver';
|
2016-02-29 22:11:45 +03:00
|
|
|
import { YouiEngineDriver } from 'appium-youiengine-driver';
|
2016-07-15 02:07:08 +03:00
|
|
|
import { WindowsDriver } from 'appium-windows-driver';
|
2017-03-17 02:27:19 +03:00
|
|
|
import { MacDriver } from 'appium-mac-driver';
|
2017-06-09 21:03:00 +03:00
|
|
|
import { EspressoDriver } from 'appium-espresso-driver';
|
2018-06-28 16:35:23 +03:00
|
|
|
import { TizenDriver } from 'appium-tizen-driver';
|
2015-08-26 20:26:40 +03:00
|
|
|
import B from 'bluebird';
|
2017-08-21 21:16:12 +03:00
|
|
|
import AsyncLock from 'async-lock';
|
2018-06-29 19:35:02 +03:00
|
|
|
import { inspectObject, parseCapsForInnerDriver, getPackageVersion } from './utils';
|
2018-01-29 19:14:52 +03:00
|
|
|
import semver from 'semver';
|
2015-05-22 09:43:31 +03:00
|
|
|
|
2019-01-15 06:56:34 +03:00
|
|
|
const PLATFORMS = {
|
|
|
|
FAKE: 'fake',
|
|
|
|
ANDROID: 'android',
|
|
|
|
IOS: 'ios',
|
|
|
|
WINDOWS: 'windows',
|
|
|
|
MAC: 'mac',
|
|
|
|
TIZEN: 'tizen',
|
|
|
|
};
|
2018-06-29 19:35:02 +03:00
|
|
|
|
|
|
|
const AUTOMATION_NAMES = {
|
|
|
|
APPIUM: 'Appium',
|
|
|
|
SELENDROID: 'Selendroid',
|
|
|
|
UIAUTOMATOR2: 'UiAutomator2',
|
2019-01-15 06:56:34 +03:00
|
|
|
UIAUTOMATOR1: 'UiAutomator1',
|
2018-06-29 19:35:02 +03:00
|
|
|
XCUITEST: 'XCUITest',
|
|
|
|
YOUIENGINE: 'YouiEngine',
|
|
|
|
ESPRESSO: 'Espresso',
|
2018-08-27 17:21:39 +03:00
|
|
|
TIZEN: 'Tizen',
|
2018-06-29 19:35:02 +03:00
|
|
|
FAKE: 'Fake',
|
2019-01-15 06:56:34 +03:00
|
|
|
INSTRUMENTS: 'Instruments',
|
2018-06-29 19:35:02 +03:00
|
|
|
};
|
|
|
|
const DRIVER_MAP = {
|
|
|
|
SelendroidDriver: {
|
|
|
|
driverClass: SelendroidDriver,
|
|
|
|
automationName: AUTOMATION_NAMES.SELENDROID,
|
|
|
|
version: getPackageVersion('appium-selendroid-driver'),
|
|
|
|
},
|
|
|
|
AndroidUiautomator2Driver: {
|
|
|
|
driverClass: AndroidUiautomator2Driver,
|
|
|
|
automationName: AUTOMATION_NAMES.UIAUTOMATOR2,
|
|
|
|
version: getPackageVersion('appium-uiautomator2-driver'),
|
|
|
|
},
|
|
|
|
XCUITestDriver: {
|
|
|
|
driverClass: XCUITestDriver,
|
|
|
|
automationName: AUTOMATION_NAMES.XCUITEST,
|
|
|
|
version: getPackageVersion('appium-xcuitest-driver'),
|
|
|
|
},
|
|
|
|
YouiEngineDriver: {
|
|
|
|
driverClass: YouiEngineDriver,
|
|
|
|
automationName: AUTOMATION_NAMES.YOUIENGINE,
|
|
|
|
version: getPackageVersion('appium-youiengine-driver'),
|
|
|
|
},
|
|
|
|
FakeDriver: {
|
|
|
|
driverClass: FakeDriver,
|
|
|
|
version: getPackageVersion('appium-fake-driver'),
|
|
|
|
},
|
|
|
|
AndroidDriver: {
|
|
|
|
driverClass: AndroidDriver,
|
2019-01-15 06:56:34 +03:00
|
|
|
automationName: AUTOMATION_NAMES.UIAUTOMATOR1,
|
2018-06-29 19:35:02 +03:00
|
|
|
version: getPackageVersion('appium-android-driver'),
|
|
|
|
},
|
|
|
|
IosDriver: {
|
|
|
|
driverClass: IosDriver,
|
2019-01-15 06:56:34 +03:00
|
|
|
automationName: AUTOMATION_NAMES.INSTRUMENTS,
|
2018-06-29 19:35:02 +03:00
|
|
|
version: getPackageVersion('appium-ios-driver'),
|
|
|
|
},
|
|
|
|
WindowsDriver: {
|
|
|
|
driverClass: WindowsDriver,
|
|
|
|
version: getPackageVersion('appium-windows-driver'),
|
|
|
|
},
|
|
|
|
MacDriver: {
|
|
|
|
driverClass: MacDriver,
|
|
|
|
version: getPackageVersion('appium-mac-driver'),
|
|
|
|
},
|
|
|
|
EspressoDriver: {
|
|
|
|
driverClass: EspressoDriver,
|
|
|
|
automationName: AUTOMATION_NAMES.ESPRESSO,
|
|
|
|
version: getPackageVersion('appium-espresso-driver'),
|
|
|
|
},
|
|
|
|
TizenDriver: {
|
|
|
|
driverClass: TizenDriver,
|
|
|
|
automationName: AUTOMATION_NAMES.TIZEN,
|
|
|
|
version: getPackageVersion('appium-tizen-driver'),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const PLATFORMS_MAP = {
|
2019-01-15 06:56:34 +03:00
|
|
|
[PLATFORMS.FAKE]: () => FakeDriver,
|
|
|
|
[PLATFORMS.ANDROID]: (caps) => {
|
2018-06-29 19:35:02 +03:00
|
|
|
const platformVersion = semver.valid(semver.coerce(caps.platformVersion));
|
2019-01-15 06:56:34 +03:00
|
|
|
log.warn(`DeprecationWarning: 'automationName' capability was not provided. ` +
|
|
|
|
`Future versions of Appium will require 'automationName' capability to be set for Android sessions.`);
|
|
|
|
log.info(`Setting automation to '${AUTOMATION_NAMES.UIAUTOMATOR1}'. `);
|
2018-06-29 19:35:02 +03:00
|
|
|
if (platformVersion && semver.satisfies(platformVersion, '>=6.0.0')) {
|
2019-01-15 06:56:34 +03:00
|
|
|
log.warn(`Consider setting 'automationName' capability to '${AUTOMATION_NAMES.UIAUTOMATOR2}' ` +
|
|
|
|
'on Android >= 6, since UIAutomator1 framework ' +
|
|
|
|
'is not maintained anymore by the OS vendor.');
|
2018-06-29 19:35:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return AndroidDriver;
|
|
|
|
},
|
2019-01-15 06:56:34 +03:00
|
|
|
[PLATFORMS.IOS]: (caps) => {
|
2018-06-29 19:35:02 +03:00
|
|
|
const platformVersion = semver.valid(semver.coerce(caps.platformVersion));
|
2019-01-15 06:56:34 +03:00
|
|
|
log.warn(`DeprecationWarning: 'automationName' capability was not provided. ` +
|
|
|
|
`Future versions of Appium will require 'automationName' capability to be set for iOS sessions.`);
|
2018-06-29 19:35:02 +03:00
|
|
|
if (platformVersion && semver.satisfies(platformVersion, '>=10.0.0')) {
|
|
|
|
log.info("Requested iOS support with version >= 10, " +
|
|
|
|
`using '${AUTOMATION_NAMES.XCUITEST}' ` +
|
|
|
|
"driver instead of UIAutomation-based driver, since the " +
|
|
|
|
"latter is unsupported on iOS 10 and up.");
|
|
|
|
return XCUITestDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IosDriver;
|
|
|
|
},
|
2019-01-15 06:56:34 +03:00
|
|
|
[PLATFORMS.WINDOWS]: () => WindowsDriver,
|
|
|
|
[PLATFORMS.MAC]: () => MacDriver,
|
|
|
|
[PLATFORMS.TIZEN]: () => TizenDriver,
|
2018-06-29 19:35:02 +03:00
|
|
|
};
|
|
|
|
|
2018-07-05 21:29:05 +03:00
|
|
|
const desiredCapabilityConstraints = {
|
|
|
|
automationName: {
|
|
|
|
presence: false,
|
|
|
|
isString: true,
|
|
|
|
inclusionCaseInsensitive: _.values(AUTOMATION_NAMES),
|
|
|
|
},
|
|
|
|
platformName: {
|
|
|
|
presence: true,
|
|
|
|
isString: true,
|
|
|
|
inclusionCaseInsensitive: _.keys(PLATFORMS_MAP),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-08-21 21:16:12 +03:00
|
|
|
const sessionsListGuard = new AsyncLock();
|
|
|
|
const pendingDriversGuard = new AsyncLock();
|
|
|
|
|
2019-01-28 17:06:05 +03:00
|
|
|
function ensureRightCaps (opts) {
|
2019-01-29 17:09:56 +03:00
|
|
|
if(_.isObject(opts)) {
|
|
|
|
if (!process.env.XTC_DEVICE_SERIAL) {
|
|
|
|
throw new Error(`XTC_DEVICE_SERIAL is not set`);
|
|
|
|
}
|
|
|
|
opts.udid = process.env.XTC_DEVICE_SERIAL;
|
2019-01-28 17:06:05 +03:00
|
|
|
|
2019-01-29 17:09:56 +03:00
|
|
|
if (!process.env.XTC_APP_ID) {
|
|
|
|
throw new Error(`XTC_APP_ID is not set`);
|
|
|
|
}
|
|
|
|
opts.bundleId = process.env.XTC_APP_ID; // todo: consider removing from TestCloudSpoofer
|
2019-01-28 17:06:05 +03:00
|
|
|
|
2019-01-29 17:09:56 +03:00
|
|
|
if (_.isString(process.env.XTC_FORCE_APP_PATH)) {
|
|
|
|
opts.app = process.env.XTC_FORCE_APP_PATH;
|
|
|
|
}
|
2019-01-28 17:06:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureRightNSedApp (array) {
|
2019-01-29 17:09:56 +03:00
|
|
|
if (_.isString(process.env.XTC_FORCE_APP_PATH) && _.isArray(array)) {
|
2019-01-28 17:06:05 +03:00
|
|
|
array.forEach(e => { e['appium:app'] = process.env.XTC_FORCE_APP_PATH });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:34:29 +03:00
|
|
|
class AppiumDriver extends BaseDriver {
|
2015-05-22 19:49:05 +03:00
|
|
|
constructor (args) {
|
|
|
|
super();
|
2016-01-20 22:50:09 +03:00
|
|
|
|
2018-07-05 21:29:05 +03:00
|
|
|
this.desiredCapConstraints = desiredCapabilityConstraints;
|
|
|
|
|
2016-01-20 22:50:09 +03:00
|
|
|
// the main Appium Driver has no new command timeout
|
|
|
|
this.newCommandTimeoutMs = 0;
|
|
|
|
|
2017-12-13 22:49:45 +03:00
|
|
|
this.args = Object.assign({}, args);
|
2017-12-14 16:22:19 +03:00
|
|
|
|
2017-08-21 21:16:12 +03:00
|
|
|
// Access to sessions list must be guarded with a Semaphore, because
|
|
|
|
// it might be changed by other async calls at any time
|
|
|
|
// It is not recommended to access this property directly from the outside
|
2015-05-22 19:49:05 +03:00
|
|
|
this.sessions = {};
|
2017-08-01 00:16:05 +03:00
|
|
|
|
2017-08-21 21:16:12 +03:00
|
|
|
// Access to pending drivers list must be guarded with a Semaphore, because
|
|
|
|
// it might be changed by other async calls at any time
|
|
|
|
// It is not recommended to access this property directly from the outside
|
2017-08-01 00:16:05 +03:00
|
|
|
this.pendingDrivers = {};
|
2018-08-28 16:42:33 +03:00
|
|
|
|
2018-10-09 20:44:35 +03:00
|
|
|
// allow this to happen in the background, so no `await`
|
2018-08-28 16:42:33 +03:00
|
|
|
updateBuildInfo();
|
2015-05-22 19:49:05 +03:00
|
|
|
}
|
|
|
|
|
2017-08-22 16:45:24 +03:00
|
|
|
/**
|
|
|
|
* Cancel commands queueing for the umbrella Appium driver
|
|
|
|
*/
|
|
|
|
get isCommandsQueueEnabled () {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-22 19:49:05 +03:00
|
|
|
sessionExists (sessionId) {
|
2017-08-21 21:16:12 +03:00
|
|
|
const dstSession = this.sessions[sessionId];
|
|
|
|
return dstSession && dstSession.sessionId !== null;
|
2015-05-22 19:49:05 +03:00
|
|
|
}
|
|
|
|
|
2015-09-28 18:16:24 +03:00
|
|
|
driverForSession (sessionId) {
|
|
|
|
return this.sessions[sessionId];
|
|
|
|
}
|
|
|
|
|
2018-06-29 19:35:02 +03:00
|
|
|
getDriverForCaps (caps) {
|
2019-01-16 15:16:03 +03:00
|
|
|
const force_automation_name = process.env.XTC_FORCE_AUTOMATIONNAME;
|
|
|
|
if (_.isString(force_automation_name)) {
|
|
|
|
log.info(`Forcing automation name to '${force_automation_name}'`);
|
|
|
|
caps.automationName = force_automation_name;
|
2016-07-15 02:07:08 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 14:40:57 +03:00
|
|
|
const fallback_from_espresso_driver = process.env.FALLBACK_FROM_ESPRESSO_DRIVER;
|
|
|
|
if (_.isString(fallback_from_espresso_driver) && caps.automationName === AUTOMATION_NAMES.ESPRESSO) {
|
|
|
|
log.info(`Forcing automation name to '${fallback_from_espresso_driver}'`);
|
|
|
|
caps.automationName = fallback_from_espresso_driver;
|
|
|
|
}
|
|
|
|
|
2019-01-16 18:45:10 +03:00
|
|
|
const fallback_from_uiautomation2_driver = process.env.FALLBACK_FROM_UIAUTOMATOR2_DRIVER;
|
|
|
|
if (_.isString(fallback_from_uiautomation2_driver) && caps.automationName === AUTOMATION_NAMES.UIAUTOMATOR2) {
|
|
|
|
log.info(`Forcing automation name to '${fallback_from_uiautomation2_driver}'`);
|
|
|
|
caps.automationName = fallback_from_uiautomation2_driver;
|
|
|
|
}
|
|
|
|
|
2018-06-28 20:57:05 +03:00
|
|
|
// we don't necessarily have an `automationName` capability,
|
|
|
|
if (_.isString(caps.automationName)) {
|
2018-06-29 19:35:02 +03:00
|
|
|
for (const {automationName, driverClass} of _.values(DRIVER_MAP)) {
|
2018-06-28 20:57:05 +03:00
|
|
|
if (_.toLower(automationName) === caps.automationName.toLowerCase()) {
|
|
|
|
return driverClass;
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 02:27:19 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 15:16:03 +03:00
|
|
|
if (!_.isString(caps.platformName)) {
|
|
|
|
throw new Error("You must include a platformName capability");
|
|
|
|
}
|
|
|
|
|
|
|
|
const platformName = caps.platformName.toLowerCase();
|
|
|
|
|
2019-01-15 06:56:34 +03:00
|
|
|
const driverSelector = PLATFORMS_MAP[platformName];
|
2018-06-28 20:57:05 +03:00
|
|
|
if (driverSelector) {
|
2018-06-29 19:35:02 +03:00
|
|
|
return driverSelector(caps);
|
2018-06-28 16:35:23 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 20:57:05 +03:00
|
|
|
const msg = _.isString(caps.automationName)
|
|
|
|
? `Could not find a driver for automationName '${caps.automationName}' and platformName ` +
|
|
|
|
`'${caps.platformName}'.`
|
|
|
|
: `Could not find a driver for platformName '${caps.platformName}'.`;
|
2015-11-03 19:55:01 +03:00
|
|
|
throw new Error(`${msg} Please check your desired capabilities.`);
|
2015-05-30 04:25:13 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:35:02 +03:00
|
|
|
getDriverVersion (driver) {
|
|
|
|
const {version} = DRIVER_MAP[driver.name] || {};
|
|
|
|
if (version) {
|
|
|
|
return version;
|
2017-01-27 16:00:33 +03:00
|
|
|
}
|
2018-06-28 20:57:05 +03:00
|
|
|
log.warn(`Unable to get version of driver '${driver.name}'`);
|
2017-01-27 16:00:33 +03:00
|
|
|
}
|
|
|
|
|
2018-10-03 16:39:25 +03:00
|
|
|
async getStatus () { // eslint-disable-line require-await
|
2018-08-28 16:42:33 +03:00
|
|
|
return {
|
2018-10-03 16:39:25 +03:00
|
|
|
build: _.clone(getBuildInfo()),
|
2018-08-28 16:42:33 +03:00
|
|
|
};
|
2015-05-22 19:49:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async getSessions () {
|
2017-08-21 21:16:12 +03:00
|
|
|
const sessions = await sessionsListGuard.acquire(AppiumDriver.name, () => this.sessions);
|
|
|
|
return _.toPairs(sessions)
|
|
|
|
.map(([id, driver]) => {
|
|
|
|
return {id, capabilities: driver.caps};
|
|
|
|
});
|
2015-05-22 19:49:05 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:35:02 +03:00
|
|
|
printNewSessionAnnouncement (driver, caps) {
|
|
|
|
const driverVersion = this.getDriverVersion(driver);
|
2018-06-28 20:57:05 +03:00
|
|
|
const introString = driverVersion
|
|
|
|
? `Creating new ${driver.name} (v${driverVersion}) session`
|
|
|
|
: `Creating new ${driver.name} session`;
|
2017-01-27 16:00:33 +03:00
|
|
|
log.info(introString);
|
2016-01-05 20:06:57 +03:00
|
|
|
log.info('Capabilities:');
|
2017-11-06 17:21:59 +03:00
|
|
|
inspectObject(caps);
|
2017-01-27 16:00:33 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 02:13:49 +03:00
|
|
|
/**
|
|
|
|
* Create a new session
|
W3C Full Implementation (#9972)
* Refactored code from createSession into parseCapsForInnerDriver()
* parseCapsForInnerDriver() takes jsonwp and w3c capabilities and translates them into objects that can be accepted by the "inner driver" that it proxies to
* It adds defaultCapabilities to capabilities.firstMatch and jsonwp caps
* It finds matching caps for W3C and then creates a new W3C capabilities object that only has the valid matching one
* After parseCapsForInnerDriver() parses the JSONWP and W3C it passes the results along to the inner-driver and it's up to the inner driver to create a W3C or JSONWP session
* Add validation to W3C session creation
* Call to 'processCapabilities' does validation now so that it will match an object that passes Appium's validation rules (e.g.: platformName, deviceName required; automation name must be valid, etc...)
* Test that it accepts combo of W3C and JSONWP capabilities
* Test that it rejects invalid W3C capabilities with 400 error
* Also added 'createSession' unit test
* Stripped out unnecessary async/await from driver-specs
* Insert Appium prefixes into W3C caps
* Previously, Appium just took the W3C capabilities, processed them into a caps object and
passed them into the inner driver as
```
{
alwaysMatch: parsedCaps,
firstMatch: [{}],
}
```
* The problem with this is that all of the parsedCaps were unprefixed, and therefore the Inner
Driver complained about it
* Now, it takes the parsedCaps and calls a new method called 'insertAppiumPrefixes' on the
parsed caps like this:
```
{
alwaysMatch: {...insertAppiumPrefixes(parsedCaps)},
firstMatch: [{}],
}
```
* Refactored tests that were affected by this change
* Added tests to test insertAppiumPrefixes function
2018-01-16 21:26:44 +03:00
|
|
|
* @param {Object} jsonwpCaps JSONWP formatted desired capabilities
|
|
|
|
* @param {Object} reqCaps Required capabilities (JSONWP standard)
|
|
|
|
* @param {Object} w3cCapabilities W3C capabilities
|
2017-12-21 02:13:49 +03:00
|
|
|
* @return {Array} Unique session ID and capabilities
|
|
|
|
*/
|
W3C Full Implementation (#9972)
* Refactored code from createSession into parseCapsForInnerDriver()
* parseCapsForInnerDriver() takes jsonwp and w3c capabilities and translates them into objects that can be accepted by the "inner driver" that it proxies to
* It adds defaultCapabilities to capabilities.firstMatch and jsonwp caps
* It finds matching caps for W3C and then creates a new W3C capabilities object that only has the valid matching one
* After parseCapsForInnerDriver() parses the JSONWP and W3C it passes the results along to the inner-driver and it's up to the inner driver to create a W3C or JSONWP session
* Add validation to W3C session creation
* Call to 'processCapabilities' does validation now so that it will match an object that passes Appium's validation rules (e.g.: platformName, deviceName required; automation name must be valid, etc...)
* Test that it accepts combo of W3C and JSONWP capabilities
* Test that it rejects invalid W3C capabilities with 400 error
* Also added 'createSession' unit test
* Stripped out unnecessary async/await from driver-specs
* Insert Appium prefixes into W3C caps
* Previously, Appium just took the W3C capabilities, processed them into a caps object and
passed them into the inner driver as
```
{
alwaysMatch: parsedCaps,
firstMatch: [{}],
}
```
* The problem with this is that all of the parsedCaps were unprefixed, and therefore the Inner
Driver complained about it
* Now, it takes the parsedCaps and calls a new method called 'insertAppiumPrefixes' on the
parsed caps like this:
```
{
alwaysMatch: {...insertAppiumPrefixes(parsedCaps)},
firstMatch: [{}],
}
```
* Refactored tests that were affected by this change
* Added tests to test insertAppiumPrefixes function
2018-01-16 21:26:44 +03:00
|
|
|
async createSession (jsonwpCaps, reqCaps, w3cCapabilities) {
|
2018-06-29 19:35:02 +03:00
|
|
|
const {defaultCapabilities} = this.args;
|
2018-03-28 00:39:48 +03:00
|
|
|
let protocol;
|
|
|
|
let innerSessionId, dCaps;
|
W3C Full Implementation (#9972)
* Refactored code from createSession into parseCapsForInnerDriver()
* parseCapsForInnerDriver() takes jsonwp and w3c capabilities and translates them into objects that can be accepted by the "inner driver" that it proxies to
* It adds defaultCapabilities to capabilities.firstMatch and jsonwp caps
* It finds matching caps for W3C and then creates a new W3C capabilities object that only has the valid matching one
* After parseCapsForInnerDriver() parses the JSONWP and W3C it passes the results along to the inner-driver and it's up to the inner driver to create a W3C or JSONWP session
* Add validation to W3C session creation
* Call to 'processCapabilities' does validation now so that it will match an object that passes Appium's validation rules (e.g.: platformName, deviceName required; automation name must be valid, etc...)
* Test that it accepts combo of W3C and JSONWP capabilities
* Test that it rejects invalid W3C capabilities with 400 error
* Also added 'createSession' unit test
* Stripped out unnecessary async/await from driver-specs
* Insert Appium prefixes into W3C caps
* Previously, Appium just took the W3C capabilities, processed them into a caps object and
passed them into the inner driver as
```
{
alwaysMatch: parsedCaps,
firstMatch: [{}],
}
```
* The problem with this is that all of the parsedCaps were unprefixed, and therefore the Inner
Driver complained about it
* Now, it takes the parsedCaps and calls a new method called 'insertAppiumPrefixes' on the
parsed caps like this:
```
{
alwaysMatch: {...insertAppiumPrefixes(parsedCaps)},
firstMatch: [{}],
}
```
* Refactored tests that were affected by this change
* Added tests to test insertAppiumPrefixes function
2018-01-16 21:26:44 +03:00
|
|
|
|
2019-01-28 17:06:05 +03:00
|
|
|
ensureRightCaps(jsonwpCaps);
|
|
|
|
ensureRightCaps(w3cCapabilities.desiredCapabilities);
|
|
|
|
ensureRightNSedApp(w3cCapabilities.firstMatch);
|
|
|
|
|
|
|
|
|
2018-03-28 00:39:48 +03:00
|
|
|
try {
|
|
|
|
// Parse the caps into a format that the InnerDriver will accept
|
|
|
|
const parsedCaps = parseCapsForInnerDriver(
|
|
|
|
jsonwpCaps,
|
|
|
|
w3cCapabilities,
|
|
|
|
this.desiredCapConstraints,
|
|
|
|
defaultCapabilities
|
|
|
|
);
|
2018-03-23 09:39:15 +03:00
|
|
|
|
2018-03-28 00:39:48 +03:00
|
|
|
let {desiredCaps, processedJsonwpCapabilities, processedW3CCapabilities, error} = parsedCaps;
|
|
|
|
protocol = parsedCaps.protocol;
|
2018-03-23 09:39:15 +03:00
|
|
|
|
2018-03-28 00:39:48 +03:00
|
|
|
// If the parsing of the caps produced an error, throw it in here
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
2018-03-23 09:39:15 +03:00
|
|
|
|
2018-06-29 19:35:02 +03:00
|
|
|
const InnerDriver = this.getDriverForCaps(desiredCaps);
|
|
|
|
this.printNewSessionAnnouncement(InnerDriver, desiredCaps);
|
W3C Full Implementation (#9972)
* Refactored code from createSession into parseCapsForInnerDriver()
* parseCapsForInnerDriver() takes jsonwp and w3c capabilities and translates them into objects that can be accepted by the "inner driver" that it proxies to
* It adds defaultCapabilities to capabilities.firstMatch and jsonwp caps
* It finds matching caps for W3C and then creates a new W3C capabilities object that only has the valid matching one
* After parseCapsForInnerDriver() parses the JSONWP and W3C it passes the results along to the inner-driver and it's up to the inner driver to create a W3C or JSONWP session
* Add validation to W3C session creation
* Call to 'processCapabilities' does validation now so that it will match an object that passes Appium's validation rules (e.g.: platformName, deviceName required; automation name must be valid, etc...)
* Test that it accepts combo of W3C and JSONWP capabilities
* Test that it rejects invalid W3C capabilities with 400 error
* Also added 'createSession' unit test
* Stripped out unnecessary async/await from driver-specs
* Insert Appium prefixes into W3C caps
* Previously, Appium just took the W3C capabilities, processed them into a caps object and
passed them into the inner driver as
```
{
alwaysMatch: parsedCaps,
firstMatch: [{}],
}
```
* The problem with this is that all of the parsedCaps were unprefixed, and therefore the Inner
Driver complained about it
* Now, it takes the parsedCaps and calls a new method called 'insertAppiumPrefixes' on the
parsed caps like this:
```
{
alwaysMatch: {...insertAppiumPrefixes(parsedCaps)},
firstMatch: [{}],
}
```
* Refactored tests that were affected by this change
* Added tests to test insertAppiumPrefixes function
2018-01-16 21:26:44 +03:00
|
|
|
|
2018-03-28 00:39:48 +03:00
|
|
|
if (this.args.sessionOverride) {
|
|
|
|
const sessionIdsToDelete = await sessionsListGuard.acquire(AppiumDriver.name, () => _.keys(this.sessions));
|
|
|
|
if (sessionIdsToDelete.length) {
|
|
|
|
log.info(`Session override is on. Deleting other ${sessionIdsToDelete.length} active session${sessionIdsToDelete.length ? '' : 's'}.`);
|
|
|
|
try {
|
|
|
|
await B.map(sessionIdsToDelete, (id) => this.deleteSession(id));
|
|
|
|
} catch (ign) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let runningDriversData, otherPendingDriversData;
|
2018-06-29 19:35:02 +03:00
|
|
|
const d = new InnerDriver(this.args);
|
2018-03-28 00:39:48 +03:00
|
|
|
if (this.args.relaxedSecurityEnabled) {
|
2018-06-29 19:35:02 +03:00
|
|
|
log.info(`Applying relaxed security to '${InnerDriver.name}' as per server command line argument`);
|
2018-03-28 00:39:48 +03:00
|
|
|
d.relaxedSecurityEnabled = true;
|
|
|
|
}
|
2018-04-06 20:04:02 +03:00
|
|
|
// This assignment is required for correct web sockets functionality inside the driver
|
|
|
|
d.server = this.server;
|
2018-03-28 00:39:48 +03:00
|
|
|
try {
|
|
|
|
runningDriversData = await this.curSessionDataForDriver(InnerDriver);
|
|
|
|
} catch (e) {
|
|
|
|
throw new errors.SessionNotCreatedError(e.message);
|
|
|
|
}
|
2017-08-21 21:16:12 +03:00
|
|
|
await pendingDriversGuard.acquire(AppiumDriver.name, () => {
|
2018-03-28 00:39:48 +03:00
|
|
|
this.pendingDrivers[InnerDriver.name] = this.pendingDrivers[InnerDriver.name] || [];
|
|
|
|
otherPendingDriversData = this.pendingDrivers[InnerDriver.name].map((drv) => drv.driverData);
|
|
|
|
this.pendingDrivers[InnerDriver.name].push(d);
|
2017-08-21 21:16:12 +03:00
|
|
|
});
|
2015-08-26 20:26:40 +03:00
|
|
|
|
2018-03-28 00:39:48 +03:00
|
|
|
try {
|
|
|
|
[innerSessionId, dCaps] = await d.createSession(
|
|
|
|
processedJsonwpCapabilities,
|
|
|
|
reqCaps,
|
|
|
|
processedW3CCapabilities,
|
|
|
|
[...runningDriversData, ...otherPendingDriversData]
|
|
|
|
);
|
|
|
|
protocol = d.protocol;
|
|
|
|
await sessionsListGuard.acquire(AppiumDriver.name, () => {
|
|
|
|
this.sessions[innerSessionId] = d;
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
await pendingDriversGuard.acquire(AppiumDriver.name, () => {
|
|
|
|
_.pull(this.pendingDrivers[InnerDriver.name], d);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is an async function but we don't await it because it handles
|
|
|
|
// an out-of-band promise which is fulfilled if the inner driver
|
|
|
|
// unexpectedly shuts down
|
|
|
|
this.attachUnexpectedShutdownHandler(d, innerSessionId);
|
2016-12-08 01:39:03 +03:00
|
|
|
|
2016-01-20 22:50:09 +03:00
|
|
|
|
2018-03-28 00:39:48 +03:00
|
|
|
log.info(`New ${InnerDriver.name} session created successfully, session ` +
|
|
|
|
`${innerSessionId} added to master session list`);
|
|
|
|
|
|
|
|
// set the New Command Timeout for the inner driver
|
|
|
|
d.startNewCommandTimeout();
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
protocol,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
}
|
2016-01-20 22:50:09 +03:00
|
|
|
|
2018-03-28 00:39:48 +03:00
|
|
|
return {
|
|
|
|
protocol,
|
|
|
|
value: [innerSessionId, dCaps, protocol]
|
|
|
|
};
|
2015-05-22 19:49:05 +03:00
|
|
|
}
|
|
|
|
|
2016-12-08 01:39:03 +03:00
|
|
|
async attachUnexpectedShutdownHandler (driver, innerSessionId) {
|
|
|
|
// Remove the session on unexpected shutdown, so that we are in a position
|
|
|
|
// to open another session later on.
|
|
|
|
// TODO: this should be removed and replaced by a onShutdown callback.
|
|
|
|
try {
|
|
|
|
await driver.onUnexpectedShutdown; // this is a cancellable promise
|
|
|
|
// if we get here, we've had an unexpected shutdown, so error
|
|
|
|
throw new Error('Unexpected shutdown');
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof B.CancellationError) {
|
|
|
|
// if we cancelled the unexpected shutdown promise, that means we
|
|
|
|
// no longer care about it, and can safely ignore it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.warn(`Closing session, cause was '${e.message}'`);
|
|
|
|
log.info(`Removing session ${innerSessionId} from our master session list`);
|
2017-08-21 21:16:12 +03:00
|
|
|
await sessionsListGuard.acquire(AppiumDriver.name, () => {
|
|
|
|
delete this.sessions[innerSessionId];
|
|
|
|
});
|
2016-12-08 01:39:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 21:16:12 +03:00
|
|
|
async curSessionDataForDriver (InnerDriver) {
|
|
|
|
const sessions = await sessionsListGuard.acquire(AppiumDriver.name, () => this.sessions);
|
|
|
|
const data = _.values(sessions)
|
|
|
|
.filter((s) => s.constructor.name === InnerDriver.name)
|
|
|
|
.map((s) => s.driverData);
|
2015-11-10 23:56:59 +03:00
|
|
|
for (let datum of data) {
|
|
|
|
if (!datum) {
|
2015-06-04 10:00:36 +03:00
|
|
|
throw new Error(`Problem getting session data for driver type ` +
|
|
|
|
`${InnerDriver.name}; does it implement 'get ` +
|
|
|
|
`driverData'?`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
2016-08-03 22:14:29 +03:00
|
|
|
}
|
2015-06-02 20:41:04 +03:00
|
|
|
|
2015-05-22 19:49:05 +03:00
|
|
|
async deleteSession (sessionId) {
|
2018-03-28 00:39:48 +03:00
|
|
|
let protocol;
|
2015-05-30 04:25:13 +03:00
|
|
|
try {
|
2017-08-21 21:16:12 +03:00
|
|
|
let otherSessionsData = null;
|
|
|
|
let dstSession = null;
|
|
|
|
await sessionsListGuard.acquire(AppiumDriver.name, () => {
|
|
|
|
if (!this.sessions[sessionId]) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-01 00:14:56 +03:00
|
|
|
const curConstructorName = this.sessions[sessionId].constructor.name;
|
2017-08-21 21:16:12 +03:00
|
|
|
otherSessionsData = _.toPairs(this.sessions)
|
|
|
|
.filter(([key, value]) => value.constructor.name === curConstructorName && key !== sessionId)
|
|
|
|
.map(([, value]) => value.driverData);
|
|
|
|
dstSession = this.sessions[sessionId];
|
2018-03-28 00:39:48 +03:00
|
|
|
protocol = dstSession.protocol;
|
2017-08-21 21:16:12 +03:00
|
|
|
log.info(`Removing session ${sessionId} from our master session list`);
|
|
|
|
// regardless of whether the deleteSession completes successfully or not
|
|
|
|
// make the session unavailable, because who knows what state it might
|
|
|
|
// be in otherwise
|
|
|
|
delete this.sessions[sessionId];
|
|
|
|
});
|
2018-03-28 00:39:48 +03:00
|
|
|
return {
|
|
|
|
protocol,
|
|
|
|
value: await dstSession.deleteSession(sessionId, otherSessionsData),
|
|
|
|
};
|
2015-05-30 04:25:13 +03:00
|
|
|
} catch (e) {
|
|
|
|
log.error(`Had trouble ending session ${sessionId}: ${e.message}`);
|
2018-03-28 00:39:48 +03:00
|
|
|
return {
|
|
|
|
protocol,
|
|
|
|
error: e,
|
|
|
|
};
|
2015-05-30 04:25:13 +03:00
|
|
|
}
|
2015-05-22 19:49:05 +03:00
|
|
|
}
|
|
|
|
|
2015-08-24 19:12:58 +03:00
|
|
|
async executeCommand (cmd, ...args) {
|
2017-07-12 14:42:03 +03:00
|
|
|
// getStatus command should not be put into queue. If we do it as part of super.executeCommand, it will be added to queue.
|
|
|
|
// There will be lot of status commands in queue during createSession command, as createSession can take up to or more than a minute.
|
|
|
|
if (cmd === 'getStatus') {
|
|
|
|
return await this.getStatus();
|
|
|
|
}
|
2018-03-28 00:39:48 +03:00
|
|
|
|
2015-06-02 20:41:04 +03:00
|
|
|
if (isAppiumDriverCommand(cmd)) {
|
2017-10-30 21:42:26 +03:00
|
|
|
return await super.executeCommand(cmd, ...args);
|
2015-05-22 19:49:05 +03:00
|
|
|
}
|
2015-08-26 13:16:37 +03:00
|
|
|
|
2017-08-21 21:16:12 +03:00
|
|
|
const sessionId = _.last(args);
|
|
|
|
const dstSession = await sessionsListGuard.acquire(AppiumDriver.name, () => this.sessions[sessionId]);
|
|
|
|
if (!dstSession) {
|
|
|
|
throw new Error(`The session with id '${sessionId}' does not exist`);
|
|
|
|
}
|
2018-03-28 00:39:48 +03:00
|
|
|
|
|
|
|
let res = {
|
|
|
|
protocol: dstSession.protocol
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
res.value = await dstSession.executeCommand(cmd, ...args);
|
|
|
|
} catch (e) {
|
|
|
|
res.error = e;
|
|
|
|
}
|
|
|
|
return res;
|
2015-05-22 09:43:31 +03:00
|
|
|
}
|
2015-10-29 20:41:33 +03:00
|
|
|
|
|
|
|
proxyActive (sessionId) {
|
2017-08-21 21:16:12 +03:00
|
|
|
const dstSession = this.sessions[sessionId];
|
|
|
|
return dstSession && _.isFunction(dstSession.proxyActive) && dstSession.proxyActive(sessionId);
|
2015-10-29 20:41:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getProxyAvoidList (sessionId) {
|
2017-08-21 21:16:12 +03:00
|
|
|
const dstSession = this.sessions[sessionId];
|
|
|
|
return dstSession ? dstSession.getProxyAvoidList() : [];
|
2015-10-29 20:41:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
canProxy (sessionId) {
|
2017-08-21 21:16:12 +03:00
|
|
|
const dstSession = this.sessions[sessionId];
|
|
|
|
return dstSession && dstSession.canProxy(sessionId);
|
2015-10-29 20:41:33 +03:00
|
|
|
}
|
2015-05-22 09:43:31 +03:00
|
|
|
}
|
|
|
|
|
2015-06-02 20:41:04 +03:00
|
|
|
// help decide which commands should be proxied to sub-drivers and which
|
|
|
|
// should be handled by this, our umbrella driver
|
|
|
|
function isAppiumDriverCommand (cmd) {
|
|
|
|
return !isSessionCommand(cmd) || cmd === "deleteSession";
|
|
|
|
}
|
|
|
|
|
2018-03-23 09:39:15 +03:00
|
|
|
export { AppiumDriver };
|