How to use adb.shell method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

Run Appium Android Driver automation tests on LambdaTest cloud grid

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

adb.js

Source: adb.js Github

copy
1/**
2 * @author andhikayuana@gmail.com
3 * @since july, 25 2018
4 * 
5 * @see http://adbshell.com/commands
6 * 
7 * adb commands : 
8 * 1. ADB Debugging
9 *    - adb devices [v]
10 *    - adb forward 
11 *    - adb kill-server [v]
12 *    - adb start-server [v]
13 * 2. Wireless
14 *    - adb connect
15 *    - adb usb
16 * 3. Package Manager
17 *    - adb install
18 *    - adb uninstall
19 *    - adb shell pm list packages
20 *    - adb shell pm path
21 *    - adb shell pm clear
22 * 4. File Manager
23 *    - adb pull
24 *    - adb push
25 *    - adb shell ls
26 *    - adb shell cd
27 *    - adb shell rm
28 *    - adb shell mkdir
29 *    - adb shell touch
30 *    - adb shell pwd
31 *    - adb shell cp
32 *    - adb shell mv
33 * 5. Network
34 *    - adb shell netstat
35 *    - adb shell ping
36 *    - adb shell netcfg
37 *    - adb shell ip
38 * 6. Logcat
39 *    - adb logcat [-] not all
40 *    - adb shell dumpsys
41 *    - adb shell dumpstate
42 * 7. Screenshot
43 *    - adb shell screencap
44 *    - adb shell screenrecord [4.4+]
45 * 8. System
46 *    - adb root
47 *    - adb sideload
48 *    - adb shell ps
49 *    - adb shell top
50 *    - adb shell getprop
51 *    - adb shell setprop
52 */
53const { exec } = require('child_process')
54
55var tester = () => { return 4 + 6; }
56
57module.exports.tester = tester
58
59/**
60 * ADB Debugging
61 */
62module.exports.devices = (callback) => exec('adb devices', callback)
63module.exports.killServer = (callback) => exec('adb kill-server', callback)
64module.exports.startServer = (callback) => exec('adb start-server', callback)
65module.exports.logcat = (callback) => exec('adb logcat', callback)
66module.exports.logcatV = (callback) => exec('adb logcat *:V', callback)
67module.exports.logcatD = (callback) => exec('adb logcat *:D', callback)
68module.exports.logcatI = (callback) => exec('adb logcat *:I', callback)
69module.exports.logcatW = (callback) => exec('adb logcat *:W', callback)
70module.exports.logcatE = (callback) => exec('adb logcat *:E', callback)
71module.exports.logcatF = (callback) => exec('adb logcat *:F', callback)
72module.exports.logcatS = (callback) => exec('adb logcat *:S', callback)
Full Screen

auto.js

Source: auto.js Github

copy
1const exec = require('child_process').execSync;
2
3const disX = 250, disY = 300, num = 3, py = 150;
4
5// 位置1
6const originX = 303, originY = 814;
7
8// 触发位置
9const touchPoints = [[663, 1752], [820, 1700], [972, 1630]]
10
11setInterval(() => {
12  for(let i = 0; i < num; i++) {
13    for(let j = 0; j < num; j++) {
14      let targetY = originY + (j * disY);
15      for(let z = 0; z < num; z++) {
16        const targetX = originX + (z * disX);
17        targetY = targetY - (z * py);
18        exec(`adb shell input swipe ${touchPoints[i][0]} ${touchPoints[i][1]} ${targetX} ${targetY}`);
19        exec(`sleep 0.01`);
20      }
21    }
22  }
23}, 1000)
24
25// 开始点击位置
26// for(let i = 0; i < num; i++) {
27//   exec(`adb shell sendevent /dev/input/event0 3 0 ${touchPoints[i][0]}`);
28//   exec(`adb shell sendevent /dev/input/event0 3 1 ${touchPoints[i][1]}`);
29//   exec(`adb shell sendevent /dev/input/event0 1 330 1`);
30//   exec(`adb shell sendevent /dev/input/event0 0 0 0`);
31//   for(let j = 0; j < num; j++) {
32//     let targetY = originY + (j * disY);
33//     for(let z = 0; z < num; z++) {
34//       const targetX = originX + (z * disX);
35//       targetY = targetY - (z * py);
36//       exec(`adb shell sendevent /dev/input/event0 3 0 ${targetX}`);
37//       exec(`adb shell sendevent /dev/input/event0 0 0 0`);
38//       exec(`adb shell sendevent /dev/input/event0 3 1 ${targetY}`);
39//       exec(`adb shell sendevent /dev/input/event0 0 0 0`);
40//     }
41//   }
42//   exec(`adb shell sendevent /dev/input/event0 1 330 0`);
43//   exec(`adb shell sendevent /dev/input/event0 0 0 0`);
44// }
45
Full Screen

AndroidUtils.js

Source: AndroidUtils.js Github

copy
1const exec = require('shell-utils').exec;
2
3module.exports = {
4  pressBack: () => {
5    exec.execSync('adb shell input keyevent 4');
6  },
7  pressMenu: () => {
8    exec.execSync('adb shell input keyevent 82');
9  },
10  pressKeyCode: (keyCode) => {
11    exec.execSync(`adb shell input keyevent ${keyCode}`);
12  },
13  grantPermission: () => {
14    exec.execSync('adb shell pm grant com.reactnativenavigation.playground android.permission.READ_PHONE_STATE');
15  },
16  revokePermission: () => {
17    exec.execSync('adb shell pm revoke com.reactnativenavigation.playground android.permission.READ_PHONE_STATE');
18  },
19  openActivity: () => {
20    exec.execSync('adb shell am start -n com.reactnativenavigation.playground/.MainActivity');
21  },
22  executeShellCommand: (command) => {
23    exec.execSync(`adb shell ${command}`);
24  }
25};
26
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Android Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)