/
BaseTest.java
104 lines (92 loc) · 3.99 KB
/
BaseTest.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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 io.appium.uiautomator2.unittest.test.internal;
import android.content.Context;
import org.json.JSONException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import java.io.IOException;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.Configurator;
import io.appium.uiautomator2.model.By;
import io.appium.uiautomator2.server.ServerInstrumentation;
import io.appium.uiautomator2.unittest.test.Config;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static io.appium.uiautomator2.unittest.test.internal.TestUtils.waitForElement;
import static io.appium.uiautomator2.unittest.test.internal.TestUtils.waitForElementInvisibility;
import static io.appium.uiautomator2.unittest.test.internal.commands.DeviceCommands.createSession;
import static io.appium.uiautomator2.unittest.test.internal.commands.DeviceCommands.deleteSession;
import static io.appium.uiautomator2.unittest.test.internal.commands.ElementCommands.click;
import static io.appium.uiautomator2.utils.Device.getUiDevice;
import static org.junit.Assert.assertNotNull;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(AndroidJUnit4.class)
public abstract class BaseTest {
protected static ServerInstrumentation serverInstrumentation;
private static Context ctx;
@Rule
public TestWatcher watcher = new TestWatcher();
/**
* start io.appium.uiautomator2.server and launch the application main activity
*/
@BeforeClass
public static void startServer() throws JSONException, IOException {
if (serverInstrumentation != null) {
return;
}
assertNotNull(getUiDevice());
ctx = InstrumentationRegistry.getInstrumentation().getContext();
serverInstrumentation = ServerInstrumentation.getInstance();
Logger.info("Starting Server");
serverInstrumentation.startServer();
Client.waitForNettyStatus(NettyStatus.ONLINE);
createSession();
Configurator.getInstance().setWaitForSelectorTimeout(0);
Configurator.getInstance().setWaitForIdleTimeout(50000);
TestUtils.grantPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
TestUtils.grantPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
}
@AfterClass
public static void stopSever() {
deleteSession();
if (serverInstrumentation == null) {
return;
}
serverInstrumentation.stopServer();
Client.waitForNettyStatus(NettyStatus.OFFLINE);
serverInstrumentation = null;
}
@Before
public void launchAUT() throws JSONException {
startActivity(Config.APP_NAME);
waitForElement(By.accessibilityId("Accessibility"));
}
protected void startActivity(String activity) throws JSONException {
TestUtils.startActivity(ctx, activity);
}
protected void clickAndWaitForStaleness(String elementId) throws JSONException {
click(elementId);
waitForElementInvisibility(elementId);
}
}