【发布时间】:2020-04-07 21:57:50
【问题描述】:
这是一个两部分的问题。
1 - 它是否为项目增加了价值,以检查每个字符串是否与跨多种语言的正确小部件匹配以进行 Activity 的关联 UI 测试,还是我应该采取不同的方法还是根本不这样做?
2 - 假设这是我尝试实施以下post 的建议的正确方法。不幸的是,我的 UI 测试的语言环境没有更新,并且根据我已经在我的设备上设置的系统语言,这两个测试之一失败了。
@RunWith(AndroidJUnit4ClassRunner.class)
public class RegisterActivityTest {
@Test
public void test_onScreenLoadStringsDisplayedInEnglish() {
testEnglishLocale();
onView(withId(R.id.welcome_text_view)).check(matches(
withText(WELCOME_EN)));
}
@Test
public void test_onScreenLoadStringsDisplayedInSpanish() {
testSpanishLocale();
onView(withId(R.id.welcome_text_view)).check(matches(
withText(WELCOME_ES)));
}
private void testEnglishLocale() {
setLocale("en", "US");
}
private void testSpanishLocale() {
setLocale("es", "ES");
}
private void setLocale(String language, String country) {
Locale locale = new Locale(language, country);
// here we update locale for date formatters
Locale.setDefault(locale);
// update locale for app resources
Resources res = InstrumentationRegistry.getInstrumentation().getTargetContext().getResources();
Configuration config = res.getConfiguration();
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
【问题讨论】:
标签: java android localization android-espresso ui-testing