pedrovgs/Shot

``compareScreenshot`` does not accept ``ActivityScenario`` but ``Activity`` instead

Aperta

#242 aperta il 11 lug 2021

 (3 commenti) (0 reazioni) (0 assegnatari)Kotlin (111 fork)github user discovery
enhancementhelp wanted

Metriche repository

Star
 (1208 stelle)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

Using the example test theActivityIsShownProperly shown in the README with ActivityScenario does not work as intended

Expected behaviour

I expected using the default test in the README to work as intended when using ActivityScenario to launch the activity without any additional steps when using

class MyActivtyTest: ScreenshotTest {
    @Test
    fun rendersTheDefaultActivityState() {
        val activity = ActivityScenario.launch(MainActivity::class.java)

        compareScreenshot(activity)
    }
}

Actual behaviour

However I see the following error messages when inheriting ScreenshotTest

Screenshot 2021-07-11 at 15 44 23

When I do the following setup, it works but I am not sure is this necessary or should I just need to rely on ScreenshotTest for it to work. Any help on the matter would be greatly appreciated.

@RunWith(AndroidJUnit4::class)
class MyActivityTest  : com.example.shot_snapshot_application.ScreenshotTest<MainActivity>(MainActivity::class.java) {

    @Test
    fun theAcitivityIsShownProperly() {
        val scenario = startActivity()

        compareScreenshot(scenario)
    }
}

and

@LargeTest
@RunWith(AndroidJUnit4::class)
abstract class ScreenshotTest<T : Activity>(private val clazz: Class<T>) :
    ScreenshotTest {
    private var scenario: ActivityScenario<T>? = null

    @Before
    fun setup() {
        Intents.init()
    }

    @After
    fun tearDown() {
        scenario?.close()
        Intents.release()
    }

    fun startActivity(args: Bundle = Bundle()): T {
        val intent = Intent(ApplicationProvider.getApplicationContext(), clazz)
        intent.putExtras(args)
        scenario = ActivityScenario.launch(intent)
        return scenario!!.waitForActivity()
    }
}

Steps to reproduce

  1. Setup a new project
  2. Follow the setup steps as mentioned the readme
  3. For the sample test theActivityIsShownProperly use val activity = ActivityScenario.launch(MainActivity::class.java) instead of val mainActivity = startMainActivity();

Version of the library

5.11.0

Guida contributor