Skip to content

Commit

Permalink
chore: Remove obsolete driver options (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Oct 17, 2023
1 parent eaf5b7e commit 2a296dc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 74 deletions.
94 changes: 32 additions & 62 deletions lib/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import {executeMethodMap} from './execute-method-map';
import {APKS_EXTENSION, APK_EXTENSION} from './extensions';
import uiautomator2Helpers from './helpers';
import {newMethodMap} from './method-map';
import type { EmptyObject } from 'type-fest';
import type {
Uiautomator2Settings,
Uiautomator2DeviceDetails,
Uiautomator2DeviceInfo,
Uiautomator2DriverCaps,
Uiautomator2DriverOpts,
Uiautomator2SessionCaps,
Expand Down Expand Up @@ -278,11 +278,6 @@ class AndroidUiautomator2Driver
this.log.info(`Chrome-type package and activity are ${pkg} and ${activity}`);
}

// @ts-expect-error FIXME: missing CLI option?
if (this.opts.reboot) {
this.setAvdFromCapabilities(startSessionOpts);
}

if (this.opts.app) {
// find and copy, or download and unzip an app url or path
this.opts.app = await this.helpers.configureApp(this.opts.app, [
Expand Down Expand Up @@ -317,12 +312,29 @@ class AndroidUiautomator2Driver
}

async getDeviceDetails(): Promise<Uiautomator2DeviceDetails> {
const [pixelRatio, statBarHeight, viewportRect] = await B.all([
const [
pixelRatio,
statBarHeight,
viewportRect,
{apiVersion, platformVersion, manufacturer, model, realDisplaySize, displayDensity},
] = await B.all([
this.getDevicePixelRatio(),
this.getStatusBarHeight(),
this.getViewPortRect(),
this.mobileGetDeviceInfo(),
]);
return {pixelRatio, statBarHeight, viewportRect};

return {
pixelRatio,
statBarHeight,
viewportRect,
deviceApiLevel: _.parseInt(apiVersion),
platformVersion,
deviceManufacturer: manufacturer,
deviceModel: model,
deviceScreenSize: realDisplaySize,
deviceScreenDensity: displayDensity,
};
}

override get driverData() {
Expand All @@ -337,27 +349,6 @@ class AndroidUiautomator2Driver
return {...sessionData, ...uia2Data};
}

setAvdFromCapabilities(caps: Uiautomator2StartSessionOpts) {
if (this.opts.avd) {
this.log.info('avd name defined, ignoring device name and platform version');
} else {
if (!caps.deviceName) {
this.log.errorAndThrow(
'avd or deviceName should be specified when reboot option is enables'
);
throw new Error(); // unreachable
}
if (!caps.platformVersion) {
this.log.errorAndThrow(
'avd or platformVersion should be specified when reboot option is enabled'
);
throw new Error(); // unreachable
}
const avdDevice = caps.deviceName.replace(/[^a-zA-Z0-9_.]/g, '-');
this.opts.avd = `${avdDevice}__${caps.platformVersion}`;
}
}

async allocateSystemPort() {
const forwardPort = async (localPort: number) => {
this.log.debug(
Expand Down Expand Up @@ -534,11 +525,18 @@ class AndroidUiautomator2Driver

// launch UiAutomator2 and wait till its online and we have a session
await uiautomator2.startSession(capsWithSessionInfo);
// now that everything has started successfully, turn on proxying so all
// subsequent session requests go straight to/from uiautomator2
this.jwpProxyActive = true;

const capsWithSessionAndDeviceInfo = {
...capsWithSessionInfo,
...(await this.getDeviceInfoFromUia2()),
};
const deviceInfoPromise: Promise<Uiautomator2DeviceDetails|EmptyObject> = (async () => {
try {
return await this.getDeviceDetails();
} catch (e) {
this.log.warn(`Cannot fetch device details. Original error: ${e.message}`);
return {};
}
})();

// Unlock the device after the session is started.
if (!this.opts.skipUnlock) {
Expand Down Expand Up @@ -570,24 +568,7 @@ class AndroidUiautomator2Driver
await retryInterval(timeout / 500, 500, this.setContext.bind(this), viewName);
}

// now that everything has started successfully, turn on proxying so all
// subsequent session requests go straight to/from uiautomator2
this.jwpProxyActive = true;

return {...capsWithSessionAndDeviceInfo, ...(await this.getDeviceDetails())};
}

async getDeviceInfoFromUia2(): Promise<Uiautomator2DeviceInfo> {
const {apiVersion, platformVersion, manufacturer, model, realDisplaySize, displayDensity} =
await this.mobileGetDeviceInfo();
return {
deviceApiLevel: _.parseInt(apiVersion),
platformVersion,
deviceManufacturer: manufacturer,
deviceModel: model,
deviceScreenSize: realDisplaySize,
deviceScreenDensity: displayDensity,
};
return {...capsWithSessionInfo, ...(await deviceInfoPromise)};
}

async initUiAutomator2Server() {
Expand Down Expand Up @@ -854,17 +835,6 @@ class AndroidUiautomator2Driver
this.log.info('Restoring hidden api policy to the device default configuration');
await this.adb.setDefaultHiddenApiPolicy(!!this.opts.ignoreHiddenApiPolicyError);
}

// @ts-expect-error unknown option
if (this.opts.reboot) {
const avdName = this.opts.avd!.replace('@', '');
this.log.debug(`Closing emulator '${avdName}'`);
try {
await this.adb.killEmulator(avdName);
} catch (err) {
this.log.warn(`Unable to close emulator: ${(err as Error).message}`);
}
}
}
if (this.mjpegStream) {
this.log.info('Closing MJPEG stream');
Expand Down
18 changes: 7 additions & 11 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ export type Uiautomator2DriverCaps = DriverCaps<Uiautomator2Constraints>;

export type W3CUiautomator2DriverCaps = W3CDriverCaps<Uiautomator2Constraints>;

export interface Uiautomator2DeviceInfo {
deviceApiLevel: number;
deviceScreenSize: string;
deviceScreenDensity: string;
deviceModel: string;
deviceManufacturer: string;
platformVersion: string;
}

export interface Uiautomator2SessionInfo {
deviceName: string;
deviceUDID: string;
Expand All @@ -27,6 +18,12 @@ export interface Uiautomator2DeviceDetails {
pixelRatio: string;
statBarHeight: number;
viewportRect: RelativeRect;
deviceApiLevel: number;
deviceScreenSize: string;
deviceScreenDensity: string;
deviceModel: string;
deviceManufacturer: string;
platformVersion: string;
}

export interface Uiautomator2ServerInfo {
Expand All @@ -48,8 +45,7 @@ export interface Uiautomator2StartSessionOpts
export interface Uiautomator2SessionCaps
extends Uiautomator2ServerInfo,
Uiautomator2SessionInfo,
Uiautomator2DeviceInfo,
Uiautomator2DeviceDetails {}
Partial<Uiautomator2DeviceDetails> {}

export interface Uiautomator2Settings {
ignoreUnimportantViews: boolean;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/driver-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chai.use(chaiAsPromised);
let sandbox = sinon.createSandbox();

function defaultStub(driver) {
sinon.stub(driver, 'getDeviceInfoFromUia2');
sinon.stub(driver, 'getDeviceDetails');
}

describe('driver.js', function () {
Expand Down

0 comments on commit 2a296dc

Please sign in to comment.