@Override public void stopActivity(String activity) { Validate.notNullOrEmpty(activity, "Activity you want to stop can not be a null object nor an empty string!"); if (!activity.contains(".")) { throw new IllegalArgumentException("Stopping of activity equals to killing the package it belongs to. It seems " + "you have not specified FQDN of activity you want to stop so package name can not be extracted from it."); } device.executeShellCommand("am kill " + activity.substring(0, activity.lastIndexOf("."))); }
/** * This method extracts a port number from the serial number of a device. It assumes that the device name is of format * [xxxx-nnnn] where nnnn is the port number. * * @param device The device to extract the port number from. * @return Returns the port number of the device */ private int extractPortFromDevice(AndroidDevice device) { String portStr = device.getSerialNumber().substring(device.getSerialNumber().lastIndexOf("-") + 1); if (portStr != null && portStr.length() > 0) { try { return Integer.parseInt(portStr); } catch (NumberFormatException e) { return -1; } } // If the port is not available then return -1 return -1; }
private void installServerAPK(AndroidDevice device, File apk) throws AndroidExecutionException { if (device.isPackageInstalled(APK_APP_NAME)) { log.info("Package " + APK_APP_NAME + " is installed, trying to uninstall it."); device.uninstallPackage(APK_APP_NAME); } device.installPackage(apk, true); }
public void uninstall(@Observes(precedence = 10) AndroidContainerStop event, AndroidDevice device) { Validate.notNull(device, "Injected Android device for is null!"); try { device.uninstallPackage(APK_APP_NAME); log.info("Uninstallation of Android server for Arquillian Droidium web was performed."); } catch (AndroidExecutionException ex) { log.info("Uninstallation of Android server for Arquillian Droidium web failed."); } try { log.log(Level.INFO, "Removing port forwaring from {0} to {1} for Android server of Arquillian Droidium web support.", new Object[] { device.getDroneHostPort(), device.getDroneGuestPort() }); device.removePortForwarding(device.getDroneHostPort(), device.getDroneGuestPort()); log.info("Removing of port forwarding for Arquillian Droidium web support was successful. "); } catch (AndroidExecutionException ex) { log.info("Removing of port forwarding for Arquillian Droidium Web support after Android server " + "was uninstalled failed."); } androidServerUninstalled.fire(new AndroidServerUninstalled()); } }
@Override public void deviceDisconnected(IDevice delegate) { if (delegate.isEmulator()) { if (delegate.getAvdName().equals(this.device.getAvdName())) { offline = true; logger.fine("Device id=" + delegate.getAvdName() + " disconnected from ADB bus."); } } else { if (delegate.getSerialNumber().equals(this.device.getSerialNumber())) { offline = true; logger.fine("Device id=" + delegate.getSerialNumber() + " disconnected from ADB bus."); } } }
.parameter(device.getSerialNumber()) .parameter("install") .parameter(deployment.getResignedApk().getAbsolutePath()) if (device.isPackageInstalled(applicationBasePackage)) { device.uninstallPackage(applicationBasePackage); if (!device.isPackageInstalled(applicationBasePackage)) { throw new AndroidExecutionException("Application " + applicationBasePackage + " was not installed on device " + device.getSerialNumber() + ".");
public void install(@Observes AndroidDeviceReady event) throws AndroidExecutionException, IOException { AndroidDevice device = event.getDevice(); installServerAPK(device, configuration.get().getServerApk()); log.info("Installation of Android Server APK for Arquillan Droidium web support was performed."); log.info("Starting Android Server for Arquillian Droidium web testing."); WebDriverMonkey monkey = new WebDriverMonkey(configuration.get().getLogFile()); device.executeShellCommand(getWebDriverHubCommand().toString(), monkey); log.info("Waiting until Android server for Arquillian Droidium web support is started."); waitUntilStarted(device, monkey); log.log(Level.INFO, "Creating port forwarding from {0} to {1} from Android server to Arquillian Droidium web plugin.", new Object[] { device.getDroneHostPort(), device.getDroneGuestPort() }); device.createPortForwarding(device.getDroneHostPort(), device.getDroneGuestPort()); androidServerInstalled.fire(new AndroidServerInstalled()); }
public void shutdownEmulator(@Observes AndroidContainerStop event) throws AndroidExecutionException { AndroidDevice device = androidDevice.get(); AndroidContainerConfiguration configuration = this.configuration.get(); androidDeviceRegister.get().remove(device); if (device != null && device.isEmulator() && !device.getAlreadyRuns()) { logger.log(Level.INFO, "Stopping Android emulator of AVD name {0}.", configuration.getAvdName()); CountDownWatch countdown = new CountDownWatch(configuration.getEmulatorShutdownTimeoutInSeconds(), TimeUnit.SECONDS); logger.info("Waiting " + countdown.timeout() + " seconds for emulator " + device.getAvdName() + " to be disconnected and shutdown."); Spacelift.task(device, EmulatorShutdownTask.class).countdown(countdown).execute().await(); logger.info("Device " + device.getAvdName() + " on port " + device.getConsolePort() + " was disconnected in " + countdown.timeElapsed() + " seconds."); if (configuration.isAVDGenerated()) { androidVirtualDeviceDelete.fire(new AndroidVirtualDeviceDelete()); } androidEmulatorShuttedDown.fire(new AndroidEmulatorShuttedDown(device)); } }
.addEnvironment(sdk.getPlatformConfiguration().getAndroidSystemEnvironmentProperties()) .command(new CommandBuilder(androidSDK.get().getAdbPath()) .parameters("-s", androidDevice.getSerialNumber(), "shell", "getprop")) .execute().until(watch, EmulatorStatusCheckTask.isBootedCondition); .serialNumber(androidDevice.getSerialNumber()) .sdk(sdk) .execute().await(); androidDevice.setDroneHostPort(configuration.getDroneHostPort()); androidDevice.setDroneGuestPort(configuration.getDroneGuestPort());
private AndroidDevice getPhysicalDevice() throws AndroidExecutionException { String serialId = configuration.get().getSerialId(); List<AndroidDevice> devices = androidBridge.get().getDevices(); if (devices == null || devices.size() == 0) { throw new AndroidExecutionException("There are no devices on the Android bridge."); } for (AndroidDevice device : devices) { if (!device.isEmulator() && serialId.equals(device.getSerialNumber())) { logger.info("Detected physical device with serial ID " + serialId + "."); return device; } } throw new AndroidExecutionException("Unable to get device with serial ID " + serialId + "."); }
private void setDronePorts(AndroidDevice device) { device.setDroneHostPort(configuration.get().getDroneHostPort()); device.setDroneGuestPort(configuration.get().getDroneGuestPort()); }
private AndroidDevice getVirtualDevice(String consolePort, String avdName) throws AndroidExecutionException { Validate.notNullOrEmpty(consolePort, "Console port to get emulator of is a null object or an empty string."); Validate.notNullOrEmpty(avdName, "AVD name to get emulator of is a null object or an empty string."); List<AndroidDevice> devices = androidBridge.get().getEmulators(); if (devices == null || devices.size() == 0) { throw new AndroidExecutionException("There are no emulators on the Android bridge."); } for (AndroidDevice device : devices) { try { if (device.getConsolePort().equals(consolePort) && device.getAvdName().equals(avdName)) { return device; } } catch (NullPointerException ex) { logger.severe("Unable to connect to the emulator. Please be sure you are running adb server."); } } return null; }
/** * Installs resigned Selendroid server which reflects Android application meant to be instrumented into Android device. * * @param deployment deployment to install to Android device * @throws IllegalArgumentException if {@code deployment} or {@code SelendroidDeployment#getResigned()} is a null object * @throws AndroidExecutionException */ public void install(SelendroidDeployment deployment) { Validate.notNull(deployment, "Selendroid deployment to deploy can not be a null object!"); Validate.notNull(deployment.getResigned(), "Resigned Selendroid application to deploy can not be a null object!"); Command selendroidInstallCommand = new Command(); selendroidInstallCommand.add(sdk.getAdbPath()) .add("-s") .add(device.getSerialNumber()) .add("install") .add(deployment.getResigned().getAbsolutePath()); logger.fine("Selendroid server installation command: " + selendroidInstallCommand.toString()); try { executor.execute(selendroidInstallCommand.getAsArray()); } catch (InterruptedException ex) { throw new AndroidExecutionException("Selendroid installation was interrupted."); } catch (ExecutionException ex) { throw new AndroidExecutionException("Unable to execute Selendroid installation process."); } if (!device.isPackageInstalled(deployment.getServerBasePackage())) { throw new AndroidExecutionException("Modified Selendroid server was not installed on device."); } }
public void shutdownEmulator(@Observes AndroidContainerStop event) throws AndroidExecutionException { AndroidEmulator emulator = androidEmulator.get(); AndroidDevice device = androidDevice.get(); AndroidContainerConfiguration configuration = this.configuration.get(); if (emulator != null && device.isEmulator()) { final ProcessExecutor executor = this.executor.get(); final Process p = emulator.getProcess(); CountDownWatch countdown = new CountDownWatch(configuration.getEmulatorShutdownTimeoutInSeconds(), TimeUnit.SECONDS); logger.info("Waiting " + countdown.timeout() + " seconds for emulator " + device.getAvdName() + " to be disconnected and shutdown."); try { final DeviceDisconnectDiscovery listener = new DeviceDisconnectDiscovery(device); AndroidDebugBridge.addDeviceChangeListener(listener); stopEmulator(p, executor, device, countdown); waitUntilShutDownIsComplete(device, listener, executor, countdown); AndroidDebugBridge.removeDeviceChangeListener(listener); if (configuration.isAVDGenerated()) { androidVirtualDeviceDelete.fire(new AndroidVirtualDeviceDelete()); } androidEmulatorShuttedDown.fire(new AndroidEmulatorShuttedDown(device)); } finally { executor.removeShutdownHook(p); } } }
@Override public void deviceDisconnected(IDevice device) { if (device.getAvdName().equals(connectedDevice.getAvdName())) { this.offline = true; } logger.fine("Discovered an emulator device id=" + device.getSerialNumber() + " disconnected from ADB bus"); }
if (!device.isOnline()) { throw new AndroidExecutionException("Android device is not online, can not take any screenshots."); RawImage rawImage = device.getScreenshot();
@Override public List<AndroidDevice> getEmulators() { Validate.notNull(delegate, "Android debug bridge must be set. Please call connect() method before execution"); List<AndroidDevice> emulators = new ArrayList<AndroidDevice>(); for (AndroidDevice device : getDevices()) { if (device.isEmulator()) { emulators.add(device); } } return emulators; }
private AndroidDevice getVirtualDeviceByConsolePort(String consolePort) throws AndroidExecutionException { Validate.notNullOrEmpty(consolePort, "Console port to get emulator of is a null object or an empty string."); List<AndroidDevice> devices = androidBridge.get().getEmulators(); if (devices == null || devices.size() == 0) { throw new AndroidExecutionException("There are no emulators on the Android bridge."); } for (AndroidDevice device : devices) { String deviceConsolePort = device.getConsolePort(); if (deviceConsolePort != null && deviceConsolePort.equals(consolePort)) { logger.log(Level.INFO, "Connecting to virtual device running on console port {0}.", consolePort); return device; } } throw new AndroidExecutionException("Unable to get Android emulator running on the console port " + consolePort); }
@Override public void deviceChanged(IDevice delegate, int changeMask) { if (this.delegate.equals(delegate) && (changeMask & IDevice.CHANGE_STATE) == 1) { if (device.isOnline()) { online = true; } } }
private void removePortForwarding(int from, int to) { validatePort(from); validatePort(to); logger.log(Level.FINE, "Removing port forwarding from {0} to {1}", new Object[] { from, to }); device.removePortForwarding(from, to); }