簡體   English   中英

Android UI 測試 - 本地化字符串測試

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

}

要回答您問題的第一部分,它確實增加了圍繞不同語言進行測試的價值。 我們遇到的一個常見問題是 UI 是為本地語言設計的,但翻譯語言的措辭通常比本地版本長。 這可能會導致意外的不良 UI 行為,例如按鈕超出屏幕邊緣,或內容被按下。 正是這些渲染問題非常適合測試。

我還不能評論第二個問題。 我剛剛開始研究如何在 espresso 中實現區域設置更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM