Skip to content

Commit

Permalink
feat: Introduce server status validation (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jul 16, 2024
1 parent 91ad21a commit 392a5d4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
72 changes: 66 additions & 6 deletions lib/espresso-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import B from 'bluebird';
import _ from 'lodash';
import { copyGradleProjectRecursively, getPackageInfoSync, getPackageInfo } from './utils';
import axios from 'axios';
import semver from 'semver';


const TEST_SERVER_ROOT = path.resolve(__dirname, '..', '..', 'espresso-server');
Expand Down Expand Up @@ -251,6 +252,58 @@ class EspressoRunner {
}
}

/**
* @param {string} driverVersion
* @param {import('@appium/types').StringRecord} serverStatus
* @returns {Promise<boolean>}
*/
async _verifyServerStatus(driverVersion, serverStatus) {
if (!_.isPlainObject(serverStatus) || !_.isPlainObject(serverStatus.build)) {
throw this.log.errorWithException(
`The Espresso server version integrated with the application under test is not compatible ` +
`with the current driver version '${driverVersion}'.`
);
}
const {
build: {
version: serverVersion,
packageName: serverPackageName,
}
} = serverStatus;
const appLabel = serverPackageName ? `'${serverPackageName}' application` : 'application under test';
const parsedServerVersion = semver.coerce(serverVersion);
const parsedDriverVersion = semver.coerce(driverVersion);
if (parsedServerVersion && parsedDriverVersion) {
if (parsedServerVersion.major !== parsedDriverVersion.major) {
throw this.log.errorWithException(
`The Espresso server version '${serverVersion}' integrated with the ${appLabel} is not compatible ` +
`with the current driver version '${driverVersion}'.`
);
} else if (parsedServerVersion.minor < parsedDriverVersion.minor) {
this.log.warn(
`The Espresso server version integrated with the ${appLabel} might not be compatible ` +
`with the current driver version (${serverVersion} < ${driverVersion})'.`
);
}
} else {
const warnMessage = parsedServerVersion
? `The Espresso driver version '${driverVersion}' ` +
`cannot be parsed. It might be incompatible with the current server '${serverVersion}' ` +
`integrated with the ${appLabel} .`
: `The Espresso server version '${serverVersion}' integrated with the ${appLabel} ` +
`cannot be parsed. It might be incompatible with the current driver ` +
`version '${driverVersion}'.`;
this.log.warn(warnMessage);
}
if (this.appPackage && serverPackageName && this.appPackage !== serverPackageName) {
throw this.log.errorWithException(
`The Espresso server that is listening on the device under tests is built for a different ` +
`application package (${serverPackageName} !== ${this.appPackage}).`
);
}
return true;
}

async startSession (caps) {
await this.cleanupSessionLeftovers();

Expand Down Expand Up @@ -306,19 +359,26 @@ class EspressoRunner {
try {
await waitForCondition(async () => {
if (hasSocketError) {
this.log.errorAndThrow(`Espresso server has failed to start due to an unexpected exception. ` +
throw this.log.errorWithException(
`Espresso server has failed to start due to an unexpected exception. ` +
`Make sure the 'INTERNET' permission is requested in the Android manifest of your ` +
`application under test (<uses-permission android:name="android.permission.INTERNET" />)`);
`application under test (<uses-permission android:name="android.permission.INTERNET" />)`
);
} else if (this.jwproxy.instrumentationState.exited) {
this.log.errorAndThrow(`Espresso server process has been unexpectedly terminated. ` +
`Check the Appium server log and the logcat output for more details`);
throw this.log.errorWithException(
`Espresso server process has been unexpectedly terminated. ` +
`Check the Appium server log and the logcat output for more details`
);
}
let serverStatus;
try {
await this.jwproxy.command('/status', 'GET');
return true;
(serverStatus = /** @type {import('@appium/types').StringRecord} */ (
await this.jwproxy.command('/status', 'GET')
));
} catch (e) {
return false;
}
return await this._verifyServerStatus(manifestPayload.version, serverStatus);
}, {
waitMs: this.serverLaunchTimeout,
intervalMs: 500,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@
"appium-adb": "^12.4.0",
"appium-android-driver": "^9.8.0",
"asyncbox": "^3.0.0",
"axios": "^1.7.2",
"bluebird": "^3.5.0",
"io.appium.settings": "^5.12.0",
"lodash": "^4.17.11",
"portscanner": "^2.1.1",
"semver": "^7.6.2",
"source-map-support": "^0.x",
"teen_process": "^2.2.0"
},
Expand Down

0 comments on commit 392a5d4

Please sign in to comment.