Skip to content

Commit

Permalink
feat: Introduce server status validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jul 16, 2024
1 parent 91ad21a commit 93ef77d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
67 changes: 61 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,53 @@ class EspressoRunner {
}
}

/**
* @param {import('@appium/types').StringRecord} driverManifestPayload
* @param {import('@appium/types').StringRecord} serverStatus
* @returns {Promise<boolean>}
*/
async _verifyServerStatus(driverManifestPayload, 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 '${driverManifestPayload.version}'.`
);
}
const {
build: {
version: serverVersion,
packageName: serverPackageName,
}
} = serverStatus;
const appLabel = serverPackageName ? `'${serverPackageName}' application` : 'application under test';
const parsedServerVersion = semver.coerce(serverVersion);
if (!parsedServerVersion) {
throw this.log.errorWithException(
`The Espresso server version '${serverVersion}' integrated with the ${appLabel} ` +
`cannot be parsed.`
);
}
const parsedDriverVersion = semver.coerce(driverManifestPayload.version);
if (parsedDriverVersion && 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 '${driverManifestPayload.version}'.`
);
} else if (parsedDriverVersion && 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} < ${driverManifestPayload.version})'.`
);
}
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 +354,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, 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 @@ -74,13 +74,15 @@
"npm-shrinkwrap.json"
],
"dependencies": {
"axios": "^1.7.2",
"appium-adb": "^12.4.0",
"appium-android-driver": "^9.8.0",
"asyncbox": "^3.0.0",
"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 93ef77d

Please sign in to comment.