Skip to content

Commit

Permalink
feat: Bump android driver to v7 (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Oct 14, 2023
1 parent f070232 commit a54bc4a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 54 deletions.
9 changes: 0 additions & 9 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,6 @@ const GeneralMixin = {
);
},

/**
* Overriding appium-android-driver's wrapBootstrapDisconnect,
* unlike in appium-android-driver avoiding adb restarting as it intern
* kills UiAutomator2 server running in the device.
**/
async wrapBootstrapDisconnect(wrapped) {
await wrapped();
},

// Stop proxying to any Chromedriver and redirect to uiautomator2
suspendChromedriverProxy() {
this.chromedriver = undefined;
Expand Down
6 changes: 4 additions & 2 deletions lib/commands/mixins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
* @module
* @privateRemarks These mixins are kind of a mishmash of stuff from `appium-android-driver`, unique things, and stuff from `ExternalDriver`. Ideally, we should be pulling the method definitions right out of `ExternalDriver` whenever possible. Also note that the mixins contain _more stuff than just commands or execute methods_.
* @privateRemarks These mixins are kind of a mishmash of stuff from `appium-android-driver`,
* @privateRemarks unique things, and stuff from `ExternalDriver`. Ideally, we should be pulling the method
* @privateRemarks definitions right out of `ExternalDriver` whenever possible. Also note that the mixins
* @privateRemarks contain _more stuff than just commands or execute methods_.
*/

import type {Element, ExternalDriver, StringRecord} from '@appium/types';
Expand Down Expand Up @@ -87,7 +90,6 @@ export type UIA2GeneralMixin = UIA2Mixin<
| 'getWindowSize'
| 'getWindowRect'
| 'setUrl'
| 'wrapBootstrapDisconnect'
| 'keyevent'
| 'execute'
| 'executeMobile'
Expand Down
51 changes: 14 additions & 37 deletions lib/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import uiautomator2Helpers from './helpers';
import {newMethodMap} from './method-map';
import type {
Uiautomator2Settings,
Uiautomator2CreateResult,
Uiautomator2DeviceDetails,
Uiautomator2DeviceInfo,
Uiautomator2DriverCaps,
Expand Down Expand Up @@ -163,14 +162,12 @@ const CHROME_NO_PROXY: RouteMatcher[] = [
const MEMOIZED_FUNCTIONS = ['getStatusBarHeight', 'getDevicePixelRatio'] as const;

class AndroidUiautomator2Driver
extends AndroidDriver<Uiautomator2Settings, Uiautomator2CreateResult>
extends AndroidDriver
implements
ExternalDriver<
Uiautomator2Constraints,
string,
StringRecord,
Uiautomator2Settings,
Uiautomator2CreateResult
StringRecord
>
{
static newMethodMap = newMethodMap;
Expand Down Expand Up @@ -236,12 +233,12 @@ class AndroidUiautomator2Driver
);
}

override async createSession(
async createSession(
w3cCaps1: W3CUiautomator2DriverCaps,
w3cCaps2?: W3CUiautomator2DriverCaps,
w3cCaps3?: W3CUiautomator2DriverCaps,
driverData?: DriverData[]
): Promise<Uiautomator2CreateResult> {
): Promise<any> {
try {
// TODO handle otherSessionData for multiple sessions
const [sessionId, caps] = (await BaseDriver.prototype.createSession.call(
Expand Down Expand Up @@ -340,11 +337,7 @@ class AndroidUiautomator2Driver
return {...sessionData, ...uia2Data};
}

override isEmulator() {
return helpers.isEmulator(this.adb, this.opts);
}

override setAvdFromCapabilities(caps: Uiautomator2StartSessionOpts) {
setAvdFromCapabilities(caps: Uiautomator2StartSessionOpts) {
if (this.opts.avd) {
this.log.info('avd name defined, ignoring device name and platform version');
} else {
Expand Down Expand Up @@ -737,7 +730,7 @@ class AndroidUiautomator2Driver
});
}

override async deleteSession() {
async deleteSession() {
this.log.debug('Deleting UiAutomator2 session');

const screenRecordingStopTasks = [
Expand Down Expand Up @@ -871,44 +864,32 @@ class AndroidUiautomator2Driver
await BaseDriver.prototype.deleteSession.call(this);
}

override async checkAppPresent() {
async checkAppPresent() {
this.log.debug('Checking whether app is actually present');
if (!(await fs.exists(this.opts.app))) {
this.log.errorAndThrow(`Could not find app apk at '${this.opts.app}'`);
throw new Error(); // unreachable
}
}

override async onSettingsUpdate() {
async onSettingsUpdate() {
// intentionally do nothing here, since commands.updateSettings proxies
// settings to the uiauto2 server already
}

// Need to override android-driver's version of this since we don't actually
// have a bootstrap; instead we just restart adb and re-forward the UiAutomator2
// port
override async wrapBootstrapDisconnect(wrapped: () => Promise<void>) {
await wrapped();
this.adb!.restart();
await this.allocateSystemPort();
await this.allocateMjpegServerPort();
}

override proxyActive(sessionId: string) {
BaseDriver.prototype.proxyActive.call(this, sessionId);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
proxyActive(sessionId: string) {
// we always have an active proxy to the UiAutomator2 server
return true;
}

override canProxy(sessionId: string) {
BaseDriver.prototype.canProxy.call(this, sessionId);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
canProxy(sessionId: string) {
// we can always proxy to the uiautomator2 server
return true;
}

override getProxyAvoidList() {
getProxyAvoidList() {
// we are maintaining two sets of NO_PROXY lists, one for chromedriver(CHROME_NO_PROXY)
// and one for uiautomator2(NO_PROXY), based on current context will return related NO_PROXY list
if (util.hasValue(this.chromedriver)) {
Expand All @@ -927,10 +908,6 @@ class AndroidUiautomator2Driver
return this.jwpProxyAvoid;
}

get isChromeSession() {
return helpers.isChromeBrowser(this.opts.browserName);
}

async updateSettings(settings: Uiautomator2Settings) {
await this.settings.update(settings);
await this.uiautomator2!.jwproxy.command('/appium/settings', 'POST', {settings});
Expand All @@ -942,7 +919,7 @@ class AndroidUiautomator2Driver
'/appium/settings',
'GET'
)) as Partial<Uiautomator2Settings>;
return {...driverSettings, ...serverSettings};
return {...driverSettings, ...serverSettings} as any;
}
}

Expand Down
6 changes: 2 additions & 4 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {DriverCaps, DriverOpts, W3CDriverCaps} from '@appium/types';
import type {AndroidSettings} from 'appium-android-driver';
import type {EmptyObject} from 'type-fest';
import type {RelativeRect} from './commands/types';
import type {Uiautomator2Constraints} from './constraints';
Expand Down Expand Up @@ -52,8 +51,7 @@ export interface Uiautomator2SessionCaps
Uiautomator2DeviceInfo,
Uiautomator2DeviceDetails {}

export type Uiautomator2CreateResult = [string, Uiautomator2SessionCaps];

export interface Uiautomator2Settings extends AndroidSettings {
export interface Uiautomator2Settings {
ignoreUnimportantViews: boolean;
allowInvisibleElements: boolean;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"dependencies": {
"appium-adb": "^9.14.12",
"appium-android-driver": "^6.0.0",
"appium-android-driver": "^7.0.1",
"appium-chromedriver": "^5.6.5",
"appium-uiautomator2-server": "^5.12.2",
"asyncbox": "^2.3.1",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"outDir": "build",
"strict": false,
"checkJs": true,
"types": ["node", "mocha", "sinon-chai", "chai-as-promised"]
"types": ["node"]
},
"include": ["lib", "index.js"]
}

0 comments on commit a54bc4a

Please sign in to comment.