我正在为android应用程序开发自动化测试(使用Robotium)。为了确保测试的一致性和可靠性,我想以干净状态(正在测试的应用程序)开始每个测试。为此,我需要清除应用数据。可以在“设置/应用程序/管理应用程序/ [我的应用程序] /清除数据”中手动完成此操作
以编程方式完成此操作的推荐方法是什么?
我正在为android应用程序开发自动化测试(使用Robotium)。为了确保测试的一致性和可靠性,我想以干净状态(正在测试的应用程序)开始每个测试。为此,我需要清除应用数据。可以在“设置/应用程序/管理应用程序/ [我的应用程序] /清除数据”中手动完成此操作
以编程方式完成此操作的推荐方法是什么?
Answers:
您可以使用包管理器工具清除已安装应用程序的数据(类似于按设备上应用程序设置中的“清除数据”按钮)。因此,使用adb可以做到:
adb shell pm clear my.wonderful.app.package
Error: unknown command 'clear'。
按照@edovino的回答,以编程方式清除应用程序所有首选项的方法是
private void clearPreferences() {
try {
// clearing app data
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear YOUR_APP_PACKAGE_GOES HERE");
} catch (Exception e) {
e.printStackTrace();
}
}
警告:应用程序将强制关闭。
从API版本19开始,可以调用ActivityManager.clearApplicationUserData()。
((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).clearApplicationUserData();
检查此代码以:
@Override
protected void onDestroy() {
// closing Entire Application
android.os.Process.killProcess(android.os.Process.myPid());
Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
trimCache(this);
super.onDestroy();
}
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// <uses-permission
// android:name="android.permission.CLEAR_APP_CACHE"></uses-permission>
// The directory is now empty so delete it
return dir.delete();
}
如果您只有几个共享的首选项需要清除,那么此解决方案会更好。
@Override
protected void setUp() throws Exception {
super.setUp();
Instrumentation instrumentation = getInstrumentation();
SharedPreferences preferences = instrumentation.getTargetContext().getSharedPreferences(...), Context.MODE_PRIVATE);
preferences.edit().clear().commit();
solo = new Solo(instrumentation, getActivity());
}
使用上下文,我们可以清除应用程序特定的文件,如首选项,数据库文件。我已使用以下代码使用Espresso进行UI测试。
@Rule
public ActivityTestRule<HomeActivity> mActivityRule = new ActivityTestRule<>(
HomeActivity.class);
public static void clearAppInfo() {
Activity mActivity = testRule.getActivity();
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(mActivity);
prefs.edit().clear().commit();
mActivity.deleteDatabase("app_db_name.db");
}
最简单的方法是
private void deleteAppData() {
try {
// clearing app data
String packageName = getApplicationContext().getPackageName();
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear "+packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
这将清除数据并从内存中删除您的应用程序。这等效于“设置”->“应用程序管理器”->您的应用程序->“清除数据”下的“清除数据”选项。
这将完全删除数据以及强制关闭应用程序
这个解决方案确实对我有帮助:
通过使用以下两种方法,我们可以以编程方式清除数据
public void clearApplicationData() {
File cacheDirectory = getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
if (applicationDirectory.exists()) {
String[] fileNames = applicationDirectory.list();
for (String fileName : fileNames) {
if (!fileName.equals("lib")) {
deleteFile(new File(applicationDirectory, fileName));
}
}
}
}
public static boolean deleteFile(File file) {
boolean deletedAll = true;
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
}
} else {
deletedAll = file.delete();
}
}
return deletedAll;
}
如果android版本高于kitkat,则也可以使用
public void onClick(View view){
Context context = getApplicationContext(); // add this line
if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
((ActivityManager) context.getSystemService(ACTIVITY_SERVICE))
.clearApplicationUserData();
return;
}
以编程方式完成此操作的推荐方法是什么?
唯一可能的选择是adb shell pm clear package在测试之前运行ADB命令。最大的问题是,将测试执行和shell命令结合起来有点令人头疼。
但是,我们(Mediafe)提供了一些解决方案,可以在常规无根设备上为您工作。您需要做的就是添加注释。其余所有操作都是通过运行简单的bash脚本完成的。
只需@ClearData在任何测试和tada before之前添加注释,ADB clear命令将在测试执行之前执行。
这是此类测试的示例:
@Test
@ClearData
public void someTest() {
// your test
}
想法如下
adb shell am instrument -e log true使用相同的想法,这些都是您可以轻松支持的所有选项:
仅使用注释。像这样:
@Test
@ClearData
@Tags(tags = {"sanity", "medium"})
@Parameterized.Repeat(count = 3)
public void myTest() throws Exception {
String param = params[index];
// ...
}
奖金!🎁对于每个失败的测试:
通常,添加更多选项很容易,因为测试是从bash脚本而不是gradle任务一个接一个地执行的。
blog 完整的博客文章:https : //medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8
with 带有示例的源代码:https : //github.com/medisafe/run-android-tests
希望这能回答6年的问题;)