UIAutomator定位Android控件的方法实践和建议

2024-08-26 05:08

本文主要是介绍UIAutomator定位Android控件的方法实践和建议,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 背景

为了和前文达成一致,这次的实践对象同样也是使用SDK自带的NotePad应用,同样是尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单选项。以下是UIAutomatorViewer对界面的一个截图.

但有一个例外的地方是下文的” 通过伪xpath方法定位控件 “章节实例需要使用到的是NoteEditor这个activity里面的Menu options,因为需要演示通过子控件获得父控件然后得到兄弟控件的功能,UIAutomatorViewer截图如下。

2. 通过文本信息定位

通过控件的text属性定位控件应该是最常用的一种方法了,毕竟移动应用的屏幕大小有限,存在text重复的可能性并不大,就算真的有重复,可以添加其他定位方法来缩写误差。

2.1 UISelector.text方法

addNote = new UiObject(new UiSelector().text("Add note"));	assertEquals(addNote.getText(),"Add note");
该方法通过直接查找当前界面上所有的控件来比较每个控件的text属性是否如预期值来定位控件,挺好理解的,所以就没有必要细说了。

2.2. UISelector.textContains方法

addNote = new UiObject(new UiSelector().textContains("Add"));	assertEquals(addNote.getText(),"Add note");
此方法跟以上方法类似,但是不需要输入控件的全部text信息。

2.3 UISelector.textStartsWith方法

addNote = new UiObject(new UiSelector().textStartsWith("Add"));	assertEquals(addNote.getText(),"Add note");
顾名思义,通过判断一个控件的text的开始是否和预期的字串相吻合来获得控件,其实个人觉得这个方法存在的必要性不强,因为它的功能完全可以用上面的方法或者下面的正则表达式的方法取代。况且既然你提供了textStartsWith方法,为什么你不提供个textEndWith的方法呢!

2.4 UISelector.textMatches方法

addNote = new UiObject(new UiSelector().textMatches("^Add.*"));	assertEquals(addNote.getText(),"Add note");
这个方法是通过正则表达式的方式来比较控件的text来定位控件,这里有意思的是用户使用的正则表达式是有限制的,请看该方法的官方描述:”  Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app). The text for the widget must match exactly with the string in your input argument  “。第一句我们不用管它,关键是第二句,翻译过来就是”  目标控件的text(的所有内容)必须和我们输入的正则表达式完全匹配  “。什么意思呢?  意思就是你不能像往常的正则表达式那样通过比较text的部分吻合来获得控件  。以下面代码为例子:
addNote = new UiObject(new UiSelector().textMatches("^Add"));	assertEquals(addNote.getText(),"Add note");
正常来说这个正则表达式是没有问题的,它的意思就是想要“获取以Add开头的text的控件,至于Add字串口面是什么值,没有必要去管它”。但是按照我们上面的官方描述,这样子是不行的,你必须要把正则表达式补充完整以使得正而表达式和控件的text完全进行匹配,至于你用什么通配符或者字串就完全按照正则表达式的语法了。

注意这个限制在UISlector使用所有的正则表达式相关的方法中都有效哦。

3 通过控件的ClassName定位

通过这种方法定位控件存在的一个问题是很容易发生重复,所以一般都是先用这种方法去narrow down目标控件,然后再去添加其他如text判断等条件进行控件定位。

3.1 UISelector.className方法

addNote = new UiObject(new UiSelector().className("android.widget.TextView").text("Add note"));	assertEquals(addNote.getText(),"Add note");
实例中首先通过ClassName找到所有的TextView控件,然后再在这些TextView控件查找text是”Add note“的控件。

3.2 UISelector.classNameMatches方法

addNote = new UiObject(new UiSelector().classNameMatches(".*TextView$"));	assertEquals(addNote.getText(),"Add note");
通过正则表达式判断className是否和预期的一致,  注意正则表达式的限制和章节2.4描述的一致  。

4. 通过伪xpath方法定位

UISelector类提供了一些方法根据控件在界面的XML布局中的层级关系来进行定位,但是UIAutomator又没有真正的提供类似Appium的findElementWithXpath相关的方法,所以这里我就称之为伪xpath方法。

这个章节使用到的不再是NotesList那个activity里面的menu options,而是NoteEditor这个activity里面的Menu options,里面不止有一个Menu entry。

4.1 通过UiSelector.fromParent或UiObject.getFromParent方法

这种方法我觉得最有用的情况是测试代码当前在操作的是同一层级的一组控件中的某一个控件,转而又需要操作同一层级的另外一个控件的时候。下面的实例就是通过save控件的父控件找到其同一层级的兄弟控件delete。这里分别列出了通过UiObject.getFromParent方法和UiSelector.fromParent方法的实例,事实上他们的功能是一样的。

UiObject.getFromPatrent方法:

save =new UiObject(new UiSelector().text("Save"));	assertEquals(save.getText(),"Save");	delete = save.getFromParent(new UiSelector().text("Delete"));	assertEquals(delete.getText(),"Delete");
UiSelector.fromParent方法(这个例子有点迂回笨拙,但为了演示功能就将就着看吧):
delete = new UiObject(new UiSelector().text("Save").fromParent(new UiSelector().text("Delete")));	assertEquals(delete.getText(),"Delete");

4.2 通过UiSelector.childSelector或UiObject.getChild方法

这种方法是在已知父控件的时候如何快速的查找该父控件下面的子控件。

UiObject.getChild方法:

UiObject parentView = new UiObject(new UiSelector().className("android.view.View"));	save = parentView.getChild(new UiSelector().text("Save"));	assertEquals(save.getText(),"Save");
UiSelector.childSelector方法:
save = new UiObject(new UiSelector().className("android.view.View").childSelector(new UiSelector().text("Save")));	assertEquals(save.getText(),"Save");

