-
Notifications
You must be signed in to change notification settings - Fork 0
API Devices
When you want to access an android device, you'll have to create a connection between your computer and the device. This
can be done with the device class provided by gotron.
The device class is located at src/device/Device.ts
and contains some controllers, which we'll cover later on.
To create a device, it isn't recommended to directly create a new instance of it with the device's serial id. You should
use one of the classes gotron provides.
Gives you the platform version of this device. The platform version is a string.
const platform = await device.platformVersion();
Run a command as this device. The command parameter can be any adb command. The execution will look like
this: adb -s <deviceId> <command>
Returns a ExecResult
, which contains the stdout and stderr.
const { stdout, stderr } = await device.adb("devices");
Run a command in the adb shell. The parameter can be any shell command. The execution will look like
this: adb -s <deviceId> shell <command>
Returns a ExecResult
, which contains the stdout and stderr.
const { stdout, stderr } = await device.shell("input text Hello");
Receive the current app hierarchy. The hierarchy contains the tree structur of the current open activity.
Returns a instance of the Hierarchy
class.
const hierarchy = await emulator.hierarchy();
This method returns a promise which will resolve as soon as the device has fully booted. It's recommended to use it always when receiving a device instance.
await emulator.waitTillBoot();
This method will take a screenshot and deploy the screenshot file from the phone to the given filePath. Screenshots will be saved as pngs.
await emulator.screenshot("chrome.png");
Receive the device's app controller. Use it for managing apps. It will be covered in a different wiki
await emulator.apps.killApp("com.android.chrome");
Receive the device's input controller. Use it for clicking, swiping, typing, etc. Will be convered in a different wiki
await emulator.inputs.click(500, 1500);
Receive the device's file controller. Use it to receive files from your phone or upload it. Will be convered in a different wiki
const files = await device.file;
Get the device's serial Id, in cause you need to run other tools that aren't supported by gotron.
const serialId = device.serialId();
The Emulator extends the device and has static methods to start a new emulator or connect to an already existing one.
This method creates a connection between your computer and an already running emulator. The port is the number the emulator listens to. Usually defaults to 5555 when you haven't specificed it. It returns a promise with the emulator.
import { Emulator } from "gotron/lib/device/Emulator";
(async () => {
const device = await Emulator.connectToEmulator(5555);
// ... Use the device
})();
This method creates a new emulator listening at the given port. A new emulator will be started this time, where the avd is going to be the emulatorId. It also returns a promise with the created emulator automatically connected.
import { Emulator } from "gotron/lib/device/Emulator";
(async () => {
const device = await Emulator.newEmulator(5555, "AVD NAME HERE");
// ... Use the device
})();
This method lists all available avd names you can start using
the newEmulator(port: number, emulatorId?: string): Promise<Emulator>
method
It returns a promise containing a array of all available avds.
import { Emulator } from "gotron/lib/device/Emulator";
(async () => {
const avd = (await Emulator.getInstalledDevices())[0];
const device = await Emulator.newEmulator(5555, avd);
// ... Use the device
})();
A USB Device is connected to your computer using a cable. The device has to have usb debugging enabled for this connection to work.
Connects to a device with the serialId. Returns a USBDevice instance.
import { USBDevice } from "./lib/device/USBDevice";
(async () => {
const device = await USBDevice.connectToDevice("RJSGEH9865");
// ... Use the device
})();