93 lines
2.8 KiB
JavaScript
93 lines
2.8 KiB
JavaScript
const {exec} = require("child_process");
|
|
|
|
// DEVICE_REGEX = "(^[a-z0-9A-Z]*) .* model:([a-z0-9A-Z_]+)"
|
|
DEVICE_REGEX = "(^[a-z0-9A-Z-.:]*) .* model:([a-z0-9A-Z_]+)"
|
|
|
|
// DEVICE_REGEX = "(^[a-z0-9A-Z-]*) .* model:([a-z0-9A-Z_]+)"
|
|
|
|
|
|
async function devices() {
|
|
return new Promise((resolve, reject) => {
|
|
exec("adb devices -l", (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.log(`error: ${error.message}`);
|
|
return;
|
|
}
|
|
if (stderr) {
|
|
console.log(`stderr: ${stderr}`);
|
|
return;
|
|
}
|
|
console.log(`stdout: ${stdout}`);
|
|
let lines = stdout.split("\n");
|
|
let result = [];
|
|
for (let i = 1; i < lines.length; i++) {
|
|
currentLine = lines[i]
|
|
console.log("currentLine is " + currentLine)
|
|
if (currentLine.length > 0) {
|
|
const o = currentLine.split('\t')
|
|
findResult = currentLine.match(DEVICE_REGEX)
|
|
console.log(findResult)
|
|
try {
|
|
result.push(new AndroidDevice(findResult[1], findResult[2]));
|
|
} catch (e) {
|
|
console.log(e)
|
|
}
|
|
console.log(result[i])
|
|
}
|
|
}
|
|
resolve(result);
|
|
}
|
|
);
|
|
})
|
|
}
|
|
|
|
class AndroidDevice {
|
|
constructor(serial, model) {
|
|
this.serial = serial
|
|
this.model = model
|
|
}
|
|
|
|
async swipeForDevice(x0, y0, x1, y1) {
|
|
let cmd = `input swipe ${x0} ${y0} ${x1} ${y1}`
|
|
await this.shell(cmd);
|
|
}
|
|
|
|
async tapForDevice(x, y) {
|
|
let cmd = `input tap ${x} ${y}`
|
|
console.log("cmd is " + cmd)
|
|
try {
|
|
await this.shell(cmd);
|
|
} catch (e) {
|
|
console.log(e)
|
|
}
|
|
}
|
|
|
|
async clearApp(packageName) {
|
|
let cmd = `adb -s ${this.serial} shell pm clear ${packageName}`
|
|
console.log("cmd is " + cmd)
|
|
await exec(cmd);
|
|
}
|
|
|
|
|
|
shell(cmd) {
|
|
return new Promise((resolve, reject) => {
|
|
exec("adb -s " + this.serial + " shell " + cmd, (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.log(`error: ${error.message}`);
|
|
reject(error);
|
|
return;
|
|
}
|
|
if (stderr) {
|
|
console.log(`stderr: ${stderr}`);
|
|
reject(stderr);
|
|
return;
|
|
}
|
|
console.log(`stdout: ${stdout}`);
|
|
resolve(stdout);
|
|
}
|
|
);
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = {AndroidDevice, devices} |