zoukankan      html  css  js  c++  java
  • 为网上流行论点“UIAutomator不能通过中文文本查找控件”正名

    1. 问题描述和起因

    相信大家学习UIAutomator一开始的时候必然会看过一下这篇文章。 因为你在百度输入UIAutomator搜索的时候,该文章是排在第一位的。 但是里面有一段说法说UIAutomator不能支持通过中文文本查找控件,这个说法害人不浅,如果不是自己去实践调查过,必然也会轻易放弃UIAutomator以及使用了它的Appium框架,因为本人现在工作上将要测试到的就是全部中文界面的app。 最为害人的还是文章中用红色高亮来标志说明这个问题,虽然文章中有评论指出UIAutomator其实是支持中文搜索控件的,但我相信很多人会像我一样一般技术文章只会看正文的。

    2. 问题分析

    做完我自己亲自去编写以下代码去验证这个问题,以下代码的重点主要是最后几行想通过中文来查找控件(假设该控件已经存在)
    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();
    	          
    	    }  
    
    }
    运行的时候也确实碰到了如下的问题: 当时看到log说"UiOjbectNotFoundException"的时候第一反应就是以上博客高亮指出的问题,以为确实如作者所说的存在这样的问题。但自己换个角度想,这个UIAutomator是google弄的,不可能对unicode字符支持这么基础的东西都有问题啊,所以几经百度谷歌和询问,可怜网上UIAutomator与此相关的资源有如凤毛麟角,几经艰辛才知道这个跟eclipse项目的Text file encoding选项有关系。现在往回想起,以上图片出现乱码的时候其实不应该先先入为主的认为引用博客的作者的说法是正确的,而应该像一网友“小吉”所说的应该先考虑是否是代码文件的编码的问题。

    3.解决方案

    如下图,把项目默认的Text file encoding从GBK改成UTF-8,重新打包运行  
    作者 自主博客 微信服务号及扫描码 CSDN
    天地会珠海分舵 http://techgogogo.com 服务号:TechGoGoGo扫描码: qrcode_for_gh_0388b3c825f5_430 http://blog.csdn.net/zhubaitian
  • 相关阅读:
    Java蛇形数组的简单实现代码
    Android Studio生成javadoc出错的解决办法
    AngularJS指令嵌套时link函数执行顺序的问题
    [转]如果我有jQuery背景,我应该如何切换到AngularJS的思维模式?
    扩展ViewFlow避免和ViewPager滑动冲突,同时支持无限循环,并完美和CircleFlowIndicator结合
    人机交互的新方向:智能聊天机器人
    利用python自动清除Android工程中的多余资源
    巧用svn create patch(打补丁)方案解决定制版需求
    【Android开发坑系列】之经常被忽略的背景图片内存泄露
    【Android开发坑系列】之try-catch
  • 原文地址:https://www.cnblogs.com/techgogogo/p/4284804.html
Copyright © 2011-2022 走看看