vendredi 11 septembre 2020

How to get activity and pass it to Spoon.screenshot

package com.github.avanlex;
import android.Manifest;
import android.app.Activity;

import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;

import com.squareup.spoon.Spoon;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class AnagramsTextReverseBehaviorTest {

    public static final String STRING_TO_BE_TYPED = "espresso";
    public static final String STRING_TO_BE_EXPECTED = "osserpse";
    private static final String PACKAGE_NAME = "com.github.avanlex";

    /*********************************************************************************************
     * Google recommends using ActivityScenarioRule instead ActivityTestRule, but I don't know How?
     *********************************************************************************************
     */
    @Rule public ActivityScenarioRule<MainActivity> activityScenarioRule
            = new ActivityScenarioRule<>(MainActivity.class);  

    @Test
    public void changeText_sameActivity() {
        // Type text and then press the button.
        onView(withId(R.id.tietStringToReverse))
            .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
        onView(withId(R.id.btnReverse)).perform(click());
        Activity activity = main.getActivity();  // GET ACTIVITY IS DEPRECATED :(
        activityScenarioRule.getScenario().onActivity(act -> { 
            // CAUSES ERROR
            Spoon.screenshot(act, "changeText_sameActivity");
        });
        //Spoon.screenshot(activity, "changeText_sameActivity");
        // Check that the text was reversed.
        onView(withId(R.id.tietReversedString)).check(matches(withText(STRING_TO_BE_EXPECTED)));
    }
}

I'm trying to get Activity and pass to Spoon to take screenshot, but Google documentation says getActivity() from androidx.test.rule.ActivityTestRule is deprecated.

I tryed:

    @Rule public final ActivityTestRule<MainActivity> main = new ActivityTestRule(MainActivity.class, true);

It works well, but in the google documentation it's marked as deprecated.

Then I searched way to update my code avoiding deprecated API:

        activityScenarioRule.getScenario().onActivity(act -> { 
            // CAUSES ERROR: java.util.concurrent.ExecutionException: 
            // java.lang.IllegalArgumentException: Could not find test class!
            Spoon.screenshot(act, "changeText_sameActivity");
        });

The only way I found to get activity causes the app crashing. Please help 🙏

Aucun commentaire:

Enregistrer un commentaire