APP自动化测试关键环节--元素定位,以下我们来了解appium提供的元素定位方法!

1. id定位,id一个控件的唯一标识,由开发人员在项目中指定,如果一个元素有对应的resource-id,我们就可以采用这种方式来实现元素定位操作,但是实际开发中,也有可能app项目的开发人员不是很严谨,一个页面有很多个相同的id,获取到的元素结果是一个集合,所以这种情况我们需要用list进行接收

//找到想要定位的元素并进行点击
androidDriver.findElement(By.id("id")).click();

2. text 定位,java-client旧版本提供了相应的API支持

//根据text属性进行定位
androidDriver.findElement(By.name("登录"));

注意:java-client新版本运行之后会发现定位失败,这是因为java-client 5.0 以后新版本不支持这个API:

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session (WARNING: The server did not provide any stacktrace information)

解决方案:

androidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"登录\")");

注意提示:

因为:

解决办法:Configure Build Path jdk1.8 

配置后:

3. className 定位。根据class类查找元素,一般得到的是多个元素(除非className唯一),如果得到多个,我们需要用一个List集合接收返回值

//根据className属性进行定位
List<WebElement> listElement = androidDriver.findElements(By.className(("className")));
listElement.get(1).click();

4. xpath 定位

//根据xpath属性进行定位
androidDriver.findElement(By.xpath("//android.widget.Button[@text='登录']")).click();
androidDriver.findElement(By.xpath("//android.widget.Button[@text=\"登录\"]")).click();

5. accessibility id 元素定位 

//根据AccessibilityId进行定位
androidDriver.findElementByAccessibilityId("登录").click();

6. 坐标定位:通过开发者选项>>指针位置或者是UIAutomatorView获取到的位置(绝对坐标)

//坐标定位
//通过TouchAction类完成坐标点击
TouchAction touchAction = new TouchAction<>(androidDriver);
//将(x,y)坐标封装成PointOption对象传入tap方法调用
PointOption pointOption =PointOption.point(500, 500);
//通过调用tap方法进行点击.调用perform()方法执行点击
touchAction.tap(pointOption).perform();

7. UIAutomator 定位,安卓的UIAutomator是一个强有力的元素定位方式,它是通过Android提供的原生的UIAutomator框架去找元素,且支持元素全部属性定位,appium提供的定位元素API其实都是UIAutomator提供的API,appium元素定位方式如下:

//通过UIAutomator 的description 方法找到属性是 content-desc 的元素
androidDriver.findElementByAndroidUIAutomator("new UiSelector().description(\"登录\")").click();
//通过UIAutomator的text 方法找到属性是text的元素
androidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"登录\")").click();
//通过UIAutomator的 resourceId方法找到属性是 resourceID的元素
androidDriver.findElementByAndroidUIAutomator("new UiSelectot().resourceId(\"resource-id\")").click();

8.未完待续.......

APP 自动化之appium元素定位(三)的更多相关文章

  1. Appium+Python自动化 4 -appium元素定位

    appium定位app上的元素方式 在前面文章中有介绍,(通过UIautomator工具查看app元素) appium定位app上的元素,可以通过id,name,class这些属性定位到 1.id 定 ...

  2. 4、通过uiautomatorviewer实现appium元素定位

    熟悉selenium自动化的小伙伴应该知道WebDriver 提供了八种元素定位方法: idnameclass nametag namelink textpartial link textxpathc ...

  3. appium自动化测试之UIautomatorviewer元素定位

    appium自动化测试之UIautomatorviewer元素定位 标签(空格分隔): uiautomatorviewer元素定位 前面的章节,已经总结了怎么搭建环境,怎样成功启动一个APP了,这里具 ...

  4. python+Appium自动化:各种元素定位方法

    name定位 driver.find_element_by_name('飞利浦净水').click() 测试结果报错:selenium.common.exceptions.InvalidSelecto ...

  5. Python+Appium自动化测试(5)-appium元素定位常用方法

    对于Android而言,查找appUI界面元素属性的工具有三种:appium desktop,uiautomatorviewer.bat,weditor.之前已经介绍过了weditor的使用,这里我将 ...

  6. appium元素定位总结

    appium元素定位方法总结 使用uiautomator定位 driver.find_element_by_android_uiautomator(uia_string) 根据resourceId属性 ...

  7. appium元素定位工具

      appium元素定位工具介绍 使用uiautomatorviewer定位工具 使用Appium Inspector定位工具 使用uiautomatorviewer定位工具 谷歌在Android S ...

  8. Python Appium 元素定位方法简单介绍

    Python  Appium  元素定位 常用的八种定位方法(与selenium通用) # id定位 driver.find_element_by_id() # name定位 driver.find_ ...

  9. python+Appium自动化:H5元素定位

    问题思考 在混合开发的App中,经常会有内嵌的H5页面.那么这些H5页面元素该如何进行定位操作呢? 解决思路 针对这种场景直接使用前面所讲的方法来进行定位是行不通的,因为前面的都是基于Andriod原 ...

随机推荐

  1. Linux系列(24) - chmod

    前言 在Unix和Linux的中,每个文件(文件夹也被看作是文件)都有三种权限:读.写.运行. 被授予权限的用户身份有三种:当前文件的拥有者,与拥有者属于同组者(同一个group),其他人 hello ...

  2. Charles抓包工具过滤网络请求

    Charles是一个HTTP代理服务器,HTTP监视器,反转代理服务器,当浏览器连接Charles的代理访问互联网时,Charles可以监控浏览器发送和接收的所有数据.它允许一个开发者查看所有连接互联 ...

  3. P3235-[HNOI2014]江南乐【整除分块,SG函数】

    正题 题目链接:https://www.luogu.com.cn/problem/P3235 题目大意 \(T\)组游戏,固定给出\(F\).每组游戏有\(n\)个石头,每次操作的人可以选择一个数量不 ...

  4. WPF进阶技巧和实战02-布局

    窗体 无边框 窗体无边框(最大化及标题位置)WindowStyle="None" 窗体透明 AllowsTransparency="True",必须设置窗体无边 ...

  5. Android 开发进程 0.35 升级编译版本Android12

    Android12升级 工作需要升级到编译版本31 在这里记录一下遇到的问题. 错误:Manifest merger failedManifest merger failed 这个问题通常搜到的答案是 ...

  6. 10.10 Rewrite 实战

    将baidu.com跳转到www.baidu.com server { listen 80; server_name baidu.com; rewrite ^/ http://www.baidu.co ...

  7. 【日志技术专题】「logback入门到精通」彻彻底底带你学会logback框架的使用和原理(入门介绍篇)

    技术介绍 什么是 logback? Logback为取代 log4j 而生.Logback 由 log4j 的创立者 Ceki Gülcü设计.以十多年设计工业级记录系统的经验为基础,所创建的logb ...

  8. MySQL复习(二)MySQL基本数据类型

    MySQL基本数据类型 常用的字段类型大致可以分为数值类型.字符串类型.日期时间类型三大类 1. 数值类型 数值类型可以分为整型.浮点型.定点型三小类. 1.1 整型 (tiny:极小的, small ...

  9. UltraSoft - Beta - Scrum Meeting 11

    Date: May 27th, 2020. Scrum 情况汇报 进度情况 组员 负责 今日进度 q2l PM.后端 记录会议 Liuzh 前端 增加了对重复日程的支持 Kkkk 前端 测试验证前后端 ...

  10. Vue 报错Error in render: “TypeError: Cannot read properties of null (reading ‘xxx’)” found in

    前端vue报错 [Vue warn]: Error in render: "TypeError: Cannot read properties of null (reading 'name' ...