我正在使用Robotium为Android应用程序开发自动化测试。为确保测试的一致性和可靠性,我希望每次测试都以被测试应用程序的清空状态开始。为此,我需要清除应用程序数据。可以在“设置/应用程序/管理应用程序/[我的应用程序]/清除数据”中手动完成此操作。
有没有推荐的以编程方式实现此操作的方法?
我正在使用Robotium为Android应用程序开发自动化测试。为确保测试的一致性和可靠性,我希望每次测试都以被测试应用程序的清空状态开始。为此,我需要清除应用程序数据。可以在“设置/应用程序/管理应用程序/[我的应用程序]/清除数据”中手动完成此操作。
有没有推荐的以编程方式实现此操作的方法?
你可以使用软件包管理工具清除已安装应用的数据(类似于在设备上的应用设置中按下“清除数据”按钮)。 因此,使用adb可以执行以下操作:
adb shell pm clear my.wonderful.app.package
回应 @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();
}
}
警告:应用将会强制关闭。
你可以使用以下代码清除SharedPreferences应用数据
Editor editor =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
要清除应用程序数据库,此答案是正确的 -> 清除应用程序数据库
从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());
}
@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");
}
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
。最大的问题是将测试执行和shell命令结合起来有点麻烦。@ClearData
注释,然后tada,在测试执行之前将执行ADB清除命令。@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 任务逐个执行的。
完整博客文章:https://medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8
带有示例的源代码:https://github.com/medisafe/run-android-tests
希望这回答了六年的问题;)
这种由Sebastiano添加的方式是可以的,但是当您从IntelliJ IDE运行测试时,需要添加:
try {
// clearing app data
Runtime runtime = Runtime.getRuntime();
runtime.exec("adb shell pm clear YOUR_APP_PACKAGE_GOES HERE");
}
不要只有“pm package…”
更重要的是:在driver.setCapability(App_package, package_name)之前添加它。