-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLoginTest.java
More file actions
114 lines (86 loc) · 3.72 KB
/
LoginTest.java
File metadata and controls
114 lines (86 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.example.Lab3;
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.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.Intents.intending;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.intent.matcher.IntentMatchers.toPackage;
import static androidx.test.espresso.matcher.RootMatchers.withDecorView;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.IBinder;
import android.view.View;
import android.view.WindowManager;
import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.Root;
import androidx.test.espresso.action.TypeTextAction;
import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.hamcrest.Description;
@RunWith(AndroidJUnit4.class)
@SmallTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LoginTest {
private View decorView;
@Rule
public ActivityScenarioRule<LoginActivity> activityRule =
new ActivityScenarioRule<>(LoginActivity.class);
@Test
public void IsEmailFieldVisible() {
onView(withId(R.id.username)).check(matches(isDisplayed()));
}
@Test
public void IsPasswordFieldVisible() {
onView(withId(R.id.password)).check(matches(isDisplayed()));
}
@Before
public void intentInit(){
Intents.init();
}
@Test
public void TryRightLogin() throws InterruptedException {
onView(withId(R.id.username)).perform(new TypeTextAction("test@gmail.com"), closeSoftKeyboard());
onView(withId(R.id.password)).perform(new TypeTextAction("123456"), closeSoftKeyboard());
// onView(withId(R.id.password)).perform(closeSoftKeyboard());
Thread.sleep(250);
onView(withId(R.id.loginButton)).perform(click());
Intent i = new Intent();
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, i);
intending(toPackage(StartActivity.class.getName()));
}
@After
public void intentRelease(){
Intents.release();
}
@Test
public void TryWrongLogin() throws InterruptedException {
onView(withId(R.id.username)).perform(new TypeTextAction("test1@gmail.com"), closeSoftKeyboard());
onView(withId(R.id.password)).perform(new TypeTextAction("123456"), closeSoftKeyboard());
Thread.sleep(250);
onView(withId(R.id.loginButton)).perform(click());
onView(withText("Authentication failed."))
.inRoot(new ToastMatcher())// Here we use decorView
.check(matches(isDisplayed()));
// onView(withText("Authentication failed."))
// .inRoot(withDecorView(Matchers.is(decorView)))// Here we use decorView
// .check(matches(isDisplayed()));
// onView(withId(R.id.checklogin)).check(matches(withText("False")));
}
}