vendredi 20 avril 2018

Fire tests from Android App

Is is possible to fire up tests (AndroidJUnit4) in an Android app? For example, I have this test and I would like to add a button in my app that would run this test. To be mentioned that this piece of code will test another app (i.e. Chrome)

Currently this test works if i run it from Android Studio, but will be terrific if it can run from an app.

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ExampleInstrumentedTest {
    private static final String TAG = "@@MyClassNameHere@@";
    private static final long DEFAULT_TIMEOUT = 1000;
    private UiDevice mDevice;

    @Before
    public void setUp() {
        // Initialize UiDevice instance
        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        mDevice = UiDevice.getInstance(instrumentation);
    }

    @Test
    public void culebraGeneratedTest() throws Exception {
        mDevice.pressHome();
        mDevice.click(559, 1708);
        mDevice.findObject(By.res("net.oneplus.launcher:id/all_apps_handle").clazz("android.widget.ImageView").text(Pattern.compile("")).pkg("net.oneplus.launcher")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
        (new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView").resourceId("net.oneplus.launcher:id/apps_list_view").index(2).packageName("net.oneplus.launcher"))).getChildByDescription(new UiSelector().description("Chrome"), "Chrome", true).click();


        mDevice.swipe(500,1300,500,400,50);
        mDevice.waitForIdle(1000);
    }

}

Aucun commentaire:

Enregistrer un commentaire