All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.testvagrant.optimus.mdb.android.Android Maven / Gradle / Ivy

Go to download

Optimus Lite API to manage test devices and create appium driver based on platform

There is a newer version: 0.1.7-beta
Show newest version
package com.testvagrant.optimus.mdb.android;

import com.testvagrant.optimus.core.models.OptimusSupportedPlatforms;
import com.testvagrant.optimus.core.models.TargetDetails;
import com.testvagrant.optimus.core.models.mobile.DeviceType;
import com.testvagrant.optimus.mdb.CommandExecutor;
import com.testvagrant.optimus.mdb.Mobile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.testvagrant.optimus.mdb.android.Commands.*;

public class Android extends Mobile {

  private final CommandExecutor commandExecutor;

  public Android() {
    commandExecutor = new CommandExecutor();
  }

  @Override
  public List getDevices() {
    return collectDeviceDetails();
  }

  private List collectDeviceDetails() {
    List devices = commandExecutor.exec(LIST_ALL_DEVICES).asList();

    List collectedDevices =
        devices.parallelStream()
            .filter(line -> !(line.equals(ADB_HEADER)) && !line.contains("offline"))
            .collect(Collectors.toList());

    if (collectedDevices.size() == 0) {
      throw new RuntimeException("Could not find any devices, are any devices available/online?");
    }

    return initDevices(collectedDevices);
  }

  private boolean isRealDevice(String process) {
    return !process.contains("vbox")
        && !process.startsWith("emulator")
        && !process.startsWith("* daemon");
  }

  private List initDevices(List processLog) {
    List devices = new ArrayList<>();
    for (String process : processLog) {
      DeviceType deviceType = isRealDevice(process) ? DeviceType.DEVICE : DeviceType.EMULATOR;
      TargetDetails device = buildDeviceDetails(process, deviceType);
      devices.add(device);
    }
    return devices;
  }

  private TargetDetails buildDeviceDetails(String process, DeviceType devicetype) {
    String udid = getUDID(process);
    String model = getModel(udid);
    String osVersion = getOSVersion(udid);
    return TargetDetails.builder()
        .udid(udid)
        .name(model)
        .platform(OptimusSupportedPlatforms.ANDROID)
        .platformVersion(osVersion)
        .runsOn(devicetype)
        .build();
  }

  private String getUDID(String process) {
    String uid;
    int uidLastChar = process.indexOf(" ");
    uid = process.substring(0, uidLastChar);
    return uid;
  }

  private String getModel(String UID) {
    String command = String.format(GET_DEVICE_MODEL, UID);
    return commandExecutor.exec(command).asLine().replace("\n", "");
  }

  private String getOSVersion(String UID) {
    String command = String.format(GET_DEVICE_OS, UID);
    return commandExecutor.exec(command).asLine().replace("\n", "");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy