安卓 :如何在仪器测试中运行大量测试之前确保共享首选项得到清除



我有一个包含登录页面的应用程序。在此登录页面中,如果用户已经登录,我将用户凭据存储在共享首选项中,它将直接进入启动画面,然后进入仪表板。但在安卓仪器测试的情况下。我的测试用例只运行一次,因为第一次用户凭据未存储在共享首选项中。将凭据存储在共享首选项中后,它将自动在测试开始时将测试用例重定向到初始屏幕。 因此我的测试用例失败了。

我想在每次运行登录测试用例之前清除我的应用程序共享首选项。

我试图清除我的共享偏好,避免使用 getInstrumentation().getContext() 获取上下文。 但它似乎不起作用。

我必须编写多个测试用例,其中我必须在同一类中运行至少 15 个测试用例。因此,对于每个@test,我都需要始终首先清除共享首选项。

以下是我的登录测试用例的代码:

import android.app.Instrumentation;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Button;
import android.widget.EditText;
import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import salesken.app.R;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.Assert.assertNotNull;
@RunWith(AndroidJUnit4.class)
public class LoginActivityTest {
@Rule
public ActivityTestRule < LoginActivity > loginActivityTestRule = new ActivityTestRule < LoginActivity > (LoginActivity.class);
private LoginActivity loginActivity = null;
private String email_input = "dummy@user.com";
private String password_input = "password1";
private int timeout = 5000;
private SharedPreferences sharedPreferences;
Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(SplashScreenActivity.class.getName(), null, false);
@Before
public void setUp() throws Exception {
loginActivity = loginActivityTestRule.getActivity();
clearsharedPref();
}
@After
public void tearDown() throws Exception {
loginActivity = null;
}
@Test
public void autenticationcheck() {
clearsharedPref();
EditText email = loginActivity.findViewById(R.id.email);
EditText password = loginActivity.findViewById(R.id.password);
Button login = loginActivity.findViewById(R.id.login);
email.setText(email_input);
password.setText(password_input);
onView(withId(R.id.login)).perform(ViewActions.click());
SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
assertNotNull(splashScreenActivity);
splashScreenActivity.finish();
}
@Test
public void emptyEmail() {
clearsharedPref();
EditText email = loginActivity.findViewById(R.id.email);
EditText password = loginActivity.findViewById(R.id.password);
Button login = loginActivity.findViewById(R.id.login);
email.setText("");
password.setText(password_input);
onView(withId(R.id.login)).perform(ViewActions.click());
SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
assertNull(splashScreenActivity);
}
@Test
public void emptyPassword() {
clearsharedPref();
EditText email = loginActivity.findViewById(R.id.email);
EditText password = loginActivity.findViewById(R.id.password);
Button login = loginActivity.findViewById(R.id.login);
email.setText("");
password.setText(password_input);
onView(withId(R.id.login)).perform(ViewActions.click());
SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
assertNull(splashScreenActivity);
}
public void clearsharedPref() {
Context context = getInstrumentation().getTargetContext().getApplicationContext();
sharedPreferences = context.getSharedPreferences("SaleskenProComplexKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
}
}

你可以通过以下方式清除所有内容

Context context = getInstrumentation().getContext();
SharedPreferences preferences = context.getSharedPreferences("mysharedpref", 
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear(); 
editor.commit();

清除SharedPrferences应该足够了,如果没有,您可以删除它而不是清除:

String filePath = getApplicationContext().getFilesDir().getParent()+"/shared_prefs/prefs_name.xml";
File deletePrefFile = new File(filePath);
deletePrefFile.delete();

如果我无法使用简单的方法获取应用程序的上下文:

public class ApplicationContext extends Application {
private static ApplicationContext INSTANCE;
@Override
public void onCreate() {
super.onCreate();
INSTANCE = this;
}
public static ApplicationContext getInstance() {
return INSTANCE;
}
}

相关内容

  • 没有找到相关文章

最新更新