-
Notifications
You must be signed in to change notification settings - Fork 206
/
BaseADFTest.java
182 lines (161 loc) · 6.12 KB
/
BaseADFTest.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.devicefarm.android.referenceapp;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.UiThreadTest;
import android.support.test.espresso.contrib.DrawerActions;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.test.ActivityInstrumentationTestCase2;
import com.amazonaws.devicefarm.android.referenceapp.Activities.MainActivity;
import com.amazonaws.devicefarm.android.referenceapp.Util.ScreenShot;
import org.junit.Ignore;
import org.junit.Test;
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.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.not;
/**
* The base Espresso class for all the Espresso tests
*/
@Ignore
public abstract class BaseADFTest extends ActivityInstrumentationTestCase2<MainActivity> {
public BaseADFTest() {
super(MainActivity.class);
}
protected abstract String getClassName();
@UiThreadTest
@Test
public abstract void testSanity();
/**
* Gets the activity and navigates to the Class's category in the navigation drawer
*
* Uses espresso-contrib dor navigation drawer and recyclerView support
*
* @throws Exception instrumentation ActivityInstrumentationTestCase2 exception
*/
@Override
protected void setUp() throws Exception {
getActivity(); //IMPORTANT you must call this before your tests!
super.setUp();
DrawerActions.openDrawer(R.id.drawer_layout);
RecyclerViewActions.scrollTo(withText(getClassName()));
onView(withId(R.id.drawerList)).perform(RecyclerViewActions.actionOnItem(hasDescendant(withText(getClassName())), click()));
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
}
/**
* Takes a screenshot after every test
*
* @throws Exception
*/
@Override
protected void tearDown() throws Exception {
try {
takeScreenShot();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
super.tearDown();
}
/**
* Inputs text into a specific id
*
* @param id the input id
* @param text the text to input
*/
protected void writeTextIntoTextBox(int id, String text){
onView(withId(id)).perform(typeText(text));
}
/**
* clicks an element id
*
* @param id the element id
*/
protected void clickId(int id){
onView(withId(id)).perform(click());
}
/**
* Checks if id is displayed with expected text
*
* @param id the element id
* @param textResource the string resource id of the expected text
*/
protected void checkIfIdIsDisplayedWithText(int id, int textResource){
onView(withId(id)).check(matches(withText(textResource))).check(matches(isDisplayed()));
}
/**
* Checks if id is displayed with expected text
*
* @param id the element id
* @param text the string of the expected text
*/
protected void checkIfIdIsDisplayedWithText(int id, String text){
onView(withId(id)).check(matches(withText(text))).check(matches(isDisplayed()));
}
/**
* Checks if a specific id is displayed
* @param id the element id
*/
protected void checkIfIdDisplayed(int id){
onView(withId(id)).check(matches(isDisplayed()));
}
/**
* A method to take a screenshot
*
* It is important to run the screenshot take on the UI thread.
* Not doing so can cause threading errors or unexpected results.
*
* @throws Throwable
*/
protected void takeScreenShot() throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
ScreenShot.take(getActivity());
}
});
}
/**
* Checks if a toast message is displayed
* @param messageId the string resource of the expected toast message
*/
protected void verifyToastMessage(int messageId){
onView(withText(messageId)).
inRoot(withDecorView(not(getActivity().getWindow().getDecorView())))
.check(matches(isDisplayed()));
}
/**
* Convenience method to match a specific id with a specific content description resource id
* @param id the element id
* @param contentDescriptionId the elements content description id
*/
protected void checkIdWithContentDescription(int id, int contentDescriptionId) {
onView(withId(id)).check(matches(withContentDescription(contentDescriptionId)));
}
/**
* Convenience method to match a specific id with a specific content description
* @param id the element id
* @param contentDescription the expected content description
*/
protected void checkIdWithContentDescription(int id, String contentDescription) {
onView(withId(id)).check(matches(withContentDescription(contentDescription)));
}
}