Skip to content

Commit 048851d

Browse files
committed
adds testing outline and dependencies
1 parent 00b047b commit 048851d

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

WebView/app/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,11 @@ dependencies {
6565
testImplementation 'junit:junit:4.13'
6666
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
6767
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
68+
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
69+
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
70+
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
71+
androidTestImplementation 'androidx.test:rules:1.1.1'
72+
androidTestImplementation 'androidx.test:runner:1.1.0'
73+
74+
6875
}

WebView/app/src/androidTest/java/com/android/samples/webviewdemo/ExampleInstrumentedTest.kt

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
*/
1616

1717
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
2024
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
2228
import org.junit.Test
2329
import org.junit.runner.RunWith
2430

25-
import org.junit.Assert.*
2631

2732
/**
2833
* Instrumented test, which will execute on an Android device.
@@ -31,10 +36,53 @@ import org.junit.Assert.*
3136
*/
3237
@RunWith(AndroidJUnit4::class)
3338
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
3469
@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+
3987
}
4088
}

WebView/app/src/main/assets/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ <h1 id="title">Location</h1>
3232
<h2 class="currentTemp" id="currentTemp">Current Temp</h2>
3333
<h2 class="shortDescription" id="shortDescription">Short Description</h2>
3434
<p class="longDescription" id="longDescription">Long Description</p>
35-
<button class="button" onclick="sendAndroidMessage()" type="button" value="Send Message">Share</button>
35+
<button id="share" class="button" onclick="sendAndroidMessage()" type="button" value="Send Message">Share</button>
3636
</body>
3737
</html>

0 commit comments

Comments
 (0)