Ad

Logout From Facebook In Android Espresso UI Test

I have some automated tests using the Facebook SDK: - Sign Up in my app - Sign In in my app

I run the Sign Up test, it basically call chrome, and login in Facebook to continue the Sign Up process in my app using the infos.

But when I want to run a second test on the emulator, Sign In, I am already logged in Facebook on Chrome and I would like to use another account for this test...

Is there a way to be logged out in my tests ?

Ad

Answer

Since this question I found a way to clean chrome in UI tests, this should work for phones in french or english :

private void cleanChrome() {
    final UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    try {
        //Clear chrome data
        String urlString = "http://nurburgring.ben-j.fr";
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setPackage("com.android.chrome");
        try {
            KlaxitApp.getContext().startActivity(intent);
        } catch (ActivityNotFoundException ex) {
            // Chrome browser presumably not installed so allow user to choose instead
            intent.setPackage(null);
            KlaxitApp.getContext().startActivity(intent);
        }
        mDevice.wait(Until.findObject(By.clazz(WebView.class)), 10 * 1000);
        //Check if never used
        acceptChromeCgus();

        UiObject urlField = mDevice.findObject(new UiSelector().className(EditText.class));
        urlField.click();
        urlField.setText("chrome://history");
        mDevice.pressEnter();

        Thread.sleep(5000);

        UiObject clearBTN = mDevice.findObject(new UiSelector().className(Button.class).descriptionContains("CLEAR"));
        if (clearBTN.exists()) {
            clearBTN.click();
        } else {
            clearBTN = mDevice.findObject(new UiSelector().textContains("CLEAR"));
            if (clearBTN.exists()) {
                clearBTN.click();
            } else {
                //french phone
                clearBTN = mDevice.findObject(new UiSelector().className(Button.class).textContains("EFFACER LES DONNÉES DE NAVIGATION"));
                if (clearBTN.exists()) {
                    clearBTN.click();
                }
            }
        }

        //Period required on dev phone but not on CI
        UiObject periodBTN = mDevice.findObject(new UiSelector().textContains("Dernière heure"));
        if (periodBTN.exists()) {
            periodBTN.click();
            UiObject allPeriodTV = mDevice.findObject(new UiSelector().textContains("Toutes"));
            if (allPeriodTV.exists()) {
                allPeriodTV.click();
            }
        }

        Thread.sleep(1000);
        clearBTN = mDevice.findObject(new UiSelector().className(Button.class).descriptionContains("CLEAR"));
        if (clearBTN.exists()) {
            clearBTN.click();
        } else {
            //french phone
            clearBTN = mDevice.findObject(new UiSelector().className(Button.class).textContains("EFFACER LES DONNÉES"));
            if (clearBTN.exists()) {
                clearBTN.click();
            }
        }

        UiObject clearDataBTN = mDevice.findObject(new UiSelector().className(Button.class).textContains("CLEAR DATA"));
        if (clearDataBTN.exists()) {
            clearDataBTN.click();
        } else {
            //french phone
            clearDataBTN = mDevice.findObject(new UiSelector().className(Button.class).textContains("EFFACER"));
            if (clearDataBTN.exists()) {
                clearDataBTN.click();
            }
        }

        //Popup
        UiObject okBTN = mDevice.findObject(new UiSelector().className(Button.class).textContains("OK"));
        if (okBTN.exists()) {
            okBTN.click();
        } else {
            okBTN = mDevice.findObject(new UiSelector().className(Button.class).textContains("CLEAR"));
            if (okBTN.exists()) {
                okBTN.click();
            }
        }

        final String connectWith = getResourceString(R.string.Connect_with);
        UiObject connectWithTV = mDevice.findObject(new UiSelector().className(TextView.class).text(connectWith));
        while (!connectWithTV.exists()) {
            Thread.sleep(2000);
            Timber.i("PRESS BACK");
            mDevice.pressBack();
            Thread.sleep(2000);
            connectWithTV = mDevice.findObject(new UiSelector().className(TextView.class).text(connectWith));
        }
    } catch (Exception e) {
        e.printStackTrace();
        mDevice.pressBack();
    }
}
Ad
source: stackoverflow.com
Ad