编写集成测试的人肯定听说过 Spoon 和 Spoon-Gradle-Plugin,它们为您的测试提供有用的功能。我正在使用stanfy/spoon-gradle-plugin。
我com.squareup.spoon.Spoon.java
在测试中使用类提供的屏幕截图方法。在我通过"./gradlew spoon"
命令运行测试后,它会为我生成包含每个测试的日志和屏幕截图的 xml 文件。这适用于系统版本低于 6.0 的每个 android 设备。当我使用 Marshmallow 在设备上运行测试时,我的控制台在测试结束时看起来像这样:
2015-12-18 15:58:00 [STRL.testRunEnded] elapsedTime=408847
03:58:00 I/XmlResultReporter: XML test result file generated at /Users/F1sherKK/Dev/myapp-Android/app/build/spoon-log/normal/debugRelease/junit-reports/05f3785c3444f1bf.xml. Total tests 32, failure 1, passed 31,
2015-12-18 15:58:00 [SDR.run] About to grab screenshots and prepare output for [05f3785c3444f1bf]
2015-12-18 15:58:00 [SDR.pullDirectory] Internal path is /data/data/com.myapp.sendmoney.debug1/app_spoon-screenshots
2015-12-18 15:58:00 [SDR.pullDirectory] External path is /sdcard/app_spoon-screenshots
2015-12-18 15:58:00 [SDR.pullDirectory] Pulling files from external dir on [05f3785c3444f1bf]
2015-12-18 15:58:05 [SDR.pullDirectory] Pulling files from internal dir on [05f3785c3444f1bf]
2015-12-18 15:58:06 [SDR.pullDirectory] Done pulling app_spoon-screenshots from on [05f3785c3444f1bf]
2015-12-18 15:58:06 [SDR.pullDirectory] Internal path is /data/data/com.myapp.sendmoney.debug1/app_spoon-files
2015-12-18 15:58:06 [SDR.pullDirectory] External path is /sdcard/app_spoon-files
2015-12-18 15:58:06 [SDR.pullDirectory] Pulling files from external dir on [05f3785c3444f1bf]
2015-12-18 15:58:06 [SDR.pullDirectory] Pulling files from internal dir on [05f3785c3444f1bf]
2015-12-18 15:58:06 [SDR.pullDirectory] Done pulling app_spoon-files from on [05f3785c3444f1bf]
2015-12-18 15:58:06 [SDR.handleImages] Moving screenshots to the image folder on [05f3785c3444f1bf]
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.flowtests.NotificationCenterActivity.NotificationCenterActivityFunctionTest#assertReferralPopUpWillAppear
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.flowtests.NotificationCenterActivity.NotificationCenterActivityFunctionTest#assertReferralPopUpWillHide
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#addMockItem_newMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#clickOnBackButton_returnToNotificationCenter
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#clickOnBackButton_returnToNotificationCenter
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#clickOnBackButton_returnToNotificationCenter
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#clickOnBackButton_returnToNotificationCenter
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.CustomerServiceActivity.CustomerServiceNavigator#clickOnItem_referralType
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.General.GeneralNavigator#sendNotification_addSampleMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.General.GeneralNavigator#sendNotification_addSampleMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.General.GeneralNavigator#sendNotification_addSampleMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.General.GeneralNavigator#sendNotification_addSampleMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.General.GeneralNavigator#sendNotification_addSampleMessage
2015-12-18 15:58:06 [SDR.handleImages] Unable to find test for com.myapp.sendmoney.instrumentation.navigation.activity.InviteFriendsActivity.InviteFriendsNavigator#clickOnBackButton_returnToCustomerService
因此,当我看到这一点时,我首先想到的是......自 android 6.0 以来更改的权限处理问题。所以我想如果我提供
WRITE_EXTERNAL_STORAGE
和
READ_EXTERNAL_STORAGE
我的应用程序的权限应该可以工作。所以我创建了这个简单的 shell 脚本来检测 SDK 版本,如果它等于或高于 23,那么在测试开始之前授予我的包的权限。
SDK=`adb shell getprop ro.build.version.sdk | tr -d '\r'`
if (( "$SDK" >= 23 )) ; then
adb shell pm grant com.myapp.sendmoney.debug1 android.permission.WRITE_EXTERNAL_STORAGE
adb shell pm grant com.myapp.sendmoney.debug1 android.permission.READ_EXTERNAL_STORAGE
fi
./gradlew spoon
似乎我的权限已获得但在测试开始后我仍然无法从设备中提取屏幕截图。
我的第二个想法是。好的,如果这不是权限问题,那么也许没有要提取的屏幕截图。因此,Spoon 会在测试完成后从您的设备中清除屏幕截图。我开始测试,等到其中一些完成,然后在我的控制台 ctrl+C 中终止进程。我打开了 Android 设备监视器,我可以看到我的测试截图在那里等待拉取。仍然没有发生。
所以我的第三个想法是。好吧,如果那没有权限并且屏幕截图功能在 6.0 设备上可以正常工作,那么可能是勺子问题。我试图在网上寻找类似的主题,但没有一个与我的案例直接相关。
我发现了这个问题,但它并没有解决我的问题。我不是UIAutomator
用来截屏的,而是 Spoon 的截屏方法。
我想我现在正在解决一个问题,但我做了建议的事情,但没有帮助。
4天前有一些修复,但我现在仍然面临问题,我的库是最近的:
dependencies {
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.squareup.spoon:spoon-client:1.2.1'
androidTestCompile 'com.jraska:falcon-spoon-compat:0.3.1'
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support:recyclerview-v7:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile "org.mockito:mockito-core:1.10.19"
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
}
和
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
(...)
classpath 'com.squareup.spoon:spoon-runner:1.2.1'
classpath ('com.stanfy.spoon:spoon-gradle-plugin:1.0.4-SNAPSHOT') {
exclude module: "guava"
}
classpath 'com.google.guava:guava:18.0'
}
所以......也许我错过了一些东西。也许我忘记了需要另一个许可。我很感激任何建议。