Skip to content

Commit

Permalink
adds testing outline and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
grcoleman committed Jul 29, 2020
1 parent 00b047b commit 048851d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
7 changes: 7 additions & 0 deletions WebView/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ dependencies {
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test:runner:1.1.0'


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
*/

package com.android.samples.webviewdemo

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches
import androidx.test.espresso.web.sugar.Web.onWebView
import androidx.test.espresso.web.webdriver.DriverAtoms.findElement
import androidx.test.espresso.web.webdriver.DriverAtoms.getText
import androidx.test.espresso.web.webdriver.DriverAtoms.webClick
import androidx.test.espresso.web.webdriver.Locator
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.rule.ActivityTestRule
import org.hamcrest.CoreMatchers.containsString
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
Expand All @@ -31,10 +36,53 @@ import org.junit.Assert.*
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

@Rule @JvmField
val activityRule = ActivityTestRule(MainActivity::class.java)

fun afterActivityLaunched() {
// Technically we do not need to do this - MainActivity has javascript turned on.
// Other WebViews in your app may have javascript turned off, however since the only way
// to automate WebViews is through javascript, it must be enabled.
onWebView().forceJavascriptEnabled()
}


@Test
fun webViewTest() {

activityRule.getActivity()


onWebView()
.withElement(findElement(Locator.ID, "title"))
.check(webMatches(getText(), containsString("New York")))
// .perform(webClick())
// .withElement(findElement(Locator.TAG_NAME, "h1"))
// .check(webMatches(getText(), containsString("Apple")))


}


// Test for calling postMessage
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.android.samples.webviewdemo", appContext.packageName)
fun callPostMessage() {
onWebView()
// Click on the share button
.withElement(findElement(Locator.ID, "share")) // similar to onView(withId(...))
.perform(webClick()) // Similar to perform(click())

// check that an intent was created

// verify that the data send to post message looks correct

//.get()
//.value


// Similar to check(matches(...))
//.check(webMatches(executeScript)

}
}
2 changes: 1 addition & 1 deletion WebView/app/src/main/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ <h1 id="title">Location</h1>
<h2 class="currentTemp" id="currentTemp">Current Temp</h2>
<h2 class="shortDescription" id="shortDescription">Short Description</h2>
<p class="longDescription" id="longDescription">Long Description</p>
<button class="button" onclick="sendAndroidMessage()" type="button" value="Send Message">Share</button>
<button id="share" class="button" onclick="sendAndroidMessage()" type="button" value="Send Message">Share</button>
</body>
</html>

0 comments on commit 048851d

Please sign in to comment.