溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

為網(wǎng)上流行論點(diǎn)“UIAutomator不能通過(guò)中文文本查找控件”正名

發(fā)布時(shí)間:2020-06-30 16:16:32 來(lái)源:網(wǎng)絡(luò) 閱讀:262 作者:zhukev 欄目:移動(dòng)開發(fā)

1. 問(wèn)題描述和起因

相信大家學(xué)習(xí)UIAutomator一開始的時(shí)候必然會(huì)看過(guò)一下這篇文章。

  • Android自動(dòng)化測(cè)試(UiAutomator)簡(jiǎn)要介紹

因?yàn)槟阍诎俣容斎險(xiǎn)IAutomator搜索的時(shí)候,該文章是排在第一位的。

但是里面有一段說(shuō)法說(shuō)UIAutomator不能支持通過(guò)中文文本查找控件,這個(gè)說(shuō)法害人不淺,如果不是自己去實(shí)踐調(diào)查過(guò),必然也會(huì)輕易放棄UIAutomator以及使用了它的Appium框架,因?yàn)楸救爽F(xiàn)在工作上將要測(cè)試到的就是全部中文界面的app。

為網(wǎng)上流行論點(diǎn)“UIAutomator不能通過(guò)中文文本查找控件”正名

最為害人的還是文章中用紅色高亮來(lái)標(biāo)志說(shuō)明這個(gè)問(wèn)題,雖然文章中有評(píng)論指出UIAutomator其實(shí)是支持中文搜索控件的,但我相信很多人會(huì)像我一樣一般技術(shù)文章只會(huì)看正文的。

2. 問(wèn)題分析

做完我自己親自去編寫以下代碼去驗(yàn)證這個(gè)問(wèn)題,以下代碼的重點(diǎn)主要是最后幾行想通過(guò)中文來(lái)查找控件(假設(shè)該控件已經(jīng)存在)

package majcit.com.UIAutomatorDemo;  import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.core.UiObject; import com.android.uiautomator.core.UiObjectNotFoundException; import com.android.uiautomator.core.UiScrollable; import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.testrunner.UiAutomatorTestCase; import static org.hamcrest.Matchers.*; import static org.hamcrest.MatcherAssert.assertThat;  public class NotePadTest extends UiAutomatorTestCase { 	 	 public void testDemo() throws UiObjectNotFoundException {   	        UiDevice device = getUiDevice(); 	        device.pressHome();   	        // Start Notepad 	        UiObject appNotes = new UiObject(new UiSelector().text("Notes"));  	        appNotes.click();   	        //Sleep 3 seconds till the app get ready 	        try {   	            Thread.sleep(3000);   	        } catch (InterruptedException e1) {   	            // TODO Auto-generated catch block   	            e1.printStackTrace();   	        }   	         	        //Evoke the system menu option 	        device.pressMenu(); 	        UiObject addNote = new UiObject(new UiSelector().text("Add note")); 	        addNote.click(); 	         	        //Add a new note 	        UiObject noteContent = new UiObject(new UiSelector().className("android.widget.EditText")); 	        noteContent.clearTextField(); 	        noteContent.setText("Note 1"); 	        device.pressMenu(); 	        UiObject save = new UiObject(new UiSelector().text("Save")); 	        save.click(); 	         	        //Find out the new added note entry 	        UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView"));   	        //UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));  	        UiObject note = null; 	        if(noteList.exists()) { 	        	note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true);   	        	//note = noteList.getChildByText(new UiSelector().text("Note1"), "中文筆記", true);  	        } 	        else { 	        	note = new UiObject(new UiSelector().text("中文筆記")); 	        } 	        assertThat(note,notNullValue()); 	         	        note.longClick(); 	         	        UiObject delete = new UiObject(new UiSelector().text("Delete")); 	        delete.click(); 	           	    }    }
運(yùn)行的時(shí)候也確實(shí)碰到了如下的問(wèn)題:

為網(wǎng)上流行論點(diǎn)“UIAutomator不能通過(guò)中文文本查找控件”正名

當(dāng)時(shí)看到log說(shuō)"UiOjbectNotFoundException"的時(shí)候第一反應(yīng)就是以上博客高亮指出的問(wèn)題,以為確實(shí)如作者所說(shuō)的存在這樣的問(wèn)題。但自己換個(gè)角度想,這個(gè)UIAutomator是google弄的,不可能對(duì)unicode字符支持這么基礎(chǔ)的東西都有問(wèn)題啊,所以幾經(jīng)百度谷歌和詢問(wèn),可憐網(wǎng)上UIAutomator與此相關(guān)的資源有如鳳毛麟角,幾經(jīng)艱辛才知道這個(gè)跟eclipse項(xiàng)目的Text file encoding選項(xiàng)有關(guān)系。現(xiàn)在往回想起,以上圖片出現(xiàn)亂碼的時(shí)候其實(shí)不應(yīng)該先先入為主的認(rèn)為引用博客的作者的說(shuō)法是正確的,而應(yīng)該像一網(wǎng)友“小吉”所說(shuō)的應(yīng)該先考慮是否是代碼文件的編碼的問(wèn)題。

3.解決方案

如下圖,把項(xiàng)目默認(rèn)的Text file encoding從GBK改成UTF-8,重新打包運(yùn)行

為網(wǎng)上流行論點(diǎn)“UIAutomator不能通過(guò)中文文本查找控件”正名



    • <tfoot id="emwsq"></tfoot>
    • <pre id="emwsq"><td id="emwsq"></td></pre>
         

        作者

        自主博客

        微信

        CSDN

        天地會(huì)珠海分舵

        http://techgogogo.com


        服務(wù)號(hào):TechGoGoGo

        掃描碼:

        為網(wǎng)上流行論點(diǎn)“UIAutomator不能通過(guò)中文文本查找控件”正名

        向AI問(wèn)一下細(xì)節(jié)

        免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

        AI