How to use this.killUiAutomatorOnDevice method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

uiautomator2.js

Source:uiautomator2.js Github

copy

Full Screen

...89    return false;90  }91  async startSession (caps) {92    // kill any uiautomator existing processes93    await this.killUiAutomatorOnDevice();94    logger.info(`Starting uiautomator2 server ${serverVersion}`);95    logger.info(`Using UIAutomator2 server from '${apkPath}' and test from '${testApkPath}'`);96    // let cmd = ['am', 'instrument', '-w',97    //   'io.appium.uiautomator2.server.test/android.support.test.runner.AndroidJUnitRunner'];98    // this.adb.shell(cmd);99    // using 3rd party module called 'adbKit' for time being as using 'appium-adb'100    // facing IllegalStateException: UiAutomation not connected exception101    // https://github.com/appium/appium-uiautomator2-driver/issues/19102    await this.startSessionUsingAdbKit(caps.deviceUDID);103    let retries = getRetries('Server launch', caps.uiautomator2ServerLaunchTimeout, SERVER_LAUNCH_RETRIES);104    logger.info(`Waiting up to ${retries * 1000}ms for UiAutomator2 to be online...`);105    // wait for UiAutomator2 to be online106    await retryInterval(retries, 1000, this.jwproxy.command.bind(this.jwproxy), '/status', 'GET');107    await this.jwproxy.command('/session', 'POST', {desiredCapabilities: caps});...

Full Screen

Full Screen

uiautomator.js

Source:uiautomator.js Github

copy

Full Screen

...19      // expecting a path like /ads/ads/foo.jar or \asd\asd\foo.jar20      let jarName = this.parseJarNameFromPath(uiAutomatorBinaryPath);21      await this.adb.push(uiAutomatorBinaryPath, this.tempPath);22      // killing any uiautomator existing processes23      await this.killUiAutomatorOnDevice();24      log.debug('Starting UIAutomator');25      let args = ['shell', 'uiautomator', 'runtest', jarName, '-c', className, ...extraParams];26      this.proc = this.adb.createSubProcess(args);27      // handle out-of-bound exit by simply emitting a stopped state28      this.proc.on('exit', (code, signal) => {29        processIsAlive = false;30        // cleanup31        if (this.state !== UiAutomator.STATE_STOPPED &&32            this.state !== UiAutomator.STATE_STOPPING) {33          let msg = `UiAutomator exited unexpectedly with code ${code}, ` +34                    `signal ${signal}`;35          log.error(msg);36        } else if (this.state === UiAutomator.STATE_STOPPING) {37          log.debug('UiAutomator shut down normally');38        }39        this.changeState(UiAutomator.STATE_STOPPED);40      });41      await this.proc.start(startDetector);42      processIsAlive = true;43      this.changeState(UiAutomator.STATE_ONLINE);44      return this.proc;45    } catch (e) {46      this.emit(UiAutomator.EVENT_ERROR, e);47      if (processIsAlive) {48        await this.killUiAutomatorOnDevice();49        await this.proc.stop();50      }51      log.errorAndThrow(e);52    }53  }54  async shutdown () {55    log.debug('Shutting down UiAutomator');56    if (this.state !== UiAutomator.STATE_STOPPED) {57      this.changeState(UiAutomator.STATE_STOPPING);58      await this.proc.stop();59    }60    await this.killUiAutomatorOnDevice();61    this.changeState(UiAutomator.STATE_STOPPED);62  }63  parseJarNameFromPath (binaryPath) {64    let reTest = /.*(\/|\\)(.*\.jar)/.exec(binaryPath);65    if (!reTest) {66      throw new Error(`Unable to parse jar name from ${binaryPath}`);67    }68    let jarName = reTest[2];69    log.debug(`Found jar name: '${jarName}'`);70    return jarName;71  }72  changeState (state) {73    log.debug(`Moving to state '${state}'`);74    this.state = state;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var androidDriver = new AndroidDriver();2androidDriver.killUiAutomatorOnDevice();3var androidDriver = new AndroidDriver();4androidDriver.killUiAutomatorOnDevice();5var androidDriver = new AndroidDriver();6androidDriver.killUiAutomatorOnDevice();7var androidDriver = new AndroidDriver();8androidDriver.killUiAutomatorOnDevice();9var androidDriver = new AndroidDriver();10androidDriver.killUiAutomatorOnDevice();11var androidDriver = new AndroidDriver();12androidDriver.killUiAutomatorOnDevice();13var androidDriver = new AndroidDriver();14androidDriver.killUiAutomatorOnDevice();15var androidDriver = new AndroidDriver();16androidDriver.killUiAutomatorOnDevice();17var androidDriver = new AndroidDriver();18androidDriver.killUiAutomatorOnDevice();19var androidDriver = new AndroidDriver();20androidDriver.killUiAutomatorOnDevice();21var androidDriver = new AndroidDriver();22androidDriver.killUiAutomatorOnDevice();23var androidDriver = new AndroidDriver();

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('assert');2var wd = require('wd');3var driver = wd.promiseChainRemote("localhost", 4723);4driver.init({5}).then(function(){6    return driver.setImplicitWaitTimeout(10000);7}).then(function(){8    return driver.sleep(10000);9}).then(function(){10    return driver.killUiAutomatorOnDevice();11}).then(function(){12    return driver.sleep(10000);13}).then(function(){14    return driver.quit();15}).catch(function(err){16    console.log(err);17    driver.quit();18});19var assert = require('assert');20var wd = require('wd');21var driver = wd.promiseChainRemote("localhost", 4723);22driver.init({23}).then(function(){24    return driver.setImplicitWaitTimeout(10000);25}).then(function(){26    return driver.sleep(10000);27}).then(function(){28    return driver.killUiAutomatorOnDevice();29}).then(function(){30    return driver.sleep(10000);31}).then(function(){32    return driver.quit();33}).catch(function(err){34    console.log(err);35    driver.quit();36});37var assert = require('assert');38var wd = require('wd');39var driver = wd.promiseChainRemote("localhost", 4723);40driver.init({41}).then(function(){42    return driver.setImplicitWaitTimeout(10000);43}).then(function(){44    return driver.sleep(10000);45}).then(function(){46    return driver.killUiAutomatorOnDevice();47}).then(function(){48    return driver.sleep(10000);49}).then(function(){50    return driver.quit();51}).catch(function(err){52    console.log(err);53    driver.quit();54});

Full Screen

Using AI Code Generation

copy

Full Screen

1var driver = new AndroidDriver();2driver.killUiAutomatorOnDevice();3driver.quit();4AndroidDriver.prototype.killUiAutomatorOnDevice = function () {5  var cmd = "ps -ef | grep uiautomator | grep -v grep | awk '{print $2}' | xargs kill -9";6  this.adb.shell(cmd);7};8ADB.prototype.shell = function (cmd, cb) {9  logger.debug("Executing adb shell " + cmd);10  var execCmd = "shell " + cmd;11  this.exec(execCmd, cb);12};13ADB.prototype.exec = function (cmd, cb) {14  var adb = this.adb;15  var args = cmd.split(" ");16  var proc = spawn(adb, args);17  var stdout = "";18  var stderr = "";19  proc.stdout.on('data', function (data) {20    stdout += data;21  });22  proc.stderr.on('data', function (data) {23    stderr += data;24  });25  proc.on('exit', function (code) {26    cb(code, stdout, stderr);27  });28};29AndroidDriver.prototype.quit = function (cb) {30  this.onStop();31  this.proxyReqRes.close();32  this.adb.stopLogcat();33  this.adb.killServer();34  cb();35};36ADB.prototype.killServer = function (cb) {37  logger.debug("Killing ADB server");38  var cmd = "kill-server";39  this.exec(cmd, function (err) {40    if (err) logger.error("Error killing ADB server, going to see if it's online anyway");41    this.getDevicesWithRetry(cb);42  }.bind(this));43};44ADB.prototype.getDevicesWithRetry = function (cb, timeoutMs) {45  if (typeof timeoutMs === "undefined") {46    timeoutMs = 2000;47  }48  var start = Date.now();49  var error = null;50  async.doWhilst(function (asyncCb) {51    this.getDevices(function (err, devices) {52      error = err;53      if (!

Full Screen

Using AI Code Generation

copy

Full Screen

1this.driver = new AndroidDriver();2this.driver.killUiAutomatorOnDevice();3AppiumAndroidDriver.prototype.killUiAutomatorOnDevice = function () {4  var cmd = "am force-stop io.appium.uiautomator2.server";5  return this.adb.shell(cmd);6};7AndroidHelpers.prototype.shell = function (cmd) {8  return this.adbExec(['shell', cmd], {timeout: 20000});9};10ADB.prototype.adbExec = function (args, opts = {}) {11  return this.exec(this.executable.path, args, opts);12};13systemCallMethods.exec = async function (cmd, opts = {}) {14  log.debug(`Executing command '${cmd}'`);15  const {stdout} = await exec(cmd, opts);16  return stdout;17};18async function exec (cmd, opts = {}) {19  const {stdout} = await exec(cmd, opts);20  return stdout;21};22async function exec (cmd, opts = {}) {23  const {stdout} = await exec(cmd, opts);24  return stdout;25};26async function exec (cmd, opts = {}) {27  const {stdout} = await exec(cmd, opts);28  return stdout;29};30async function exec (cmd, opts = {}) {31  const {stdout} = await exec(cmd, opts);32  return stdout;33};34async function exec (cmd, opts = {}) {35  const {stdout} = await exec(cmd, opts);36  return stdout;37};38async function exec (cmd, opts = {}) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var driver = new AndroidDriver();2driver.killUiAutomatorOnDevice();3killUiAutomatorOnDevice: function () {4    return this.adb.killUiAutomatorOnDevice();5}6AndroidHelpers.prototype.killUiAutomatorOnDevice = function () {7    return this.adb.shell("am force-stop io.appium.uiautomator2.server");8};9ADB.prototype.killUiAutomatorOnDevice = function () {10    return this.shell("am force-stop io.appium.uiautomator2.server");11};12ADB.prototype.shell = function (cmd) {13    return exec(this.executable.path, this.adb.defaultArgs.concat(['shell', cmd]));14};15Object.defineProperty(ADB.prototype, 'executable', {16    get: function () {17        if (this._adb) {18            return this._adb;19        }20        this._adb = this.binaries.adb;21        logger.debug("Using adb from " + this._adb);22        return this._adb;23    }24});25Object.defineProperty(SystemCalls.prototype, 'binaries', {26    get: function () {27        if (this._binaries) {28            return this._binaries;29        }30        this._binaries = {};31        this._binaries.adb = this.getBinaryPath("adb");32        this._binaries.android = this.getBinaryPath("android");33        this._binaries.fastboot = this.getBinaryPath("fastboot");34        this._binaries.sysZip = this.getBinaryPath("sysZip");35        this._binaries.zipAlign = this.getBinaryPath("zipAlign");36        this._binaries.mksdcard = this.getBinaryPath("mksdcard");37        return this._binaries;38    }39});40SystemCalls.prototype.getBinaryPath = function (binaryName) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var desired = {4};5var driver = wd.remote("localhost", 4723);6driver.init(desired, function() {7  setTimeout(function() {8    driver.quit();9    driver.killUiAutomatorOnDevice(function(err) {10      if (err) {11        console.log("Error killing UiAutomator on device");12      } else {13        console.log("UiAutomator killed on device");14      }15    });16  }, 5000);17});18var wd = require('wd');19var assert = require('assert');20var desired = {21};22var driver = wd.remote("localhost", 4723);23driver.init(desired, function() {24  setTimeout(function() {25    driver.quit();26    driver.killUiAutomatorOnEmulator(function(err) {27      if (err) {28        console.log("Error killing UiAutomator on emulator");29      } else {30        console.log("UiAutomator killed on emulator");31      }32    });33  }, 5000);34});35var wd = require('wd');36var assert = require('assert');37var desired = {

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('test', function () {2  it('should kill UiAutomator', function () {3    driver.killUiAutomatorOnDevice();4  });5});6describe('test', function () {7  it('should kill UiAutomator', function () {8    driver.killUiAutomatorOnDevice().then(function () {9      console.log('UiAutomator killed');10    });11  });12});13describe('test', function () {14  it('should kill UiAutomator', function () {15    driver.killUiAutomatorOnDevice().then(function () {16      console.log('UiAutomator killed');17    });18  });19});20describe('test', function () {21  it('should kill UiAutomator', function () {22    driver.killUiAutomatorOnDevice().then(function () {23      console.log('UiAutomator killed');24    });25  });26});27describe('test', function () {28  it('should kill UiAutomator', function () {29    driver.killUiAutomatorOnDevice().then(function () {30      console.log('UiAutomator killed');31    });32  });33});34describe('test', function () {35  it('should kill UiAutomator', function () {

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Android Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful