Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: uninstall wda with '.xctrunner' suffix for real device #1052

Merged
merged 20 commits into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ class XCUITestDriver extends BaseDriver {
await this.wda.quitAndUninstall();
this.logEvent('wdaUninstalled');
} else if (!util.hasValue(this.wda.webDriverAgentUrl)) {
await this.wda.setupCaching(this.opts.updatedWDABundleId);
await this.wda.setupCaching();
}

// local helper for the two places we need to uninstall wda and re-start it
Expand Down
34 changes: 31 additions & 3 deletions lib/real-device-management.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { utilities } from 'appium-ios-device';
import { utilities, services } from 'appium-ios-device';
import IOSDeploy from './ios-deploy';
import log from './logger';

import _ from 'lodash';

async function getConnectedDevices () {
return await utilities.getConnectedDevices();
Expand Down Expand Up @@ -66,5 +66,33 @@ function getRealDeviceObj (udid) {
return new IOSDeploy(udid);
}

/**
* @typedef {Object} RealDevice
* @param {string} udid The udid of the target device
*/

/**
* @param {RealDevice} device The real device object which has 'udid' attribute
* @param {string} bundleName The name of CFBundleName in Info.plist
*
* @returns {Array<string>} A list of User level apps' bundle ids which has
* 'CFBundleName' attribute as 'bundleName'.
*/
async function getBundleIdsByBundleName (device, bundleName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, lets move this method into IOSDeploy class

const service = await services.startInstallationProxyService(device.udid);
try {
const applications = await service.listApplications({applicationType: 'User'});
return _.reduce(applications, (acc, {CFBundleName}, key) => {
if (CFBundleName === bundleName) {
acc.push(key);
}
return acc;
}, []);
} finally {
service.close();
}
}


export { getConnectedDevices, getOSVersion, runRealDeviceReset, installToRealDevice,
getRealDeviceObj };
getRealDeviceObj, getBundleIdsByBundleName };
7 changes: 6 additions & 1 deletion lib/simulator-management.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import B from 'bluebird';
import path from 'path';
import { getSimulator } from 'appium-ios-simulator';
import { createDevice, getDevices, terminate, shutdown } from 'node-simctl';
Expand Down Expand Up @@ -214,5 +215,9 @@ async function shutdownOtherSimulators (currentDevice) {
}
}

async function getInstalledBundleIds (device, candidateBundleIds) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we actually change this in appium-ios-simulator that we get a similar api like what the real devices have? This would help us get rid of having a list of potential bundle ids

Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Aug 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can either add this call to ios simulator or we can attach it to the object dynamically right in this module:

this.device.getInstalledBundleIds = getInstalledBundleIds.bind(this.device);

Then this context in this method will be pointing to device instance if called from this.device.getInstalledBundleIds(...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm talking about something else. I'm talking about making this method similar as the getBundleIdsByBundleName (device, bundleName)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^
This one as well. Otherwise how can it be polymorphic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is an oneliner then we could simply move it to getInstalledWDBundleId

return await B.filter(candidateBundleIds, async (bundleId) => await device.isAppInstalled(bundleId));
}

export { createSim, getExistingSim, runSimulatorReset, installToSimulator,
shutdownSimulator, shutdownOtherSimulators };
shutdownSimulator, shutdownOtherSimulators, getInstalledBundleIds };
1 change: 0 additions & 1 deletion lib/wda/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ function getXctestrunFileName (deviceInfo, version) {
? `WebDriverAgentRunner_tvOS_appletv${deviceInfo.isRealDevice ? `os${version}-arm64` : `simulator${version}-x86_64`}.xctestrun`
: `WebDriverAgentRunner_iphone${deviceInfo.isRealDevice ? `os${version}-arm64` : `simulator${version}-x86_64`}.xctestrun`;
}

