【问题标题】:Logout from Facebook in Android Espresso UI Test在 Android Espresso UI 测试中从 Facebook 注销
【发布时间】:2018-10-26 07:50:59
【问题描述】:

我有一些使用 Facebook SDK 的自动化测试: - 在我的应用中注册 - 登录我的应用程序

我运行注册测试,它基本上调用 chrome,然后登录 Facebook 以使用信息在我的应用中继续注册过程。

但是当我想在模拟器上运行第二个测试时,登录,我已经在 Chrome 上登录 Facebook,我想使用另一个帐户进行此测试...

有没有办法在我的测试中退出?

【问题讨论】:

    标签: android facebook testing logout android-espresso


    【解决方案1】:

    由于这个问题我找到了一种在 UI 测试中清理 chrome 的方法,这应该适用于法语或英语的手机:

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多