Robotium问答

如何开始?

下载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 use solo.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

版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=570