5. 通过控件ID定位

在Android API Level18及其以上的版本增加了一个Android控件的属性ResourceId, 所以要注意在使用这种方法之前先确保你的目标测试设备和你的UIAutomoator库jar包使用的都是API Level 18以上的版本 。例如我自己使用的就是本地sdk中版本19的库:D:/Develops/AndroidSDK/platforms/android-19/uiautomator.jar

5.1 UiSelector.resourceId方法

addNote = new UiObject(new UiSelector().resourceId("android:id/title"));	assertEquals(addNote.getText(),"Add note");

5.2 UiSelector.resourceIdMatches方法

addNote = new UiObject(new UiSelector().resourceIdMatches(".+id/title"));	assertEquals(addNote.getText(),"Add note");
注意正则表达式的限制和章节2.4描述的一致

6. 通过contentDescription定位

在UiAutomator框架和使用了Uiautomator框架的Appium中,控件的属性contentDescription一直是强调开发人员需要添加进去的,因为

  • 有些控件使用其他办法很难或者根本没有办法定位
  • 最重要的是给每个控件的contentDescription设计个唯一值让我们可以非常快速的定位控件,让我们足够敏捷!

以下的实例并没有真正跑过的,因为Notepad应用上面的控件是没有contentDescription这个属性的,但是如果我们假设Add note这个控件的contentDescription是“AddNoteMenuDesc”的话,代码的相应写法应该就如下了。

6.1 UiSelector.description方法

addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc));	assertEquals(addNote.getText(),"Add note");
</pre><h2>6.2 UiSelector.descriptionContains方法</h2></div><div><pre name="code" class="java">	addNote = new UiObject(new UiSelector().descriptionContains("AddNote"));	assertEquals(addNote.getText(),"Add note");

6.3 UiSelector.descriptionStartWith方法

addNote = new UiObject(new UiSelector().descriptionStartsWith("AddNote"));	assertEquals(addNote.getText(),"Add note");

6.4 UiSelector.descriptionMatches方法

//addNote = new UiObject(new UiSelector().descriptionMatches("^AddNote.*$"));	//assertEquals(addNote.getText(),"Add note");

7.通过其他方法定位

除了以上比较常用的方法外,UIAutomator还支持其他一些方法,比如根据控件属性是否可点击可聚焦可长按等来缩小要定位的控件的范围,具体使用方法不一一列举,可以查看以下测试验证代码。
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 UISelectorFindElementTest 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"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject (new UiSelector().checked(false).clickable(true));	assertEquals(addNote.getText(),"Add note");			addNote = new UiObject(new UiSelector().className("android.widget.TextView").text("Add note"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().classNameMatches(".*TextView$"));	assertEquals(addNote.getText(),"Add note");		//addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc));	//assertEquals(addNote.getText(),"Add note");		//addNote = new UiObject(new UiSelector().descriptionContains("AddNote"));	//assertEquals(addNote.getText(),"Add note");		//addNote = new UiObject(new UiSelector().descriptionStartsWith("AddNote"));	//assertEquals(addNote.getText(),"Add note");		//addNote = new UiObject(new UiSelector().descriptionMatches("^AddNote.*$"));	//assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().focusable(true).text("Add note"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().focused(false).text("Add note"));	assertEquals(addNote.getText(),"Add note");			//TBD	//addNote = new UiObject(new UiSelector().fromParent(selector))		addNote = new UiObject(new UiSelector().index(0).text("Add note"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().className("android.widget.TextView").enabled(true).instance(0));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().longClickable(false).text("Add note"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().text("Add note"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().textContains("Add"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().textStartsWith("Add"));	assertEquals(addNote.getText(),"Add note");			addNote = new UiObject(new UiSelector().textMatches("Add.*"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().resourceId("android:id/title"));	assertEquals(addNote.getText(),"Add note");		addNote = new UiObject(new UiSelector().resourceIdMatches(".+id/title"));	assertEquals(addNote.getText(),"Add note");		//Go to the editor activity, need to cancel menu options first	device.pressMenu();	//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"), "Note1", true); 	}	else {		note = new UiObject(new UiSelector().text("Note1"));	}	assertNotNull(note);		//Go to the NoteEditor activity	note.click();	device.pressMenu();		UiObject save = null;	UiObject delete = null;		save =new UiObject(new UiSelector().text("Save"));	assertEquals(save.getText(),"Save");	delete = save.getFromParent(new UiSelector().text("Delete"));	assertEquals(delete.getText(),"Delete");		delete = new UiObject(new UiSelector().text("Save").fromParent(new UiSelector().text("Delete")));	assertEquals(delete.getText(),"Delete");		save = new UiObject(new UiSelector().className("android.view.View").childSelector(new UiSelector().text("Save")));	assertEquals(save.getText(),"Save");		UiObject parentView = new UiObject(new UiSelector().className("android.view.View"));	save = parentView.getChild(new UiSelector().text("Save"));	assertEquals(save.getText(),"Save");				}}

这篇关于UIAutomator定位Android控件的方法实践和建议的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1107630

相关文章

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

mac安装nvm(node.js)多版本管理实践步骤

《mac安装nvm(node.js)多版本管理实践步骤》:本文主要介绍mac安装nvm(node.js)多版本管理的相关资料,NVM是一个用于管理多个Node.js版本的命令行工具,它允许开发者在... 目录NVM功能简介MAC安装实践一、下载nvm二、安装nvm三、安装node.js总结NVM功能简介N

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接