async function killProcess (name, proc) {
if (proc && proc.proc) {
log.info(`Shutting down ${name} process (pid ${proc.proc.pid})`);
Expand Down
61 changes: 49 additions & 12 deletions lib/wda/webdriveragent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import B from 'bluebird';
import _ from 'lodash';
import path from 'path';
import url from 'url';
Expand All @@ -12,11 +13,14 @@ import iProxy from './iproxy';
import { exec } from 'teen_process';
import AsyncLock from 'async-lock';
import { BOOTSTRAP_PATH, WDA_BUNDLE_ID, WDA_RUNNER_BUNDLE_ID, checkForDependencies } from 'appium-webdriveragent';

import { getBundleIdsByBundleName } from '../real-device-management';
import { getInstalledBundleIds } from '../simulator-management';

const WDA_LAUNCH_TIMEOUT = 60 * 1000;
const WDA_AGENT_PORT = 8100;
const WDA_BASE_URL = 'http://localhost';
const WDA_CF_BUNDLE_NAME = 'WebDriverAgentRunner-Runner';
const WDA_BUNDLE_ID_XCODE11 = 'com.facebook.WebDriverAgentRunner.xctrunner'; // FIXME: Move to appium-webdriveragent

const SHARED_RESOURCES_GUARD = new AsyncLock();

Expand All @@ -32,7 +36,7 @@ class WebDriverAgent {
this.platformName = args.platformName;
this.iosSdkVersion = args.iosSdkVersion;
this.host = args.host;
this.realDevice = !!args.realDevice;
this.isRealDevice = !!args.realDevice;

this.setWDAPaths(args.bootstrapPath, args.agentPath);

Expand All @@ -53,13 +57,15 @@ class WebDriverAgent {
this.usePrebuiltWDA = args.usePrebuiltWDA;
this.derivedDataPath = args.derivedDataPath;

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.realDevice,
realDevice: this.isRealDevice,
showXcodeLog: args.showXcodeLog,
xcodeConfigFile: args.xcodeConfigFile,
xcodeOrgId: args.xcodeOrgId,
Expand All @@ -68,9 +74,9 @@ class WebDriverAgent {
keychainPassword: args.keychainPassword,
useSimpleBuildTest: args.useSimpleBuildTest,
usePrebuiltWDA: args.usePrebuiltWDA,
updatedWDABundleId: args.updatedWDABundleId,
updatedWDABundleId: this.updatedWDABundleId,
launchTimeout: args.wdaLaunchTimeout || WDA_LAUNCH_TIMEOUT,
wdaRemotePort: this.realDevice ? WDA_AGENT_PORT : (this.wdaLocalPort || WDA_AGENT_PORT),
wdaRemotePort: this.isRealDevice ? WDA_AGENT_PORT : (this.wdaLocalPort || WDA_AGENT_PORT),
useXctestrunFile: this.useXctestrunFile,
derivedDataPath: args.derivedDataPath,
mjpegServerPort: args.mjpegServerPort,
Expand Down Expand Up @@ -155,10 +161,23 @@ class WebDriverAgent {
}
}

/**
* Uninstall WDAs from the test device.
* Over Xcode 11, multiple WDA can be in the device since Xcode 11 generates different WDA.
* Appium does not expect multiple WDAs are running on a device.
*/
async uninstall () {
log.debug(`Removing WDA application from device`);
try {
await this.device.removeApp(WDA_BUNDLE_ID);
const bundleIds = await this.getInstalledWDBundleIds();
if (_.isEmpty(bundleIds)) {
log.debug('No WDAs on the device.');
return;
}

log.debug(`Uninstalling WDAs: '${bundleIds}'`);
await B.each(bundleIds, async (bundleId) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here I would revert back to for of. each call does not provide any preferences over a normal loop

await this.device.removeApp(bundleId);
});
} catch (e) {
log.warn(`WebDriverAgent uninstall failed. Perhaps, it is already uninstalled? Original error: ${JSON.stringify(e)}`);
}
Expand Down Expand Up @@ -221,7 +240,7 @@ class WebDriverAgent {
});
}
// We need to provide WDA local port, because it might be occupied with
await resetXCTestProcesses(this.device.udid, !this.realDevice);
await resetXCTestProcesses(this.device.udid, !this.isRealDevice);

await this.ensureConnection();

Expand Down Expand Up @@ -278,7 +297,7 @@ class WebDriverAgent {
}

async ensureConnection () {
if (!this.realDevice || this.webDriverAgentUrl || this.iproxy) {
if (!this.isRealDevice || this.webDriverAgentUrl || this.iproxy) {
return;
}
if (isLocalHost(this.wdaBaseUrl)) {
Expand Down Expand Up @@ -349,7 +368,7 @@ class WebDriverAgent {
*
* @param {string} updatedWDABundleId BundleId you'd like to use
*/
async setupCaching (updatedWDABundleId) {
async setupCaching () {
const status = await this.getStatus();
if (!status || !status.build) {
log.debug('WDA is currently not running. There is nothing to cache');
Expand All @@ -360,11 +379,13 @@ class WebDriverAgent {
productBundleIdentifier,
upgradedAt,
} = status.build;
if (util.hasValue(productBundleIdentifier) && util.hasValue(updatedWDABundleId) && updatedWDABundleId !== productBundleIdentifier) {
// for real device
if (util.hasValue(productBundleIdentifier) && util.hasValue(this.updatedWDABundleId) && this.updatedWDABundleId !== productBundleIdentifier) {
log.info(`Will uninstall running WDA since it has different bundle id. The actual value is '${productBundleIdentifier}'.`);
return await this.uninstall();
}
if (util.hasValue(productBundleIdentifier) && !util.hasValue(updatedWDABundleId) && WDA_RUNNER_BUNDLE_ID !== productBundleIdentifier) {
// for simulator
if (util.hasValue(productBundleIdentifier) && !util.hasValue(this.updatedWDABundleId) && WDA_RUNNER_BUNDLE_ID !== productBundleIdentifier) {
log.info(`Will uninstall running WDA since its bundle id is not equal to the default value ${WDA_RUNNER_BUNDLE_ID}`);
return await this.uninstall();
}
Expand All @@ -385,10 +406,26 @@ class WebDriverAgent {
this.webDriverAgentUrl = this.url.href;
}

/**
* Quit and uninstall running WDA.
*/
async quitAndUninstall () {
await this.quit();
await this.uninstall();
}

/**
* Get WDA bundleIds installed in the target device.
* Over Xcode 11, multiple WDA can be in a device since Xcode 11
* set their bundleIds as 'xxxxxx.xctrunner'.
*
* @returns {Array<string>} A list of User level apps' bundle ids.
*/
async getInstalledWDBundleIds () {
return this.isRealDevice
? await getBundleIdsByBundleName(this.device, WDA_CF_BUNDLE_NAME)
: await getInstalledBundleIds(this.device, [WDA_BUNDLE_ID, WDA_BUNDLE_ID_XCODE11]);
}
}

export default WebDriverAgent;
Expand Down
20 changes: 11 additions & 9 deletions test/unit/webdriveragent-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,30 @@ describe('setupCaching()', function () {
});

it('should call uninstall once since bundle id is different with updatedWDABundleId capability', async function () {
const updatedWDABundleId = 'com.example.WebDriverAgent';
wdaStub.callsFake(function () {
return {build: { time: 'Jun 24 2018 17:08:21', productBundleIdentifier: 'com.example.different.WebDriverAgent' }};
});

wdaStubUninstall.callsFake(_.noop);

await wda.setupCaching(updatedWDABundleId);
await wda.setupCaching();
wdaStub.calledOnce.should.be.true;
wdaStubUninstall.calledOnce.should.be.true;
_.isUndefined(wda.webDriverAgentUrl).should.be.true;
});

it('should not call uninstall since bundle id is equal to updatedWDABundleId capability', async function () {
const updatedWDABundleId = 'com.example.WebDriverAgent';
wda = new WebDriverAgent('1', { updatedWDABundleId: 'com.example.WebDriverAgent' });
wdaStub = sinon.stub(wda, 'getStatus');
wdaStubUninstall = sinon.stub(wda, 'uninstall');

wdaStub.callsFake(function () {
return {build: { time: 'Jun 24 2018 17:08:21', productBundleIdentifier: 'com.example.WebDriverAgent' }};
});

wdaStubUninstall.callsFake(_.noop);

await wda.setupCaching(updatedWDABundleId);
await wda.setupCaching();
wdaStub.calledOnce.should.be.true;
wdaStubUninstall.notCalled.should.be.true;
wda.webDriverAgentUrl.should.equal('http://localhost:8100/');
Expand All @@ -200,7 +202,7 @@ describe('setupCaching()', function () {
getTimestampStub.callsFake(() => '2');
wdaStubUninstall.callsFake(_.noop);

await wda.setupCaching('something');
await wda.setupCaching();
wdaStub.calledOnce.should.be.true;
wdaStubUninstall.calledOnce.should.be.true;
});
Expand All @@ -212,7 +214,7 @@ describe('setupCaching()', function () {
getTimestampStub.callsFake(() => '1');
wdaStubUninstall.callsFake(_.noop);

await wda.setupCaching('something');
await wda.setupCaching();
wdaStub.calledOnce.should.be.true;
wdaStubUninstall.notCalled.should.be.true;
});
Expand All @@ -224,7 +226,7 @@ describe('setupCaching()', function () {
getTimestampStub.callsFake(() => '1');
wdaStubUninstall.callsFake(_.noop);

await wda.setupCaching('something');
await wda.setupCaching();
wdaStub.calledOnce.should.be.true;
wdaStubUninstall.notCalled.should.be.true;
});
Expand All @@ -236,8 +238,8 @@ describe('setupCaching()', function () {
getTimestampStub.callsFake(() => null);
wdaStubUninstall.callsFake(_.noop);

await wda.setupCaching('something');
await wda.setupCaching();
wdaStub.calledOnce.should.be.true;
wdaStubUninstall.notCalled.should.be.true;
});
});
});