Android UI Testing – 本地化字符串测试

huangapple 未分类评论46阅读模式
英文:

Android UI Testing - Localized String testing

问题

这是一个双重问题。

1 - 是否在与Activity关联的UI测试中为了检查每个字符串是否与正确的小部件匹配,跨多种语言添加价值,还是应该采取不同的方法,或者根本不要这样做?

2 - 假设这是正确的方法,我尝试实施了以下帖子中的建议。不幸的是,我的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());
    }

}
英文:

This is a two part question.

1 - Does it add value to the project to check every string matches the correct widget across multiple languages for an Activity's associated UI test or should I take a different approach or just not do this at all?

2 - Assuming this is the right approach I have tried to implement the suggestion from the following post. Unfortunately the locale is not updating for my UI tests and fails one of the two tests depending on the system language that I have already set on my device.

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

}

答案1

得分: 0

为了回答你问题的第一部分,尝试测试不同语言确实会增加价值。我们经常遇到的一个问题是,用户界面是为本地语言设计的,但通常翻译后的语言比本地版本的措辞要长。这可能会导致意外的不良界面行为,比如按钮扩展到屏幕边缘之外,或者内容被推下来。正是这些渲染问题需要进行测试。

我暂时无法评论第二个问题。我刚开始研究如何在 Espresso 中实现区域设置更改。

英文:

To answer the first part of your question, It does add value to test around different languages. A common issue we run in to is that UI's are designed for a local language, but often a translated language has longer verbiage than the local version. This can cause unexpected bad UI behavior, such as buttons expanding beyond the edge of a screen, or content getting pushed down. It's these rendering issues that are good to test around.

I can't comment on the second question yet. I've just started digging in to how to implement the locale change in espresso.

huangapple
  • 本文由 发表于 2020年4月8日 05:57:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/61090032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定