We are developing mobile apps for our products. As these apps support multiple language(English, french, Italian, German etc) how would one test it automation weather the language is correct or not. We have to do it manually word by word. If there is any way we can automate the language testing and verification either in development environment or through the phone.
Asked
Active
Viewed 562 times
1 Answers
0
There is no reason for your app to get it wrong if you used strings.xml correctly. To make an automated ui test for any feature you want you can use espresso. You should place this class under androidTest A sample test in espresso would look like this.
@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);
}
Locale helper class to change the locale (which you can find in 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;
}
}
Of course you should add necessary dependiencies of espresso in gradle
Sam
- 31
- 4