【问题标题】:Multiple language testing on mobile app(android/ios)移动应用程序上的多语言测试(android/ios)
【发布时间】:2018-06-19 11:03:32
【问题描述】:

我们正在为我们的产品开发移动应用程序。由于这些应用程序支持多种语言(英语、法语、意大利语、德语等),因此如何在语言正确与否的情况下对其进行自动化测试。我们必须逐字逐句手动完成。如果有任何方法,我们可以在开发环境中或通过电话自动进行语言测试和验证。

【问题讨论】:

    标签: android automated-tests mobile-application


    【解决方案1】:

    如果您正确使用了 strings.xml,您的应用就没有理由出错。要对您想要的任何功能进行自动化 ui 测试,您可以使用 espresso。你应该把这个类放在 androidTest 下 espresso 中的示例测试如下所示。

    @RunWith(AndroidJUnit4.class)
    public class MainActivityTest {
    
     @Rule
    public ActivityTestRule<MainActivity> mainActivityTestRule = new ActivityTestRule<>(MainActivity.class);
    
    @Test
    public void changeLocalization() {
        changeLocal(TURKISH_ABBREVATION); // For french it is "fr"
        onView(withText(TURKISH_TEXT)).check(matches(isDisplayed())); // This is where the magic happens. If you want to check all texts you should add all of them but one is enough to see if strings are translated.
    }
    public void changeLocal(String language) {
        Context context = InstrumentationRegistry.getTargetContext();
        LocaleHelper.setLocale(context, language);
    }
    

    用于更改语言环境的语言环境助手类(您可以在 SO 中找到)

    public final class LocaleHelper {
    
        private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
    
        /**
         * Prevents the util class from being instantiated.
         */
        private LocaleHelper() {
        }
    
    
        public static Context onAttach(Context context) {
            final String lang = getPersistedData(context, Locale.getDefault().getLanguage());
            return setLocale(context, lang);
        }
    
        public static Context onAttach(Context context, String defaultLanguage) {
            final String lang = getPersistedData(context, defaultLanguage);
            return setLocale(context, lang);
        }
    
        public static String getLanguage(Context context) {
            return getPersistedData(context, Locale.getDefault().getLanguage());
        }
    
        public static Context setLocale(Context context, String language) {
            persist(context, language);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return updateResources(context, language);
            }
    
            return updateResourcesLegacy(context, language);
        }
    
        private static String getPersistedData(Context context, String defaultLanguage) {
            final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
        }
    
        private static void persist(Context context, String language) {
            final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            final SharedPreferences.Editor editor = preferences.edit();
    
            editor.putString(SELECTED_LANGUAGE, language);
            editor.apply();
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        private static Context updateResources(Context context, String language) {
            final Locale locale = new Locale(language);
            Locale.setDefault(locale);
    
            final Configuration configuration = context.getResources().getConfiguration();
            configuration.setLocale(locale);
            configuration.setLayoutDirection(locale);
    
            return context.createConfigurationContext(configuration);
        }
    
        private static Context updateResourcesLegacy(Context context, String language) {
            final Locale locale = new Locale(language);
            Locale.setDefault(locale);
    
            final Resources resources = context.getResources();
    
            final Configuration configuration = resources.getConfiguration();
            configuration.locale = locale;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                configuration.setLayoutDirection(locale);
            }
    
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
            return context;
        }
    }
    

    当然你应该在gradle中添加espresso的必要依赖

    【讨论】:

      猜你喜欢
      • 2018-08-17
      • 2015-06-05
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多