Issue with Kotlin lambdas halting code using Spoon plug-in by jaredsburrows

91 Views Asked by At

I'm attempting to run an espresso test on a simple pre-fabricated Android app using the Spoon plugin to take screenshots and test on multiple devices in Kotlin. I looked at the official Github example but can't seem to get it working. I'm thinking it might be an issue with my understanding of lambda functions in Kotlin, as I've never used them before.

Currently, my code is freezing after the first command in the lambda function.

@Test
    fun firstFragmentTest()
    {
        scenario.scenario.onActivity { activity ->
            Log.d("First Fragment Test: ", "Testing to make sure first fragment is accurate")

            //checking to see if center text is accurate
            onView(withId(R.id.textview_first)).check(matches(withText("Hello first fragment")))
            Log.d("First Fragment Test: ", "Center text is accurate")
            Spoon.screenshot(activity, "FirstFragment", "ButtonTests", "firstFragmentTest")

            //checking to see if button text is accurate
            onView(withId(R.id.button_first)).check(matches(withText("Next")))
            Log.d("First Fragment Test: ", "Button text is accurate")

            //checking to see if top text is accurate
            //onView(withId(R.id.FirstFragment)).check(matches(withText("First Fragment")))
            Log.d("First Fragment Test: ", "First fragment text is accurate")
        }
    }

This is my logcat:

2022-05-31 13:43:39.640 2155-2190/com.example.problem2_21 D/Previous Button Test:: Testing functionality of previous button
2022-05-31 13:43:39.643 2155-2155/com.example.problem2_21 I/ViewInteraction: Performing 'single click' action on view view.getId() is <2131230815/com.example.problem2_21:id/button_second>
2022-05-31 13:43:39.972 2155-2190/com.example.problem2_21 D/Previous Button Test:: Previous button pressed
2022-05-31 13:43:39.973 2155-2190/com.example.problem2_21 D/Previous Button Test:: First fragment screen should be displayed
2022-05-31 13:43:39.974 2155-2155/com.example.problem2_21 D/First Fragment Test:: Testing to make sure first fragment is accurate

As you can see, the test stops running after the first line of the lambda. I have multiple test functions that lead into this one, which aren't set-up for Spoon yet and don't use lambda functions, that work fine.

1

There are 1 best solutions below

0
wfootball126 On

I actually figured it out, in case anyone else has the same issue. I just had to place the lambda with the screenshot after the test code like this:

    @Test
    fun firstFragmentTest()
    {
        Log.d("First Fragment Test: ", "Testing to make sure first fragment is accurate")

        //checking to see if center text is accurate
        onView(withId(R.id.textview_first)).check(matches(withText("Hello first fragment")))
        Log.d("First Fragment Test: ", "Center text is accurate")

        //checking to see if button text is accurate
        onView(withId(R.id.button_first)).check(matches(withText("Next")))
        Log.d("First Fragment Test: ", "Button text is accurate")

        //checking to see if top text is accurate
        //onView(withId(R.id.FirstFragment)).check(matches(withText("First Fragment")))
        Log.d("First Fragment Test: ", "First fragment text is accurate")

        scenario.scenario.onActivity { activity ->
            Spoon.screenshot(activity, "FirstFragment", "ButtonTests", "firstFragmentTest")
        }
    }