pedrovgs/Shot

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

Open

#242 opened on Jul 11, 2021

 (3 comments) (0 reactions) (0 assignees)Kotlin (111 forks)github user discovery
enhancementhelp wanted

Repository metrics

Stars
 (1,208 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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

Contributor guide