Reserve an available device at runtime

With the Perfecto REST API, you can check device availability and reserve available devices during run time. To access the relevant functions, add the PerfectoReservationOptimizer.jar to your project. You can also retrieve the file from our community code site at the Perfecto Community GitHub.

The following code sample is a simple demo of how to reserve a device without manual interaction.

Restriction: You must be an admin user to perform this action.
Copy
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.json.simple.parser.ParseException;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.xml.sax.SAXException;

import com.perfecto.reservation.PerfectoReservationActivity;

public class CreateDeviceReservation2 {
    
    static String cloudName = "partners.perfectomobile.com";

    static String securityToken = "personal_security_token";
 
public static void main(String args[] ) throws Exception, IOException, ParserConfigurationException, SAXException, ParseException{
    
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("platformName", "iOS");
    caps.setCapability("platformVersion", "13.*");
    
    PerfectoReservationActivity activity = new PerfectoReservationActivity(cloudName, securityToken);

    List<String> listofAvailableDevices = activity.getAvailableDevices(caps);
    
    String delim = ",";

    StringBuilder sb = new StringBuilder();

    int i = 0;
    while (i < listofAvailableDevices.size() - 1) {
        sb.append(listofAvailableDevices.get(i));
        sb.append(delim);
        i++;
    }
    sb.append(listofAvailableDevices.get(i));

    String availableDevices = sb.toString();
    
    System.out.println("List of Available Devices "+ availableDevices);
    
    activity.reserveDeviceNow(availableDevices, 60);
        
}
    
}
Restriction: This is not an official code sample offering from Perfecto. This code sample is intended as a working proof of concept to confirm that it is possible to write code to handle your cloud device reservations using Perfecto API calls.

Feel free to take the JAR to review how it is written, or to simply use it to write your own reservation tools.