11package com.example.android.testing.espresso.screenshotsample
22
3+ import android.graphics.Bitmap
34import androidx.test.core.app.takeScreenshot
45import androidx.test.core.graphics.writeToTestStorage
56import androidx.test.espresso.Espresso.onView
7+ import androidx.test.espresso.action.ViewActions
8+ import androidx.test.espresso.action.ViewActions.captureToBitmap
69import androidx.test.espresso.matcher.ViewMatchers.isRoot
710import androidx.test.espresso.matcher.ViewMatchers.withText
8- import androidx.test.espresso.screenshot.captureToBitmap
11+
912import androidx.test.ext.junit.rules.activityScenarioRule
1013import androidx.test.ext.junit.runners.AndroidJUnit4
1114import org.junit.Rule
@@ -23,43 +26,41 @@ import java.io.IOException
2326@RunWith(AndroidJUnit4 ::class )
2427class ScreenshotTest {
2528
26- // a handy JUnit rule that stores the method name, so it can be used to generate unique
27- // screenshot files per test method
28- @get:Rule
29- var nameRule = TestName ()
29+ // a handy JUnit rule that stores the method name, so it can be used to generate unique
30+ // screenshot files per test method
31+ @get:Rule
32+ var nameRule = TestName ()
3033
31- @get:Rule
32- val activityScenarioRule = activityScenarioRule<MainActivity >()
34+ @get:Rule
35+ val activityScenarioRule = activityScenarioRule<MainActivity >()
3336
34- /* *
35- * Captures and saves an image of the entire [MainActivity] contents.
36- */
37- @Test
38- @Throws(IOException ::class )
39- fun saveActivityBitmap () {
40- onView(isRoot())
41- .captureToBitmap()
42- .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} " )
43- }
37+ /* *
38+ * Captures and saves an image of the entire [MainActivity] contents.
39+ */
40+ @Test
41+ @Throws(IOException ::class )
42+ fun saveActivityBitmap () {
43+ onView(isRoot())
44+ .perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} " ) }))
45+ }
4446
45- /* *
46- * Captures and saves an image of the 'Hello world' view.
47- */
48- @Test
49- @Throws(IOException ::class )
50- fun saveViewBitmap () {
51- onView(withText(" Hello World!" ))
52- .captureToBitmap()
53- .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} " )
54- }
47+ /* *
48+ * Captures and saves an image of the 'Hello world' view.
49+ */
50+ @Test
51+ @Throws(IOException ::class )
52+ fun saveViewBitmap () {
53+ onView(withText(" Hello World!" ))
54+ .perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} " ) }))
55+ }
5556
56- /* *
57- * Captures and saves an image of the entire device screen to storage.
58- */
59- @Test
60- @Throws(IOException ::class )
61- fun saveDeviceScreenBitmap () {
62- takeScreenshot()
63- .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} " )
64- }
57+ /* *
58+ * Captures and saves an image of the entire device screen to storage.
59+ */
60+ @Test
61+ @Throws(IOException ::class )
62+ fun saveDeviceScreenBitmap () {
63+ takeScreenshot()
64+ .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} " )
65+ }
6566}
0 commit comments