0

I've written a test like following:

@RunWith(AndroidJUnit4.class)
public class MainActivityEspressoTest
{
    private static final ArrayList<String> LANGUAGES = new ArrayList<String>()
    {
        {
            add("de");
            add("en");
            add("es");
        }
    };

    @ClassRule
    public static final LanguageRule localeTestRule = new LanguageRule(new Locale(LANGUAGES.get(0)));
    @Rule
    public final PermissionRule permissionsRule = new PermissionRule(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE});

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void makeScreenshot()
    {
        TestUtil.takeScreenshot(mActivityRule.getActivity().getResources().getConfiguration().locale.getCountry() + "screenshot", 1, mActivityRule.getActivity());
    }
}

What I want is to run this test in all langauges defined in LANGUAGES, is that somehow possible? I don't want to write n different espresso tests for that...

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
prom85
  • 16,896
  • 17
  • 122
  • 242
  • 1
    Did try something like this to create localized screenshots. You can see it on [Github](https://github.com/bleeding182/localized-screenshots) and there is [more explanation in this article](http://blog.davidmedenjak.com/android/2015/12/12/instrumentation-tests-and-localized-screenshots.html) – David Medenjak Mar 19 '17 at 12:08
  • whatever I try, the second run always crashes with `Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''`´... Even when copy&pasting your code.. – prom85 Mar 19 '17 at 12:50
  • You probably missed something. The project itself will build and run just fine, as you can see here: https://circleci.com/gh/bleeding182/localized-screenshots/31#artifacts – David Medenjak Mar 19 '17 at 12:52
  • seems like it is my static permission rule, it only works once... I don't see anything in your code that works with runtime permissions on marshmallow and above... For me, only declaring it in the manifest is not enough. You're targeting API 23, it should not be enough for you either. Why does that work for you? – prom85 Mar 19 '17 at 13:24
  • I ran the test on an API 22 emulator. For manual testing you could grant the permission in the settings, which is some effort, or you could have a look into writing a custom gradle task that grants the permissions after installing the app on your device, e.g. see here: http://stackoverflow.com/a/33848860/1837367 – David Medenjak Mar 19 '17 at 13:35
  • Got it working now by manually setting the permissions before making a screenshot in the test class now (via `InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("pm grant " + InstrumentationRegistry.getTargetContext().getPackageName() + " " + permission);`). Thanks for the help – prom85 Mar 19 '17 at 13:42

0 Answers0