Complete Java Example
2 min
this example lists a device, creates an allocation, starts and stops one session, then releases the allocation it uses try with resources and attempts cleanup even when the test flow fails add the sdk dependency described in installation /getting started/installation , then set credentials export devicepark client id=your client id export devicepark client secret=your client secret create deviceparkquickstart java import io testinium devicepark deviceparkapiclient; import io testinium devicepark authentication credentials credentials; import io testinium devicepark model allocation allocation; import io testinium devicepark model allocation deviceallocationrequest; import io testinium devicepark model common pagedto; import io testinium devicepark model devices device; import io testinium devicepark model devices listdevicesrequest; import io testinium devicepark model sessions devicestartsessionrequest; import io testinium devicepark model sessions session; public final class deviceparkquickstart { private deviceparkquickstart() { } public static void main(string\[] args) throws exception { string allocationid = null; string sessionid = null; try (deviceparkapiclient client = deviceparkapiclient builder() url("https //devicepark testinium io") credentials(credentials of( system getenv("devicepark client id"), system getenv("devicepark client secret"))) timeout(60) build()) { try { pagedto\<device> devices = client devices() list( listdevicesrequest builder() page(0) size(20) build()); device device = devices data() stream() filter(candidate > candidate serial() != null) findfirst() orelsethrow(() > new illegalstateexception( "no visible device has a serial number")); allocation allocation = client allocations() create( deviceallocationrequest builder() serial(device serial()) priority(3) build()); allocationid = requirevalue( allocation allocationid(), "allocation response does not contain allocationid"); if (allocation deviceserial() == null) { throw new illegalstateexception( "allocation " + allocationid \+ " is queued at position " + allocation position()); } session session = client sessions() start( devicestartsessionrequest builder() allocationid(allocationid) videorecording(true) build()); sessionid = requirevalue( session sessionid(), "session response does not contain sessionid"); system out printf( "allocationid=%s sessionid=%s deviceserial=%s%n", allocationid, sessionid, session deviceserial()); // run appium automation here while the session is active } finally { if (sessionid != null) { try { client sessions() stop(sessionid); } catch (runtimeexception cleanuperror) { system err println("could not stop session " + sessionid); } } if (allocationid != null) { try { client allocations() delete(allocationid); } catch (runtimeexception cleanuperror) { system err println("could not release allocation " + allocationid); } } } } } private static string requirevalue(string value, string message) { if (value == null || value trim() isempty()) { throw new illegalstateexception(message); } return value; } } if the allocation enters the queue, this minimal example reports its position and releases it during cleanup a production runner should keep the same allocationid , inspect list allocations /allocations/list allocations until deviceserial is assigned, and enforce its own maximum wait time continue understand allocation targeting and queue behavior /allocations/create allocationreview java failure behavior docid\ crq4ngnidpa tsqqpzboqreview exact method signatures /reference/sdk methods
