0

I'm working to setup my test infrastructure on an Android app. I'm using Android Studio, gradle, Espresso, MonkeyRunner, emulators, and real devices. I run AndroidTest using gradlew connectedAndroidTest.

In setting up proper test fixtures, I'm interested in having a background process that is started up before the tests start, then torn down when they're done. Is there a way to do that with gradle, etc?

Specifically what I'm trying to do right now is do a screen capture with MonkeyRunner in the middle of an Espresso test. Spinning up MonkeyRunner in the middle of the test is time-consuming so having it running as a daemon could speed the test up. (I understand the whole thing is a bit hokey, but ultimately I'm looking for an efficient way to do screen capture compares. If it was built into the Java libs in a more native way I'd do it that way). In this case, the MonkeyRunner process would spin up and connect to the device. The AndroidTest process would then spin up, run a test which may signal the MonkeyRunner process to take a snapshot and compare it to a reference, then AndroidTest continues to the next test which may do the same thing.

Other things the separate process pattern might enable would be external comms, or other things I may need for a test fixture.

TinyTheBrontosaurus
  • 4,010
  • 6
  • 21
  • 34
  • Why don't you try using jenkins ?? I've been using it for a while and I have a setup which boots up a virtual machine and run tests there and a whole lot. – Jaswanth Manigundan May 16 '17 at 02:45
  • is it an android emulator you're spinning up? when I switch to Jenkins, Jenkins will still be in a vm making it difficult to spin up an emulator. arbitrary vm's are not a problem – TinyTheBrontosaurus May 16 '17 at 03:32
  • In my experience, emulators are hard to spin up anywhere !! So I always use the test devices connected and let jenkins run over the night. – Jaswanth Manigundan May 16 '17 at 03:33
  • right. and I can't connect usb to the vm with Xen – TinyTheBrontosaurus May 16 '17 at 03:34
  • Oh ! I've not used that ! Anyway. Thats all I've got :-) – Jaswanth Manigundan May 16 '17 at 03:36
  • Sorry @JaswanthManigundan, I thought you were commenting on my other question: http://stackoverflow.com/questions/43987837/how-do-i-setup-android-continuous-integration-on-a-vm which basically lays out the Jenkins/VM/USB issue. Question: Can I use Jenkins to do all that stuff (screenshots, writing unit tests)? I thought I would just use Jenkins to call `gradlew` and parse the JUnit – TinyTheBrontosaurus May 16 '17 at 13:29

1 Answers1

0

If you are already creating your tests using Java there's no reason to use MonkeyRunner to take the screenshot. You should use takeScreenshot().

final UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
final String filename = "/path/to/screenshot.png";
device.takeScreenshot(new File(filename));
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134