Espresso Login Activity Test Cases

This commit is contained in:
developerraman
2021-01-28 17:46:04 +05:30
parent ce49e907a2
commit 719c0137fb
2 changed files with 168 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
package in.sminnovations.arrest_tb;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class LoginActivity {
@Rule
public ActivityTestRule<LoginScreen> mActivityTestRule = new ActivityTestRule<>(LoginScreen.class);
@Test
public void LoginSuccess_2() {
// Type text and then press the button.
onView(withId(R.id.contactnumber))
.perform(replaceText("9779057263"), closeSoftKeyboard());
onView(withId(R.id.password))
.perform(typeText("Deep"), closeSoftKeyboard());
onView(withId(R.id.showpassword)).perform(click());
onView(withId(R.id.login)).perform(click());
try {
Thread.sleep(160);
} catch (InterruptedException e) {
e.printStackTrace();
}
// onView(withText("logged in Successfully...")).inRoot(new ToastMatcher()).check(matches(isDisplayed()));
// Check that the text was changed.
}
@Test
public void LoginInvalidNumber_3() {
// Type text and then press the button.
onView(withId(R.id.contactnumber))
.perform(typeText("977905"), closeSoftKeyboard());
onView(withId(R.id.password))
.perform(typeText("Deep"), closeSoftKeyboard());
onView(withId(R.id.showpassword)).perform(click());
onView(withId(R.id.login)).perform(click());
try {
Thread.sleep(160);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(withText("No such user exists")).inRoot(new ToastMatcher()).check(matches(isDisplayed()));
// Check that the text was changed.
}
@Test
public void LoginWrongPassword_4() {
// Type text and then press the button.
onView(withId(R.id.contactnumber))
.perform(typeText("9779057263"), closeSoftKeyboard());
onView(withId(R.id.password))
.perform(typeText("Deep1"), closeSoftKeyboard());
onView(withId(R.id.showpassword)).perform(click());
onView(withId(R.id.login)).perform(click());
try {
Thread.sleep(160);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(withText("Wrong Password")).inRoot(new ToastMatcher()).check(matches(isDisplayed()));
// Check that the text was changed.
}
@Test
public void EmptyPassword_5() {
// Type text and then press the button.
onView(withId(R.id.contactnumber))
.perform(typeText("9779057263"), closeSoftKeyboard());
onView(withId(R.id.showpassword)).perform(click());
onView(withId(R.id.login)).perform(click());
try {
Thread.sleep(160);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(withText("Please enter all the required fields")).inRoot(new ToastMatcher()).check(matches(isDisplayed()));
// Check that the text was changed.
}
@Test
public void EmptyNumber_6() {
// Type text and then press the button.
onView(withId(R.id.password))
.perform(typeText("Deep1"), closeSoftKeyboard());
onView(withId(R.id.showpassword)).perform(click());
onView(withId(R.id.login)).perform(click());
try {
Thread.sleep(160);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(withText("Please enter all the required fields")).inRoot(new ToastMatcher()).check(matches(isDisplayed()));
// Check that the text was changed.
}
}

View File

@@ -0,0 +1,30 @@
package in.sminnovations.arrest_tb;
import android.os.IBinder;
import android.view.WindowManager;
import androidx.test.espresso.Root;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
public class ToastMatcher extends TypeSafeMatcher<Root> {
@Override
public void describeTo(Description description) {
description.appendText("is toast");
}
@Override
public boolean matchesSafely(Root root) {
int type = root.getWindowLayoutParams().get().type;
if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
IBinder windowToken = root.getDecorView().getWindowToken();
IBinder appToken = root.getDecorView().getApplicationWindowToken();
if (windowToken == appToken) {
return true;
}
}
return false;
}
}