Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
afterEach(async function () {
if (driver) {
if (await adb.getApiLevel() > 23) {
let [language, country] = initialLocale.split('-');
await androidHelpers.ensureDeviceLocale(adb, language, country);
} else {
// This method is flakey in CI
if (!process.env.CI) {
await androidHelpers.ensureDeviceLocale(adb, null, initialLocale);
}
}
await deleteSession();
}
});
afterEach(async function () {
if (driver) {
if (await adb.getApiLevel() > 23) {
let [language, country] = initialLocale.split('-');
await androidHelpers.ensureDeviceLocale(adb, language, country);
} else {
// This method is flakey in CI
if (!process.env.CI) {
await androidHelpers.ensureDeviceLocale(adb, null, initialLocale);
}
}
await deleteSession();
}
});
function doInstall () {
// UiAutomator2 needs Java. Fail early if it doesn't exist
let androidHelpers = require('appium-android-driver').androidHelpers;
androidHelpers.getJavaVersion().then(function () {
let tries = 0;
function onErr (err) {
console.log(err.message);
let codeNotBuilt = err.message.indexOf('Cannot find module') !== -1;
if (tries >= MAX_ATTEMPTS) {
console.log("Tried too many times to install UiAutomator2, failing");
console.log("Original error: " + err.message);
throw new Error("Unable to import and run the installer. " +
"If you're running from source, run `gulp transpile` " +
"and then re-run `npm install`");
}
tries++;
if (codeNotBuilt && !attemptedToBuild) {
attemptedToBuild = true;
console.log("Attempting to transpile setup code...");
exec("npm run transpile", {cwd: path.resolve(__dirname, "..")}, function (err) {
function doInstall () {
// UiAutomator2 needs Java. Fail early if it doesn't exist
let androidHelpers = require('appium-android-driver').androidHelpers;
androidHelpers.getJavaVersion().then(function () {
let tries = 0;
function onErr (err) {
console.log(err.message);
let codeNotBuilt = err.message.indexOf('Cannot find module') !== -1;
if (tries >= MAX_ATTEMPTS) {
console.log("Tried too many times to install UiAutomator2, failing");
console.log("Original error: " + err.message);
throw new Error("Unable to import and run the installer. " +
"If you're running from source, run `gulp transpile` " +
"and then re-run `npm install`");
}
tries++;
if (codeNotBuilt && !attemptedToBuild) {
attemptedToBuild = true;
console.log("Attempting to transpile setup code...");
gulp.task('sign-apk', async function signApks () {
// Signs the APK with the default Appium Certificate
const adb = await androidHelpers.createADB({});
const pathToApk = path.resolve('apks', `appium-uiautomator2-server-v${version}.apk`);
return adb.sign(pathToApk);
});
async startUiAutomator2Session () {
// get device udid for this session
let {udid, emPort} = await helpers.getDeviceInfoFromCaps(this.opts);
this.opts.udid = udid;
this.opts.emPort = emPort;
// now that we know our java version and device info, we can create our
// ADB instance
this.adb = await androidHelpers.createADB(this.opts);
const apiLevel = await this.adb.getApiLevel();
if (apiLevel < 21) {
logger.errorAndThrow('UIAutomator2 is only supported since Android 5.0 (Lollipop). ' +
'You could still use other supported backends in order to automate older Android versions.');
}
if (apiLevel >= 28) { // Android P
logger.warn('Relaxing hidden api policy');
await this.adb.setHiddenApiPolicy('1');
}
// check if we have to enable/disable gps before running the application
if (util.hasValue(this.opts.gpsEnabled)) {
if (this.isEmulator()) {
async function main () {
// Signs the APK with the default Appium Certificate
const adb = await androidHelpers.createADB({});
const pathToApk = path.resolve('apks', `appium-uiautomator2-server-v${packageJson.version}.apk`);
await adb.sign(pathToApk);
}
after(async function () {
if (driver) {
if (await adb.getApiLevel() > 23) {
let [language, country] = initialLocale.split('-');
await androidHelpers.ensureDeviceLocale(driver.adb, language, country);
} else {
await androidHelpers.ensureDeviceLocale(driver.adb, null, initialLocale);
}
await deleteSession();
}
});
after(async function () {
if (driver) {
if (await adb.getApiLevel() > 23) {
let [language, country] = initialLocale.split('-');
await androidHelpers.ensureDeviceLocale(driver.adb, language, country);
} else {
await androidHelpers.ensureDeviceLocale(driver.adb, null, initialLocale);
}
await deleteSession();
}
});
async deleteSession () {
logger.debug('Deleting UiAutomator2 session');
await androidHelpers.removeAllSessionWebSocketHandlers(this.server, this.sessionId);
await this.mobileStopScreenStreaming();
if (this.uiautomator2) {
try {
await this.stopChromedriverProxies();
} catch (err) {
logger.warn(`Unable to stop ChromeDriver proxies: ${err.message}`);
}
if (this.jwpProxyActive) {
try {
await this.uiautomator2.deleteSession();
} catch (err) {
logger.warn(`Unable to proxy deleteSession to UiAutomator2: ${err.message}`);
}
}
this.uiautomator2 = null;
}