1
1
package com.example.android.testing.espresso.screenshotsample
2
2
3
+ import android.graphics.Bitmap
3
4
import androidx.test.core.app.takeScreenshot
4
5
import androidx.test.core.graphics.writeToTestStorage
5
6
import androidx.test.espresso.Espresso.onView
7
+ import androidx.test.espresso.action.ViewActions
8
+ import androidx.test.espresso.action.ViewActions.captureToBitmap
6
9
import androidx.test.espresso.matcher.ViewMatchers.isRoot
7
10
import androidx.test.espresso.matcher.ViewMatchers.withText
8
- import androidx.test.espresso.screenshot.captureToBitmap
11
+
9
12
import androidx.test.ext.junit.rules.activityScenarioRule
10
13
import androidx.test.ext.junit.runners.AndroidJUnit4
11
14
import org.junit.Rule
@@ -23,43 +26,41 @@ import java.io.IOException
23
26
@RunWith(AndroidJUnit4 ::class )
24
27
class ScreenshotTest {
25
28
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 ()
30
33
31
- @get:Rule
32
- val activityScenarioRule = activityScenarioRule<MainActivity >()
34
+ @get:Rule
35
+ val activityScenarioRule = activityScenarioRule<MainActivity >()
33
36
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
+ }
44
46
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
+ }
55
56
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
+ }
65
66
}
0 commit comments