test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/HttpServer.java in calabash-android-0.2.17 vs test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/HttpServer.java in calabash-android-0.2.18

- old
+ new

@@ -2,10 +2,11 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.util.Properties; +import java.util.List; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import android.graphics.Bitmap; @@ -81,14 +82,14 @@ return new Response(HTTP_OK, MIME_HTML, Boolean.toString(ready)); } else if (uri.endsWith("/screenshot")) { Bitmap bitmap; - View v1 = InstrumentationBackend.solo.getViews().get(0).getRootView(); - v1.setDrawingCacheEnabled(true); - bitmap = Bitmap.createBitmap(v1.getDrawingCache()); - v1.setDrawingCacheEnabled(false); + View rootView = getRootView(); + rootView.setDrawingCacheEnabled(true); + bitmap = Bitmap.createBitmap(rootView.getDrawingCache()); + rootView.setDrawingCacheEnabled(false); ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); return new NanoHTTPD.Response( HTTP_OK, "image/png", new ByteArrayInputStream(out.toByteArray())); } @@ -103,9 +104,26 @@ String result = toJson(runCommand(commandString)); System.out.println("result:" + result); return new NanoHTTPD.Response( HTTP_OK, MIME_HTML, result); } + + private View getRootView() { + for ( int i = 0; i < 25; i++) { + try { + View rootView = InstrumentationBackend.solo.getCurrentActivity().getWindow().getDecorView(); + if (rootView != null) { + return rootView; + } + System.out.println("Retry: " + i); + + Thread.sleep(200); + } catch (Exception e) { + } + } + + throw new RuntimeException("Could not find any views"); + } private ObjectMapper createJsonMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, true); return mapper;