张亚洲 发表于 2014-12-30 14:37:53

【我分享】Robotium问答

本帖最后由 张亚洲 于 2014-12-30 14:41 编辑

目录[-]
如何开始?
Robotium支持什么安卓什么版本?
Robotium 支持在真机上测试吗?
Robotium支持那些功能?
使用Robotium写出的case,可以跨应用吗?
如果只有apk文件,能用Robotium进行测试吗?
能使用Robotium测试手机中预装的应用吗?
Robotium能测试杂合(web页面)的应用吗?
能使用Robotium进行截图吗?
如何在命令行下运行Robotium 测试用例当对文本和按钮进行点击操作时,为什么会报错?
可以使用localised strings进行测试吗?如何统计Robotium自动化case的覆盖率
            
如何开始?
      下载 Robotium.jar,将其加载到工程的build path中。更加详细的介绍,点击 此处。
Robotium支持什么安卓什么版本?
       Robotium支持安卓1.6,及以上
Robotium 支持在真机上测试吗?
         支持。只要将手机连接到电脑,就可以像在虚拟机上运行一样。对于一些定制的安卓版本,可能会有些问题,但是多数情况,都可以通过尝试Robotium的其他方法解决。
Robotium支持那些功能?
         可以通过Robotium在线 Javadoc,来查看Robotium的所有功能。也可以 下载这些文档。这些帮助文档以zip格式打成jar包。如果不知道怎么解压,可以将拓展名直接改为zip,然后解压。
使用Robotium写出的case,可以跨应用吗?
          不可以。在测试工程里的AndroidManifest.xml我们已经声明要测试的应用,只能测试这一个应用。如例子: <instrumentation android:targetPackage="com.example.android.notepad" android:name="android.test.InstrumentationTestRunner" /> 上面的代码表明,我们要测试 com.example.android.notepad这个包。android不允许我们对其之外的应用进行操作。
如果只有apk文件,能用Robotium进行测试吗?
         可以。使用Robotium不需要android程序源码。更多信息点击 此处。
能使用Robotium测试手机中预装的应用吗?
            可以,但是需要手机的root权限。详细点击 此处。
Robotium能测试杂合(web页面)的应用吗?
             可以。从Robotium 4.0开始,就支持含有web内容的android应用。
能使用Robotium进行截图吗?
            可以。使用 takeScreenshot()就可以截图手机屏幕,保存到 /sdcard/Robotium-Screenshots/中。但是需要我们在测试工程的AndroidManifest.xml文件中,打开写权限 android.permission.WRITE_EXTERNAL_STORAGE。
如何在命令行下运行Robotium 测试用例
            使用下面的命令: adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner com.android.foo是我们测试工程的包名。更多信息点击 此处
当对文本和按钮进行点击操作时,为什么会报错?
            如果这个问题发生在robotium支持的android版本中,可以试着在测试工程的AndroidManifest.xml文件中添加下面配置: <uses-sdk android:targetSdkVersion="YOUR_VERSION" /> 其中的 YOUR_VERSION:6--->android 2.0、 7--->android 2.1、 8--->android 2.2,最新的为17--->android 4.2。 如果上面代码有没生效,可以尝试在我们要测试的android工程中的AndroidManifest.xml文件中添加下面配置: <supports-screens android:anyDensity="true"/> 如果上述方法均无效,可以点击 此处反馈。
可以使用localised strings进行测试吗?
          Yes you can. You can usesolo.getString() and in there pass in the resource id of the string that you want to use. You will need to have the source code of the application under test to access the strings.
如何统计Robotium自动化case的覆盖率
          目前,需要使用ant(或者使用命令行)。 由于安全限制,获取覆盖率只能在有root权限的手机或者模拟器上进行。 下面是主要步骤:
[*]在命令行下运行android update test-project -m [目标应用路径] -p [测试文件夹路径],android这个命令是android sdk下的android.bat,运行这条命令后,在测试文件夹中会生成一个build.xml文件。
[*]确保Robotium jar包在你的测试工程目录下,一般在工程目录下的libs文件夹中。
[*]ant需要获取到目标应用的class文件和依赖的jar包(前提是你有目标应用的源码,并且可以进行build)。参见http://stackoverflow.com/questions/2472059/cant-build-and-run-an-android-test-project-created-using-ant-create-test-projec,下面是我的build.xml文件。
[*]运行ant coverage
[*]如果运行成功,ant会生成一个coverage文件夹,里面含有coverage.html文件,用浏览器打开即可查看覆盖率 :) 下面是我的build.xml文件。 <!-- override "compile" target in platform android_rules.xml to include tested app's external libraries --><target name="compile" depends="-resource-src, -aidl"    description="Compiles project's .java files into .class files"><!-- If android rules are used for a test project, its classpath should include tested project's location --><condition property="extensible.classpath"               value="${tested.project.dir}/bin/classes" else="."><isset property="tested.project.dir" /></condition><javac encoding="ascii" target="1.5" debug="true" extdirs=""    destdir="${out.classes.absolute.dir}"    bootclasspathref="android.target.classpath"    verbose="${verbose}" classpath="${extensible.classpath}">    <src path="${source.absolute.dir}" />    <src path="${gen.absolute.dir}" />    <classpath>      <fileset dir="${tested.project.dir}/libs" includes="*.jar" />      <fileset dir="${tested.project.dir}/bin/classes" includes="*.class" />      <fileset dir="${external.libs.absolute.dir}/libs" includes="*.jar" />    </classpath></javac></target>   可以copy上面的配置到你的build.xml文件中。另外建议,编辑测试工程、目标工程根目录下的build.properties这个文件(如果没有就新建),在里面添加emma.enabled=true。
英文原文 http://code.google.com/p/robotium/wiki/QuestionsAndAnswers

lsekfe 发表于 2014-12-30 14:51:11

亚洲 分享的内容还真不少呢~~~:lol
页: [1]
查看完整版本: 【我分享】Robotium问答