Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't take a screenshot on Jenkins #265

Closed
brnrc opened this issue Jun 14, 2013 · 8 comments · Fixed by calabash/screenshotTaker#2
Closed

Can't take a screenshot on Jenkins #265

brnrc opened this issue Jun 14, 2013 · 8 comments · Fixed by calabash/screenshotTaker#2

Comments

@brnrc
Copy link

brnrc commented Jun 14, 2013

I can run the tests ok in my local VM. However, trying to run this on Jenkins the it breaks whenever I try to take a screenshot (commenting the screenshot part works).

2013-06-14 09:31:02 - Action: version - Params: 
2013-06-14 09:31:03 - Result:'{"bonusInformation":[],"message":"0.4.6","success":true}'
2013-06-14 09:31:03 - Client and server versions match. Proceeding...
2013-06-14 09:31:06 - connected_devices: ["localhost:57688"]
2013-06-14 09:31:06 - java -jar /Users/test/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.6/lib/calabash-android/lib/screenshotTaker.jar localhost:57688 screenshot_0.png
Could not take screenshot
java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:532)
    at java.nio.channels.SocketChannel.open(SocketChannel.java:146)
    at com.android.ddmlib.AdbHelper.getFrameBuffer(AdbHelper.java:280)
    at com.android.ddmlib.CalabashAdbHelper.getFrameBuffer(Unknown Source)
    at com.android.ddmlib.CalabashDevice.getScreenshot(Unknown Source)
    at sh.calaba.screenshot.ScreenshotTaker.takeScreenshot(Unknown Source)
    at sh.calaba.screenshot.ScreenshotTaker.main(Unknown Source)
@brnrc
Copy link
Author

brnrc commented Jun 14, 2013

Falling back to version calabash-android 0.4.3 work.
(was using 0.4.6)

@jhansche
Copy link
Contributor

One thing I had to do was add support to the screenshotTaker.jar and ddmlib to allow the adb port to be overridden by an environment variable, because we use the Android Emulator Plugin for Jenkins, and that assigns a random ADB port on each build. So had to update the jar to use the ANDROID_AVD_ADB_PORT env var, which is assigned by the plugin (and fall back to port 5073, which is the adb default, if that is not set)

@jonasmaturana
Copy link
Contributor

Are you using adb over wifi?

In that case I don't think taking screenshots over usb will work.

Try to upgrade your calabash-android and run with screenshots from the test server instead

SCREENSHOT_VIA_USB=false calabash-android run <apk>

@jhansche
Copy link
Contributor

It works by modifying the screenshotTaker.jar to connect to the adb server on a different port. The OP stated that this is broken on Jenkins, which almost certainly means he is referring to the Android Emulator Plugin for Jenkins. That plugin starts up an adb server on a random port and assigns that port number in the ANDROID_AVD_ADB_PORT environment variable. In order to get calabash's screenshot taker (which uses ddmlib) to work, I had to modify the source to have it override the default port (5037) with whatever is in that environment variable. That change does work and fixes the problem. I will submit a pull request to the screenshotTaker repo that has this change.

Actually, I made the change in a different repository (roman10/roman10-android-tutorial), but both projects take the example code from the core Android codebase. The change is roughly:

diff --git screenshot/src/com/android/ddmlib/AndroidDebugBridge.java screenshot/src/com/android/ddmlib/AndroidDebugBridge.java
index 950e313..0c2d83a 100755
--- screenshot/src/com/android/ddmlib/AndroidDebugBridge.java
+++ screenshot/src/com/android/ddmlib/AndroidDebugBridge.java
@@ -62,8 +62,18 @@ public final class AndroidDebugBridge {
     static {
         // built-in local address/port for ADB.
         try {
+            int port = ADB_PORT;
+
+            // Allow the server port to be overridden
+            String envVar = System.getenv("ANDROID_ADB_SERVER_PORT");
+
+            if (envVar != null) {
+              port = Integer.parseInt(envVar);
+              System.out.println("Overriding server port: " + port);
+            }
+
             sHostAddr = InetAddress.getByName(ADB_HOST);
-            sSocketAddr = new InetSocketAddress(sHostAddr, ADB_PORT);
+            sSocketAddr = new InetSocketAddress(sHostAddr, port);
         } catch (UnknownHostException e) {

         }

@jonasmaturana
Copy link
Contributor

@jhansche Awesome! Do you want me to just add it to screenshotTaker or do you prefer to make the PR?

@jhansche
Copy link
Contributor

Either way.. I just uploaded the PR: calabash/screenshotTaker#2

Tested and it works.

@brnrc
Copy link
Author

brnrc commented Jul 1, 2013

@jhansche Why did you use the ANDROID_ADB_SERVER_PORT instead of ANDROID_AVD_ADB_PORT?

@jhansche
Copy link
Contributor

@brucardoso2 because that is what the adb tool looks for also :)
https://github.com/android/platform_system_core/blob/master/adb/commandline.c#L948

ANDROID_ADB_SERVER_PORT is the one that ddmlib needs to communicate with the connected adb server. The two are not interchangeable: https://github.com/jenkinsci/android-emulator-plugin/blob/master/src/main/java/hudson/plugins/android_emulator/AndroidEmulator.java#L482

Also, ANDROID_AVD_ADB_PORT is a concept used only by the Jenkins plugin. No other Android-related tool makes use of that variable, and it does not make sense to make this utility use a port that is only dictated by a Jenkins plugin. Instead, if adb devices and adb logcat will work using an exported ANROID_ADB_SERVER_PORT variable, then the same should also be true of using screenshotTaker.jar.

The full list of environment variables exported by the Jenkins plugin is at: https://wiki.jenkins-ci.org/display/JENKINS/Android+Emulator+Plugin#AndroidEmulatorPlugin-Environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants