Issue
I have an app which contain a login page. In this login page i am storing user credentials in shared preference if user already login it will directly goes to splash screen then dashboard. But in case of android Instrumentation testing. My test case runs for only once since for the first time user credential is not stored in shared preference. once credentials has been stored in shared preference it will automatically redirect the test case to splash screen on start of the test. hence my test case is failed.
I want to clear my app sharedpreference everytime before i run login test case.
I tried to clear my sharedpreference from taking context using getInstrumentation().getContext(). but it seem its not working.
I have to write multiple test cases where i will have to run atleast 15 test cases in this same class. So for every @test i need to clear the shared preference first all the time.
below is code for my login test case:
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 = "[email protected]";
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();
}
}
Solution
You can clear all by
Context context = getInstrumentation().getContext();
SharedPreferences preferences = context.getSharedPreferences("mysharedpref",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
Answered By - Manoj Perumarath
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.