安卓:从自动化测试创建/数据/本地/ tmp下的文件 [英] Android : Create a file in /data/local/tmp from an automated test

查看:1892
本文介绍了安卓:从自动化测试创建/数据/本地/ tmp下的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:
在设备上的自动测试过程中采取的屏幕截图。亚行通过拉截图文件一旦测试完成。

Goal: Taking a screenshot during an automated test on a device. Pull the screenshot file using adb once the test is done.

上下文:

目前,我正在尝试写自动化测试取设备屏幕的快照。使用的UIDevice 导航,我想借一截图在测试的中间。的UIDevice有一个方法<一href=\"http://developer.android.com/reference/android/support/test/uiautomator/UiDevice.html#takeScreenshot(java.io.File,%20float,%20int)\"相对=nofollow> takeScreenshot 我打电话的时候,我想拍摄快照。

I'm currently trying to write automated tests to take snapshots of the device screen. Using UiDevice to navigate, I would like to take a screenshot in the middle of a test. UiDevice has a method takeScreenshot that I call when I would like to take a snapshot.

经过一番调查后,我意识到,负责该类写入图像插入文件<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4_r1/com/android/uiautomator/core/UiAutomatorBridge.java#UiAutomatorBridge.takeScreenshot%28java.io.File%2Cint%29\"相对=nofollow> UiAutomatorBridge 捕捉到一个例外:

After some investigation, I realised that the class responsible to write the image into file UiAutomatorBridge catches an Exception :

java.io.FileNotFoundException:
  /data/local/tmp/screenshots/screen2.png:打开失败:EACCES
  (拒绝)

java.io.FileNotFoundException: /data/local/tmp/screenshots/screen2.png: open failed: EACCES (Permission denied)

使用 ADB ,我创建的文件和设置的所有权限给所有用户

Using adb, I created the file and set all permissions to all users.

adb shell touch /data/local/tmp/screenshots/screen1.png
adb shell chmod 777 /data/local/tmp/screenshots

一旦这样做,我可以拿一个截图:

Once done, I can take a screenshot with :

@Test
public void takeSnapShot() {
    String filename = "/data/local/tmp/screenshots/screen1.png";
    File file = new File(filename);
    assertEquals(true, mDevice.takeScreenshot(file));
}

问题:

我希望能够创建直接在测试的执行文件,而不需要使用亚行。
我使用的 createNewFile

I would like to be able to create a file directly while the test is executing, without the need of using adb. I tried to create a file directly from Java using createNewFile.

try {
     file.createNewFile();
} catch (IOException e) {
     e.printStackTrace();
}

不过,我得到一个IOException异常。

But I get an IOException

java.io.IOException异常:打开失败:EACCES(拒绝)

java.io.IOException: open failed: EACCES (Permission denied)

有没有人发生了什么事的想法?我很新的Linux,所以不要犹豫,建议的东西,即使它似乎是显而易见的你;)

Has anyone an idea of what is going on ? I'm quite new to Linux, so don't hesitate to suggest something even if it seems obvious to you ;)

我应该张贴此在超级呢?

修改

该目录的 /数据的具有这些权限

The directory /data has these permissions

drwxrwx--x system   system            2016-01-14 14:03 data

我不能列出的 /数据的,这使我相信用户壳并不属于该组的系统的内容。我不能列出的内容的 /数据/本地的也没有。

I can't list the content of /data, which makes me believe the user "shell" doesn't belong to the group "system". I can't list the content of /data/local neither.

然而, /数据/本地/ tmp目录的是由壳拥有。

However, the /data/local/tmp is owned by "shell".

drwxrwx--x shell    shell             2016-01-14 12:20 . (tmp)

/数据/本地/ tmp目录的给予+ X权限给所有用户。

/data/local/tmp gives +x permission to all users.

最后,目录中的截屏属于具有权限的外壳777。

Finally, the directory "screenshots" belongs to shell with permissions 777.

drwxrwxrwx shell    shell             2016-01-14 11:46 screenshots

据我了解,任何用户应该能够访问的 /数据/本地的/ tmp /截图

推荐答案

而不是下节省的 /数据/本地的/ tmp / 的,使用 Environment.getExternalStorageDirectory()
你需要一个读/写权限做到这一点。你必须将以下两行添加到应用程序的清单! (我想这些行加入到/androidTest/Manifest.xml文件,但它没有影响)。

Instead of saving under /data/local/tmp/, use Environment.getExternalStorageDirectory(). You need a read/write permission to do that. You have to add these two lines into the Manifest of the application ! (I tried to add those lines into the /androidTest/Manifest.xml file, but it has no effects).

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

虽然它解决找到一个共同的目录来保存/读屏的问题,它带来了一个新的为好。

While it solves the problem of finding a common directory to save/read screenshots, it brings a new one as well.

无需添加这些权限2是不是,如果你已经有了他们什么大不了的事。但是,如果你不想询问用户对这些权限,必须添加/删除这些行每次测试/登录您的应用程序,这是很不理想的时间。

Having to add these 2 permissions isn't a big deal if you already had them. But if you don't want to ask the user for those permissions, you have to add/remove these lines every time you test/sign your application, which is far from ideal.

编辑:

在<一个href=\"http://stackoverflow.com/questions/25172608/testing-permissions-and-an-android-apps-manifest-file\">this帖子,有人提出利用构建差异(调试,发布)中有两个不同的清单。

In this post, someone proposed to make use of the build differentials (debug, release) to have two different Manifests.

在最后,我有3个舱单:

In the end, I have 3 Manifests :


  • 发布清单(无外部读/写权限)

  • 调试清单(与外部读/写权限)

  • AndroidTest舱单

工具:overrideLibrary =android.support.test.uiautomator.v18

tools:overrideLibrary="android.support.test.uiautomator.v18"

这篇关于安卓:从自动化测试创建/数据/本地/ tmp下的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