Reserve an available device at runtime

It is possible to check device availability and reserve available devices during run time with the Perfecto REST API. To do this, add the PerfectoReservationOptimizer.jar to your project to access those functions.

You can also retrieve the JAR from our community code site, at the Perfecto Community GitHub.

The following code sample is a simple demo of how we 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 is a code sample as a working proof of concept, to confirm that a customer can write code to handle their own cloud device reservations, using our 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.