繁体   English   中英

Android:如何在Instrumentation测试中进行大量测试之前确保sharedPreference变得清晰

[英]Android : How to make sure that sharedPreference get clear before running a large nos of test in Instrumentation testing

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

我想在运行登录测试用例之前每次清除应用程序的sharedpreference。

我试图从使用getInstrumentation()。getContext()获取上下文中清除我的sharedpreference。 但似乎不起作用。

我必须编写多个测试用例,其中我必须在同一类中至少运行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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM