Check for available devices in C#

You can use a REST API call to check if devices with the desired specifications are available before actually attempting to initialize the Appium driver. This way, you avoid a driver initialization error in case no such devices are available for the test to start. This error would show up in Perfecto's Report Library as a blocked test.

The following example is based on these sample API scripts:

It uses the GetResponse() method implementation.

The REST API returns an XML response that includes the list of available devices of a specific OS, OS version, and model. The list is based on the specific cloud user running the test, authenticated with the Perfecto security token.

Copy
private static string CloudName = "<cloud_name>";
private static string ServerURL = "https://" + CloudName + ".perfectomobile.com";
private static string SecurityToken = "<security_token>";
private static string User = "<cloud_user>";


public static Boolean AvailableDevices(String platformName, String platformVersion, String model)
    {
        XmlDocument deviceList = RetrieveAvailableDevicesList(platformName, platformVersion, model);
        int availableDevices = deviceList.GetElementsByTagName("handset").Count;

        if (availableDevices!=0)
        {
            Console.WriteLine("Available devices: " + availableDevices);
            return true;
        } else
        {
            return false;
        }
    }

private static XmlDocument RetrieveAvailableDevicesList(String platformName, String platformVersion, String model)
    {
        String operation = "/services/handsets?operation=list";

        Dictionary<String, String> parameters = new Dictionary<String, String>();
        //device specifics
        parameters.Add("os", platformName);
        parameters.Add("osVersion", platformVersion);
        parameters.Add("model", model);
        //availability parameters
        parameters.Add("inUse", "false");
        parameters.Add("availableTo", User);
        //authorization
        parameters.Add("securityToken", SecurityToken);

        String response = GetResponse(operation, parameters);

        XmlDocument deviceList = new XmlDocument();
        deviceList.LoadXml(response);
        return deviceList;
    }

Following is a usage example.

Tip: Access the AvailableDevices() static method as part of a separate DeviceAPI class. Any of the following defined device specification values could be an empty string as well.
Copy
//.............

AppiumDriver<IWebElement> driver;
            
String platformName = "<device_OS>";
String platformVersion = "<device_OS_version>";
String deviceModel = "<device_model>";


DesiredCapabilities capabilities = new DesiredCapabilities();

//run the tests on devices of the specified platform
capabilities.SetCapability("platformName", platformName);
capabilities.SetCapability("platformVersion", platformVersion);
capabilities.SetCapability("model", deviceModel);

//authenticate to the Perfecto cloud 
capabilities.SetCapability("securityToken", SecurityToken);

//check for available devices in up to 60 retries per 1 second interval
//modify the values according to your tests' expected duration
int retry = 1;
int interval = 1000;
while (retry <= 60 && driver == null)
    {
        if(DeviceAPI.AvailableDevices(platformName, platformVersion, deviceModel))
        {
            Console.WriteLine("Initializing driver..");
            driver = new AndroidDriver<IWebElement>(url, capabilities);
            //driver = new IOSDriver<IWebElement>(url, capabilities);
        } else
        {
            Console.WriteLine("Attempt: " + retry + ". No available devices. Retrying.....");
            Thread.Sleep(interval);
            ++retry;
        }
    }

            //if retries limit is reached but still no available device to initialize the driver:
            if (driver == null)
    {
            throw new WebDriverException("Could not initialize driver.");
    }