Reserve your device through the code before test execution

It is possible to check the list of available devices at runtime. This can be helpful when you reserve devices before the execution via the Perfecto REST API. Add the PerfectoReservationOptimizer.jar to your project to access those functions.

The following sample code shows how to get the list of available and unreserved devices. The reservation can be performed before instantiating the driver.

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 CreateDeviceReservation {
    
    static String cloudName = "partners.perfectomobile.com";

    static String securityToken = "1BUWU1zZHBSZ21LXzk4OGZwaUx6cWsxdWpZMnduZG5SQU50ejgyVHBNIn0.eyJqdGkiOiIxNTRiMjI3My1hOGE0LTQ1YzItYjNjOS0wYTcxNjUzZjI3NzYiLCJleHAiOjAsIm5iZiI6MCwiaWF0IjoxNTc1NjE1NjE0LCJpc3MiOiJodHRwczovL2F1dGgucGVyZmVjdG9tb2JpbGUuY29tL2F1dGgvcmVhbG1zL3BhcnRuZXJzLXBlcmZlY3RvbW9iaWxlLWNvbSIsImF1ZCI6Im9mZmxpbmUtdG9rZW4tZ2VuZXJhdG9yIiwic3ViIjoiNWE5OGU5NmMtYmU2Zi00YTk3LTg2ZDMtMWNjZmY0NWQyYjE4IiwidHlwIjoiT2ZmbGluZSIsImF6cCI6Im9mZmxpbmUtdG9rZW4tZ2VuZXJhdG9yIiwibm9uY2UiOiI3MjYxMjdmYS00ZjZhLTRiN2EtYjY5My0xY2YwMTVhMjZlNTQiLCJhdXRoX3RpbWUiOjAsInNlc3Npb25fc3RhdGUiOiJlMTNhOThiYi1mOGJmLTRlYzEtYTRmMy02MzhmYjEwMDA0YzgiLCJyZWFsbV9hY2Nlc3MiOnsicm9sZXMiOlsib2ZmbGluZV9hY2Nlc3MiXX0sInJlc291cmNlX2FjY2VzcyI6eyJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX19fQ.aKzGsr4cFJ9Q_bYXGEyuI9-Lg335Jh5Qw_kkImgIoSRR2_OhEN8zf52r6AxHgajhYMDVy_B9uVkcyMXStBis4ZIm8s14hFESmsayWLfk3bmF3g9QR6IGMq51ud-81sXj4pWVHvIXirU8LQcVfUtmcj5Q0xTj8F6IulBTdtXbKznsO0D-gj_damwySTIAEXG2V9lE5kiwUu1uMNl_WaPaN9uqLNWb-T0VEEZLj6BjPhI5-SB3JapFx_sEkNgXuLDHbnJ7QS5ZGo4F8dUfI5aekfAvojODwbWhYNRL0xS26JEXJGdnWYcCBmmAVea2mKBVSxSQmN8tFIY7ijoMacmuuQ";
     
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> availableDevices = activity.getAvailableDevices(caps);
    
    String delim = ",";

    StringBuilder sb = new StringBuilder();

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

    String res = sb.toString();
    System.out.println(res);
         
    System.out.println("List of Available Devices "+ availableDevices);
        
}