15
15
*/
16
16
17
17
package com.android.samples.webviewdemo
18
-
19
- import androidx.test.platform.app.InstrumentationRegistry
18
+ import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches
19
+ import androidx.test.espresso.web.sugar.Web.onWebView
20
+ import androidx.test.espresso.web.webdriver.DriverAtoms.findElement
21
+ import androidx.test.espresso.web.webdriver.DriverAtoms.getText
22
+ import androidx.test.espresso.web.webdriver.DriverAtoms.webClick
23
+ import androidx.test.espresso.web.webdriver.Locator
20
24
import androidx.test.ext.junit.runners.AndroidJUnit4
21
-
25
+ import androidx.test.rule.ActivityTestRule
26
+ import org.hamcrest.CoreMatchers.containsString
27
+ import org.junit.Rule
22
28
import org.junit.Test
23
29
import org.junit.runner.RunWith
24
30
25
- import org.junit.Assert.*
26
31
27
32
/* *
28
33
* Instrumented test, which will execute on an Android device.
@@ -31,10 +36,53 @@ import org.junit.Assert.*
31
36
*/
32
37
@RunWith(AndroidJUnit4 ::class )
33
38
class ExampleInstrumentedTest {
39
+
40
+ @Rule @JvmField
41
+ val activityRule = ActivityTestRule (MainActivity ::class .java)
42
+
43
+ fun afterActivityLaunched () {
44
+ // Technically we do not need to do this - MainActivity has javascript turned on.
45
+ // Other WebViews in your app may have javascript turned off, however since the only way
46
+ // to automate WebViews is through javascript, it must be enabled.
47
+ onWebView().forceJavascriptEnabled()
48
+ }
49
+
50
+
51
+ @Test
52
+ fun webViewTest () {
53
+
54
+ activityRule.getActivity()
55
+
56
+
57
+ onWebView()
58
+ .withElement(findElement(Locator .ID , " title" ))
59
+ .check(webMatches(getText(), containsString(" New York" )))
60
+ // .perform(webClick())
61
+ // .withElement(findElement(Locator.TAG_NAME, "h1"))
62
+ // .check(webMatches(getText(), containsString("Apple")))
63
+
64
+
65
+ }
66
+
67
+
68
+ // Test for calling postMessage
34
69
@Test
35
- fun useAppContext () {
36
- // Context of the app under test.
37
- val appContext = InstrumentationRegistry .getInstrumentation().targetContext
38
- assertEquals(" com.android.samples.webviewdemo" , appContext.packageName)
70
+ fun callPostMessage () {
71
+ onWebView()
72
+ // Click on the share button
73
+ .withElement(findElement(Locator .ID , " share" )) // similar to onView(withId(...))
74
+ .perform(webClick()) // Similar to perform(click())
75
+
76
+ // check that an intent was created
77
+
78
+ // verify that the data send to post message looks correct
79
+
80
+ // .get()
81
+ // .value
82
+
83
+
84
+ // Similar to check(matches(...))
85
+ // .check(webMatches(executeScript)
86
+
39
87
}
40
88
}
0 commit comments