Skip to content

API Devices

Tobero edited this page Mar 13, 2023 · 5 revisions

Device class


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.

platformVersion(): Promise

Gives you the platform version of this device. The platform version is a string.

Example

const platform = await device.platformVersion();

async adb(command: string): Promise

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.

Example

const { stdout, stderr } = await device.adb("devices");

async shell(command: string): Promise

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.

Example

const { stdout, stderr } = await device.shell("input text Hello");

async hierarchy(): Promise

Receive the current app hierarchy. The hierarchy contains the tree structur of the current open activity. Returns a instance of the Hierarchy class.

Example

const hierarchy = await emulator.hierarchy();

waitTillBoot(): Promise

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.

Example

await emulator.waitTillBoot();

async screenshot(filePath: string): Promise

This method will take a screenshot and deploy the screenshot file from the phone to the given filePath. Screenshots will be saved as pngs.

Example

await emulator.screenshot("chrome.png");

get apps(): AppController

Receive the device's app controller. Use it for managing apps. It will be covered in a different wiki

Example

await emulator.apps.killApp("com.android.chrome");

get inputs(): InputController

Receive the device's input controller. Use it for clicking, swiping, typing, etc. Will be convered in a different wiki

Example

await emulator.inputs.click(500, 1500);

get files(): FileController

Receive the device's file controller. Use it to receive files from your phone or upload it. Will be convered in a different wiki

Example

const files = await device.file;

get serialId(): string

Get the device's serial Id, in cause you need to run other tools that aren't supported by gotron.

Example

const serialId = device.serialId();

Devices

Emulator


The Emulator extends the device and has static methods to start a new emulator or connect to an already existing one.

static async connectToEmulator(port: number): Promise

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.

Example

import { Emulator } from "gotron/lib/device/Emulator";

(async () => {
  const device = await Emulator.connectToEmulator(5555);
  // ... Use the device
})();

static async newEmulator(port: number, emulatorId?: string): Promise

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.

Example

import { Emulator } from "gotron/lib/device/Emulator";

(async () => {
  const device = await Emulator.newEmulator(5555, "AVD NAME HERE");
  // ... Use the device
})();

static async getInstalledDevices(): Promise<string[]>

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.

Example

import { Emulator } from "gotron/lib/device/Emulator";

(async () => {
  const avd = (await Emulator.getInstalledDevices())[0];
  const device = await Emulator.newEmulator(5555, avd);
  // ... Use the device
})();

USBDevice


A USB Device is connected to your computer using a cable. The device has to have usb debugging enabled for this connection to work.

static connectToDevice(serialId: string): USBDevice

Connects to a device with the serialId. Returns a USBDevice instance.

Example

import { USBDevice } from "./lib/device/USBDevice";

(async () => {
  const device = await USBDevice.connectToDevice("RJSGEH9865");
  // ... Use the device
})();