Skip to content

Commit

Permalink
feat: start wda process via Xctest in a real device (#687)
Browse files Browse the repository at this point in the history
* move some implementation to wda

* tune

* Update webdriveragent.js

* update review

* refactor: set started=true after getStatus

* chore: use WDA_RUNNER_BUNDLE_ID

* refactor: rename

* chore: refer to updatedWDABundleId to build xctrunner

* add fixme to not create xcodebuild instance for webDriverAgentUrl

* return a bit early

* refactor: fix review

* refactor: fix review
  • Loading branch information
KazuCocoa authored May 2, 2023
1 parent 215a7c9 commit e1c0f83
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 31 deletions.
4 changes: 3 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';

const WDA_RUNNER_BUNDLE_ID = 'com.facebook.WebDriverAgentRunner';
const WDA_RUNNER_BUNDLE_ID_FOR_XCTEST = `${WDA_RUNNER_BUNDLE_ID}.xctrunner`;
const WDA_RUNNER_APP = 'WebDriverAgentRunner-Runner.app';
const WDA_SCHEME = 'WebDriverAgentRunner';
const PROJECT_FILE = 'project.pbxproj';
Expand All @@ -17,5 +18,6 @@ const WDA_UPGRADE_TIMESTAMP_PATH = path.join('.appium', 'webdriveragent', 'upgra
export {
WDA_RUNNER_BUNDLE_ID, WDA_RUNNER_APP, PROJECT_FILE,
WDA_SCHEME, PLATFORM_NAME_TVOS, PLATFORM_NAME_IOS,
SDK_SIMULATOR, SDK_DEVICE, WDA_BASE_URL, WDA_UPGRADE_TIMESTAMP_PATH
SDK_SIMULATOR, SDK_DEVICE, WDA_BASE_URL, WDA_UPGRADE_TIMESTAMP_PATH,
WDA_RUNNER_BUNDLE_ID_FOR_XCTEST
};
122 changes: 92 additions & 30 deletions lib/webdriveragent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import AsyncLock from 'async-lock';
import { exec } from 'teen_process';
import { bundleWDASim } from './check-dependencies';
import {
WDA_RUNNER_BUNDLE_ID, WDA_RUNNER_APP, WDA_BASE_URL, WDA_UPGRADE_TIMESTAMP_PATH,
WDA_RUNNER_BUNDLE_ID, WDA_RUNNER_BUNDLE_ID_FOR_XCTEST, WDA_RUNNER_APP,
WDA_BASE_URL, WDA_UPGRADE_TIMESTAMP_PATH,
} from './constants';
import {Xctest} from 'appium-ios-device';

const WDA_LAUNCH_TIMEOUT = 60 * 1000;
const WDA_AGENT_PORT = 8100;
Expand Down Expand Up @@ -59,31 +61,47 @@ class WebDriverAgent {

this.updatedWDABundleId = args.updatedWDABundleId;

this.xcodebuild = new XcodeBuild(this.xcodeVersion, this.device, {
platformVersion: this.platformVersion,
platformName: this.platformName,
iosSdkVersion: this.iosSdkVersion,
agentPath: this.agentPath,
bootstrapPath: this.bootstrapPath,
realDevice: this.isRealDevice,
showXcodeLog: args.showXcodeLog,
xcodeConfigFile: args.xcodeConfigFile,
xcodeOrgId: args.xcodeOrgId,
xcodeSigningId: args.xcodeSigningId,
keychainPath: args.keychainPath,
keychainPassword: args.keychainPassword,
useSimpleBuildTest: args.useSimpleBuildTest,
usePrebuiltWDA: args.usePrebuiltWDA,
updatedWDABundleId: this.updatedWDABundleId,
launchTimeout: args.wdaLaunchTimeout || WDA_LAUNCH_TIMEOUT,
wdaRemotePort: this.wdaRemotePort,
useXctestrunFile: this.useXctestrunFile,
derivedDataPath: args.derivedDataPath,
mjpegServerPort: this.mjpegServerPort,
allowProvisioningDeviceRegistration: args.allowProvisioningDeviceRegistration,
resultBundlePath: args.resultBundlePath,
resultBundleVersion: args.resultBundleVersion,
}, this.log);
this.usePreinstalledWDA = args.usePreinstalledWDA;
this.xctestApiClient = null;

// FIXME: maybe 'this.webDriverAgentUrl' also can ignore
// the xcodebuild instance itself.

this.xcodebuild = this.usePreinstalledWDA
? null
: new XcodeBuild(this.xcodeVersion, this.device, {
platformVersion: this.platformVersion,
platformName: this.platformName,
iosSdkVersion: this.iosSdkVersion,
agentPath: this.agentPath,
bootstrapPath: this.bootstrapPath,
realDevice: this.isRealDevice,
showXcodeLog: args.showXcodeLog,
xcodeConfigFile: args.xcodeConfigFile,
xcodeOrgId: args.xcodeOrgId,
xcodeSigningId: args.xcodeSigningId,
keychainPath: args.keychainPath,
keychainPassword: args.keychainPassword,
useSimpleBuildTest: args.useSimpleBuildTest,
usePrebuiltWDA: args.usePrebuiltWDA,
updatedWDABundleId: this.updatedWDABundleId,
launchTimeout: args.wdaLaunchTimeout || WDA_LAUNCH_TIMEOUT,
wdaRemotePort: this.wdaRemotePort,
useXctestrunFile: this.useXctestrunFile,
derivedDataPath: args.derivedDataPath,
mjpegServerPort: this.mjpegServerPort,
allowProvisioningDeviceRegistration: args.allowProvisioningDeviceRegistration,
resultBundlePath: args.resultBundlePath,
resultBundleVersion: args.resultBundleVersion,
}, this.log);
}

/**
*
* @returns {string} Bundle ID for Xctest.
*/
get bundleIdForXctest () {
return `${this.updatedWDABundleId}.xctrunner` || WDA_RUNNER_BUNDLE_ID_FOR_XCTEST;
}

setWDAPaths (bootstrapPath, agentPath) {
Expand Down Expand Up @@ -248,13 +266,40 @@ class WebDriverAgent {
return;
}

if (this.usePreinstalledWDA) {
return;
}
try {
await this.xcodebuild.cleanProject();
} catch (e) {
this.log.warn(`Cannot perform WebDriverAgent project cleanup. Original error: ${e.message}`);
}
}

/**
* Launch WDA with preinstalled package without xcodebuild.
* @param {string} sessionId Launch WDA and establish the session with this sessionId
* @return {?object} State Object
*/
async launchWithPreinstalledWDA(sessionId) {
const xctestEnv = {
USE_PORT: this.wdaLocalPort || WDA_AGENT_PORT,
WDA_PRODUCT_BUNDLE_IDENTIFIER: this.bundleIdForXctest
};
if (this.mjpegServerPort) {
xctestEnv.MJPEG_SERVER_PORT = this.mjpegServerPort;
}
this.log.info('Launching WebDriverAgent on the device without xcodebuild');
this.xctestApiClient = new Xctest(this.device.udid, this.bundleIdForXctest, null, {env: xctestEnv});

await this.xctestApiClient.start();

this.setupProxies(sessionId);
const status = await this.getStatus();
this.started = true;
return status;
}

/**
* Return current running WDA's status like below after launching WDA
* {
Expand Down Expand Up @@ -286,6 +331,13 @@ class WebDriverAgent {
return await this.getStatus();
}

if (this.usePreinstalledWDA) {
if (this.isRealDevice) {
return await this.launchWithPreinstalledWDA(sessionId);
}
throw new Error('usePreinstalledWDA is available only for a real device.');
}

this.log.info('Launching WebDriverAgent on the device');

this.setupProxies(sessionId);
Expand Down Expand Up @@ -393,10 +445,17 @@ class WebDriverAgent {
}

async quit () {
this.log.info('Shutting down sub-processes');

await this.xcodebuild.quit();
await this.xcodebuild.reset();
if (this.usePreinstalledWDA) {
if (this.xctestApiClient) {
this.log.info('Stopping the XCTest session');
this.xctestApiClient.stop();
this.xctestApiClient = null;
}
} else {
this.log.info('Shutting down sub-processes');
await this.xcodebuild.quit();
await this.xcodebuild.reset();
}

if (this.jwproxy) {
this.jwproxy.sessionId = null;
Expand Down Expand Up @@ -437,6 +496,9 @@ class WebDriverAgent {
}

async retrieveDerivedDataPath () {
if (this.usePreinstalledWDA) {
return;
}
return await this.xcodebuild.retrieveDerivedDataPath();
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@appium/base-driver": "^9.0.0",
"@appium/support": "^3.0.0",
"@babel/runtime": "^7.0.0",
"appium-ios-device": "^2.5.0",
"appium-ios-simulator": "^5.0.1",
"async-lock": "^1.0.0",
"asyncbox": "^2.5.3",
Expand Down

0 comments on commit e1c0f83

Please sign in to comment.