Sam Eeles
Sam Eeles

Reputation: 23

Appium Android Emulator not closing or making tests fail

I have an Appium test that I am trying to run against and Android Emulator using the stock browser. This test passes on iOS however on Android it fails half way between the test because Android seems a little more fussy about element locators than iOS.

Anyway my problem is that I would expect it to fail at this point because it can't find the element it tries to click, however the test then just hangs (for around 10 minutes when it is then killed by the socket timeout), and doesn't cause the test to fail. The emulator just stays open and the test looks like it's continuing to run.

I have the latest version of Appium installed via npm.

Here are my desired capabilities:

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", "browser");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("takesScreenshot", true);
        capabilities.setCapability("version", "5.1.1");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("avd", "nexus5");
        webDriver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                                      capabilities);

My Appium log shows the following:

        info: Got response with status 200: {"sessionId":"27038f591907917c7f2c1ce48db7d032","status":13,"value":{"message":"unknown error: Element is not clickable at point (342, 32). Other element would receive the click: <button class=\"butto...
        info: <-- POST /wd/hub/session/27038f591907917c7f2c1ce48db7d032/element/0.9723546949680895-1/click 200 825.852 ms - 381 
        info: --> GET /wd/hub/session/27038f591907917c7f2c1ce48db7d032/screenshot {}
        info: Proxying [GET /wd/hub/session/27038f591907917c7f2c1ce48db7d032/screenshot] to [GET http://127.0.0.1:9515/wd/hub/session/27038f591907917c7f2c1ce48db7d032/screenshot] with body: {}
        info: [debug] Didn't get a new command in 30 secs, shutting down...
        info: Shutting down appium session
        info: Proxying [DELETE /] to [DELETE http://127.0.0.1:9515/wd/hub/session/27038f591907917c7f2c1ce48db7d032] with no body

I would expect my test to exit at this point due to it not being able to click the element. Could any one offer any advice?

Thanks

UPDATE

Added the code where I take a screenshot which looks like is what is making my test hang:

  public void captureScreenshot(String methodName) {
                try {
                    new File(screenshotDirectory).mkdirs();
                    String filename = methodName + ".png";
                    File screenshot = ((TakesScreenshot) webDriverService.getWebDriver()).getScreenshotAs(OutputType.FILE);
                    FileUtils.copyFile(screenshot, new File(screenshotDirectory + filename));
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            }

Upvotes: 2

Views: 1410

Answers (2)

Vish
Vish

Reputation: 2164

(I originally posted this answer here: https://discuss.appium.io/t/appium-hangs-when-taking-a-screenshot-android/5318/4?u=savishy)

I know this is a slightly old topic, but with the Appium Java client 3.2.0 I faced this issue and thought I'd chime in with my solution.

Here's the line of code where the hang happens. I know because the previous log.debug line gets printed.

    log.debug(filePath);
    File file  = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

And here is the Appium Server Log:

2015-11-26 09:20:03:085 - info: <-- POST /wd/hub/session/93df24a9-1b33-49c8-aea1-b3b768f21d97/element 200 1210.866 ms - 87 {"status":0,"value":{"ELEMENT":"1"},"sessionId":"93df24a9-1b33-49c8-aea1-b3b768f21d97"}
2015-11-26 09:20:03:101 - info: --> GET /wd/hub/session/93df24a9-1b33-49c8-aea1-b3b768f21d97/screenshot {}
2015-11-26 09:20:03:103 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "/system/bin/rm /data/local/tmp/screenshot.png; /system/bin/screencap -p /data/local/tmp/screenshot.png "

Further debugging: look at /data/local/tmp

I took a look at the /data/local/tmp device folder. Here are the contents, their permissions and timestamps:

$ adb shell ls -l /data/local/tmp
-rw-rw-rw- shell    shell    20456741 2015-11-25 21:41 0978df74a3eeb9a231b2e0cb5a1f7e73.apk
-rw-rw-rw- shell    shell       48797 2015-10-10 03:23 AppiumBootstrap.jar
-rw-rw-rw- shell    shell        2554 2015-07-25 23:40 UIAutomatorTest.jar
drwxrwxrwx shell    shell             2015-07-25 23:36 dalvik-cache
-rw------- root     root         8529 2015-11-18 15:38 dump.xml
drwx------ shell    shell             2015-08-07 16:37 local
drwxrwxrwx shell    shell             2015-10-14 15:33 mobizen
-rw-rw-r-- root     root       379501 2015-11-18 15:38 screenshot.png
-rw-rw-rw- shell    shell       53503 2015-11-26 14:49 strings.json
-rw------- shell    shell       13965 2015-11-19 14:20 uidump.xml

As you can see, there is a file screenshot.png from about a week ago. (presumably captured by an old Appium Java run).

Next: try deleting manually using adb shell

Next - I tried deleting this file using the exact adb shell command used by Appium. Here's the output. Notice the prompt from shell?

vish@freeman /cygdrive/d/vish/ma-app-verification-android/appium-projects/AppiumAndroid
$ adb -s TA64303N7W shell "/system/bin/rm /data/local/tmp/screenshot.png"
override rw-rw-r-- root:root for '/data/local/tmp/screenshot.png'?

(drum-roll)

The prompt seen above is what is causing Appium to hang.

Luckily, my phone is rooted, so I was able to remove the offending screenshot manually as root:

vish@freeman /cygdrive/d/vish/ma-app-verification-android/appium-projects/AppiumAndroid
$ adb root

vish@freeman /cygdrive/d/vish/ma-app-verification-android/appium-projects/AppiumAndroid
$ adb shell rm /data/local/tmp/screenshot.png

Upvotes: 1

Sam Eeles
Sam Eeles

Reputation: 23

Managed to fix it had to use a slightly different piece of code to take a screenshot on android:

        public void captureScreenshot(String methodName) {
            try {
                new File(screenshotDirectory).mkdirs();
                String filename = methodName + ".png";
                AppiumDriver webDriver = (AppiumDriver) webDriverService.getWebDriver();
                webDriver.context("NATIVE_APP");
                File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(screenshot, new File(screenshotDirectory + filename));
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    };

Upvotes: 0

Related Questions