appium点击坐标 list 属性没有click属性怎么点击

Android(14)
Appium(14)
我想大家在玩自动化的时候最关心的一定是如何定位元素,因为元素定位不到后面的什么方法都实现不了。
那我们就直接看看APPIUMLIBRARY官方给出的信息:
Introduction
AppiumLibrary is a App testing library for Robot Framework.
Locating elements
All keywords in AppiumLibrary that need to find an element on the app take an argument,
locator. By default, when a locator value is provided, it is matched against the key attributes of the particular element type. For example,
id and name are key attributes to all elements, and locating elements is easy using just the
id as a locator. For example:
Click Element my_element
Appium additionally supports some of the Mobile JSON Wire Protocol&locator strategies It is also possible to specify the approach AppiumLibrary should take to find an element by specifying a lookup strategy with a locator prefix. Supported strategies
Description
identifier
Click Element | identifier=my_element
Matches by @id or @name attribute
Click Element | id=my_element
Matches by @id attribute
Click Element | name=my_element
Matches by @name attribute
Click Element | xpath=//UIATableView/UIATableCell/UIAButton
Matches with arbitrary XPath
Click Element | class=UIAPickerWheel
Matches by class
accessibility_id
Click Element | accessibility_id=t
Accessibility options utilize.
Click Element | android=new UiSelector().description('Apps')
Matches by Android UI Automator
Click Element | ios=.buttons().withName('Apps')
Matches by iOS UI Automation
Click Element | css=.green_button
Matches by css in webview
这里的方法有很多,接下来作者来给大家说说每个方法分别对应ANDROID界面控件的什么属性,大致怎么用的。
1.&identifier
identifier
Click Element&|&identifier=my_element
Matches by @id or @name attribute
通过描述,我们可见对应的属性为ID 或者NAME,如果有过ANDROID开发经验的同学们,一定会奇怪ANDROID控件中有ID属性,但是没有name属性,在这里作者告诉大家一个SDK的工具,在TOOLS目录中【uiautomatorviewer.bat】,双击可以打开,这是一个类似QTP中SPY的工具,可以用来抓控件,如图所示:
更具这个工具,我来告诉大家,文档中的ID和NAME属性分别对应的为【resource-id】和【text】
Click Element&|&id=my_element
Matches by @id attribute
Click Element&|&name=my_element
Matches by @name attribute
就不多说了对应1就可以了
Click Element&|&xpath=//UIATableView/UIATableCell/UIAButton
Matches with arbitrary XPath
这是一个较为新出寻址方式,用法就像文件路径,一般使用方法为:“//android.widget.LinearLayout/android.widget.LinearLayout/android.widget.TextView”
就是一层一层的写下去,用到的属性主要是【class】,当然XPATH不会这么呆板,它还有其他的用法(通过特殊属性、序列等等定位的),在这里作者也就不过多啰嗦了,有兴趣的同学可以去W3C上看看,在这里作者无私奉献给出网址
.cn/xpath/index.asp
4.class,对应属性就是【class】
Click Element&|&class=UIAPickerWheel
Matches by class
5.accessibility_id,对应属性为【content-desc】
accessibility_id
Click Element&|&accessibility_id=t
Accessibility options utilize.
6.android,这是调用UIAUTOMATOR的方法来实现元素定位,new UiSelector().之后可以接很多方法,不光光是description,还有TEXT,NAME,DESCRIPTIONCONTAINS等等,具体的内容可以用【UiSelector】在百度中搜索,很多这方面的知识。
Click Element&|&android=new UiSelector().description('Apps')
Matches by Android UI Automator
7.IOS不用说大家都知道这个是用在苹果测试上的,好吧作者还未涉足苹果,就不卖乖了
Click Element&|&ios=.buttons().withName('Apps')
Matches by iOS UI Automation
8.CSS这个方法,作者坦白没自己用过,见过开发玩过,用起来很方便,但是据说很危险很多人都不建议使用CSS,都建议使用XPATH,作者大部分用的也是XPATH,至于是不是也被蒙在鼓里,作者也不好说,同学们自己把握,说不定这个是个原子弹,大杀器。
Click Element&|&css=.green_button
Matches by css in webview
好啦~关于APPIUM里的定位也就都讲完了,下面趁作者过年期间有空给大家唠唠嗑
1.首先就说说定位的方法吧,作者已经把方法都试了遍了,发现identifier,accessibility_id,android这三个不怎么好用,主要还是用了ID,NAME,XPATH,
2.然后XPATH中如果用到【CONTENT-DESC】属性来定位话,有时会定位不到,如果这个属性的字符串里有【空格】,基本就残废了,这点是作者血淋淋的教训,
3.坐标点击函数,对图形化的控件效果较好,但是对于VIEW控件却支持不佳
Click on a point
4.个人感觉目前这套自动化测试方案对安卓纯APP较为适宜,但是遇到混合测试则有点力不从心,就拿作者来说目前有个微信公众号的自动化测试项目,结果发现各种找不到元素,各种没有反应,各种方法无效。
恩~作者也不吐槽了,毕竟APPIUMLIBRARY出来的时间也不长,还是去社区提交BUG吧,还有就是作者个人感觉,同学们除了用APPIUMLIBRARY之外,也可以尝试自己封装自己的LIBRARY,或者重写APPIUMLIBRARY的部分方法,作者先坦白,作者还没有这个能力,作者正在往这方面努力。作者希望可以通过自己的博客结交更多志同道合的朋友,一起为中国的自动化测试道路添砖加瓦,哈哈哈,不要拍我~不要拍脸~
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:10220次
排名:千里之外
原创:16篇
(1)(3)(8)(3)(4)Appium&获取元素控件及其属性
以上文为例&
先来分析下脚本
& WebElement el = driver.findElement(By.name("Add
Contact"));
& //根据控件的name来获取控件
& el.click();
& //对控件执行点击操作
& List textFieldsList =
driver.findElements(By.tagName("textfield"));
& //根据控件的tagName来获取所有控件
& textFieldsList.get(0).sendKeys("Some
//对页面上第一个textfield类型的控件的赋值输入Some Name
textFieldsList.get(2).sendKeys("");
//对页面上第三个textfield类型的控件的赋值输入
driver.findElement(By.name("Save")).click();
&&//对名为Save的控件执行点击操作
由此可见控件至少有tagName,name ,text 三个属性。
一、获取控件属性
1.获取控件类名
System.out.println("[Button Add Contact]Class:
"+el.getClass());
2.获取控件类型&
System.out.println("[Button Add Contact]TagName:
"+el.getTagName());
3.获取控件的值
System.out.println("[Button Add Contact]Text: "+el.getText());
输出如下:
[Button Add Contact]Class:
class org.openqa.selenium.remote.RemoteWebElement
[Button Add Contact]TagName: android.widget.Button
[Button Add Contact]Text: Add Contact
二、获取元素的方式
1.元素类型(tagname)
通过UI的控件类型
List textFieldsList = driver.findElements(By.tagName("textfield"));
注:在android下文本输入框的类型是EditText,而是上面举例写的是textfield,那是因为android /
ios都要对与seleium都有一个元素映射,对于Android下的元素对应, 可以参考
2.元素的位置(xpath)
具有一定约束的路径抽象标示, 基于XPath方式
3.元素的值(name)
driver.findElement(By.name("Save")).click();
通过元素的文本(text), 标签(label),描述信息(content‐desc) ,或者id标示来查找
4.元素的ID
driver.findElement(By.id("com.example.android.contactmanager:id/contactNameEditText")).getText()
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。1536人阅读
Appium(11)
1、关于没有name,没有ID的元素的定位---通用篇
解题思路:
因为没有name,id;其实剩下的选择已不多,要么xpath,要么className。xpath木有好印象(稳定性不高,加之1.0x后需要写全路径,相当崩溃),我决定十分个人色彩的建议使用className。
具体方法:
用className获取所有该类型的元素;
根据元素的位置确定元素的
获取目标元素。
&span style=&white-space:pre&& &/span&//获取ImageView的所有元素
List&WebElement& lis = driver.findElementsByClassName(&android.widget.ImageView&);
//获取列表中第一个元素
WebEelement targetEle = lis.get(0);
你可以自己定义一个className的常量,然后封装个方法,提供大家使用:
public List&WebElement& getAllImges(){
List&WebElement& lis = driver.findElementsByClassName(AndroidClassName.IMAGEVIEW);
使用的时候,就只需要调用此方法就可以了
此方法的好处:通吃的方法,基本可解决定位的问题;能解决多语言(自然语言)的困扰;
缺点:因为系统需要去获取List,就目前来看,运行效率不是很高,那直接是可以用肉眼看的到的速度在跑。。。
2、关于没有name,没有ID的元素的定位---特用篇
因为是特用,所以需要对页面元素做进一步的分析,找出其中的特性。这些特性主要为className,index,以及clickable等属性。
场景1:同一个页面有10个ImageView对象,而我们的目标元素的index为4,而同为ImageView且index为4的页面只有2个。那我们就可以根据这2个条件来组合查询条件,提高效率。
List&WebElement& lis = driver.findElementsByAndroidUIAutomator(&new UiSelector().className(&+&android.widget.ImageView&+&).index(4)&);
基于这个场景可以封装一个方法:
public List&WebElement& getElementsByClassAndIndex(String classname,int index){
List&WebElement& lis =
lis = driver.findElementsByAndroidUIAutomator(&new UiSelector().className(&+classname+&).index(&+index+&)&);
场景2:同一个页面有10个ImageView对象,而其中index为4的有5个,而这时我们发现我们的目标元素的是clickable的。然后review页面发现,同时满足上述条件的只有2个。那么。。。
List&WebElement& lis = driver.findElementsByAndroidUIAutomator(&new UiSelector().className(&+&android.widget.ImageView&+&).index(4).clickable(true)&);同样可以封装一个方法:
public List&WebElement& getElementsByClassAndIndexAndClickable(String classname,int index){
List&WebElement& lis =
lis = driver.findElementsByAndroidUIAutomator(&new UiSelector().className(&+classname+&).index(&+index+&).clickable(true)&);
场景3:在分析页面元素的时候发现,页面相对比较简单,而且其中只有目标元素的index为4.
driver.findElementByAndroidUIAutomator(&new UiSelector().index(&+index+&)&);
同样得到一个方法
public WebElement getElementByIndex(int index){
&span style=&white-space:pre&& &/span&return driver.findElementByAndroidUIAutomator(&new UiSelector().index(&+index+&)&);
3、关于输入框有默认text,且当你选中时,不会清空的清空的处理。
类似的情况,用element.clear()是没有效果的。那怎么办呢? 不急,appium提供了很多的操作,直击不行,我们就上组合拳。
public void clear(WebElement el){
//选中输入框
el.click();
//将光标移到最后
driver.sendKeyEvent(123);
//获取字符串长度
String txt = el.getText();
for(int i=0;i&txt.length();i++){
driver.sendKeyEvent(67);//一个个的删除。。。。。
4、关于UiAutomator无法识别的元素
整体思路:不要忘了咱的手机是可以点击的。
获取相对坐标,点击,就这么简单。(不用绝对坐标是保证程序能在不同的分辨率下运行)
下面一起讨论下相对坐标的获取:找一个页面,这个页面上有相关的元素的位置与目标位置类似,或者根据这些元素能获取到目标的大致位置。
上段代码,帮助理解:
public List&Point& getFileMngPoint(){
List&Point& list = new ArrayList&Point&();
WebElement scrol = driver.findElementByClassName(AndroidClassName.HORIZONTALSCROLL);
List&WebElement& imgs = scrol.findElements(By.className(AndroidClassName.IMAGEVIEW));
//以上仅获取类似位置的元素
Point e0 = getCenterPoint(imgs.get(0));
Point e1 = getCenterPoint(imgs.get(1), imgs.get(2));
Point e2 = getCenterPoint(imgs.get(3));
Point e4 = getCenterPoint(imgs.get(2),imgs.get(3));
Point e5 = getCenterPoint(getElementsByClassAndIndexAndClickable(AndroidClassName.IMAGEVIEW, 0).get(4));
//以上获取元素的中心坐标,相关方法是自己写的
list.add(e0);
list.add(e1);
list.add(e2);
list.add(e4);
list.add(e5);
关于使用className的方法时,如果我们的list能在页面加载完成之后就能被缓存起来,那么其运行效率还能提高不少
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:264535次
积分:2766
积分:2766
排名:第9439名
原创:56篇
转载:72篇
评论:33条
(2)(1)(1)(1)(1)(1)(4)(5)(3)(1)(6)(4)(1)(1)(1)(4)(4)(3)(23)(25)(11)(17)(3)(5)(2)Appium源码分析(七)-click - 博客频道 - CSDN.NET
saii的专栏
测试,注:不要加博客qq哦,不常用
分类:appium
click源代码
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
if (command.isElementCommand()) {
final AndroidElement el = command.getElement();
el.click();
return getSuccessResult(true);
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final Exception e) {
return getErrorResult("Unknown error");
final Hashtable&String, Object& params = command.params();
Point coords = new Point(Double.parseDouble(params.get("x").toString()),
Double.parseDouble(params.get("y").toString()) );
coords = PositionHelper.getDeviceAbsPos(coords);
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final InvalidCoordinatesException e) {
return new AndroidCommandResult(WDStatus.INVALID_ELEMENT_COORDINATES,
e.getMessage());
final boolean res = UiDevice.getInstance().click(coords.x.intValue(), coords.y.intValue());
return getSuccessResult(res);
首先我们先看前面两句句吧
final AndroidElement el = command.getElement();
el.click();
通过command的方法 getElement获取到一个AndroidElement的对象。再调用该对象的click方法。其实在最开始的时候应该就说过,在API&18的系统下,appium采用的是UiAutomator的框架,那么的话在不看AndroidElement的情况下,我们应该也能够猜测出来,它里面肯定实际还是调用的是UiObject的click方法的,首先我们得先看看command.getElement里面做了啥。
public AndroidElement getElement() throws JSONException {
String elId = (String) params().get("elementId");
return AndroidElementsHash.getInstance().getElement(elId);
根据我们的分析,这里上面的代码应该就很容易懂了,返回的是AndroidElement对象,里面就包含了UiObject el的元素,所以才可以调用el.click()方法。
再来就是剩余的如果参数中不包含elementId,说明点击的操作是针对于坐标来进行的。这里还是一样,进行了一些坐标的转换后,再调用uidevice的click方法。
排名:千里之外
(28)(8)(19)(2)(7)(2)(4)(5)(3)(9)(6)(16)(4)(1)

我要回帖

更多关于 appium click a point 的文章

 

随机推荐