-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathEspressoTest.java
More file actions
50 lines (41 loc) · 1.67 KB
/
EspressoTest.java
File metadata and controls
50 lines (41 loc) · 1.67 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
package com.example.testing.testingexample;
import android.support.test.InstrumentationRegistry;
import android.test.ActivityInstrumentationTestCase2;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
/**
* 不推荐
*/
public class EspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {
private static final String STRING_TO_BE_TYPED = "peter";
private MainActivity activity;
public EspressoTest() {
super(MainActivity.class);
}
@Override public void setUp() throws Exception {
super.setUp();
/**
* 必须调用这两行代码
*/
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
activity = getActivity();
}
/**
* 方法名必须以test开头.尽量不要继承,请使用JUnit 4 style,
* 这样写会多写代码,并且会出错.
* 比如此例,输入太慢了,程序就执行了.
*/
@Test
public void testSayHello() {
onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED),
closeSoftKeyboard()); //line 1
onView(withText("Say hello!")).perform(click()); //line 2
String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!";
onView(withId(R.id.textView)).check(matches(withText(expectedText))); //line 3
}
}