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. CSS写一个缺角的div和菱形

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. sonar扫面代码总体流程

  3. jquery中请求格式

    $.ajax({ url:"/ceshi/", type:"get", cache:false, dataType:"json", data ...

  4. NetCore5实现https请求

    前言 本文主要介绍在NetCore5中,实现证书加载和https访问请求. 证书准备 首先我们先创建一个自定义的证书Kiba518.pfx. 证书创建参考:最通俗易懂的RSA加密解密指导. 然后将证书 ...

  5. P5825-排列计数【EGF,NTT】

    正题 题目链接:https://www.luogu.com.cn/problem/P5825 题目大意 对于每个\(k\),求有多少个长度为\(n\)的排列有\(k\)个位置上升. \(1\leq n ...

  6. Spring系列之Redis的两种集成方式

    在工作中,我们用到分布式缓存的时候,第一选择就是Redis,今天介绍一下SpringBoot如何集成Redis的,分别使用Jedis和Spring-data-redis两种方式. 一.使用Jedis方 ...

  7. 【原创】SystemVerilog中的typedef前置声明方式

    SystemVerilog中,为了是代码简洁.易记,允许用户根据个人需要使用typedef自定义数据类型名,常用的使用方法可参见"define和typedef区别".但是在Syst ...

  8. C#开发BIMFACE系列43 服务端API之图纸拆分

    BIMFACE二次开发系列目录     [已更新最新开发文章,点击查看详细] 在上一篇博客<C#开发BIMFACE系列42 服务端API之图纸对比>的最后留了一个问题,在常规业务场景下,一 ...

  9. Java初步学习——2021.10.10每日总结,第五周周日

    (1)今天做了什么: (2)明天准备做什么? (3)遇到的问题,如何解决? 今天继续学习菜鸟教程java字符串实例 5.字符串反转--reverse方法 public class Main { pub ...

  10. SpringBoot入门05-全局配置文件

    springboot全局配置文件作用是设置或修改默认设置 springboot全局配置文件有下面两种方式 application.xml配置文件 示例 server.port=8088 server. ...