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. 大前端快闪:package.json文件知多少?

    最近在公司某项目参与了一些前端工作,作为后端抠脚大汉,改点前端细节磕磕绊绊,改点大前端.工程化.HTTP交互倒也还能做到柳暗花明. 于是打算用后端程序猿的视角记录一些{大前端}的知识快闪,也算是帮助读 ...

  2. 一篇文章搞定Selenium元素定位/封装/数据驱动

    小伙伴都知道,自动化最重的,又最"难"(因为实战中会碰到定位的各种坑)那就是定位元素.如果不熟练掌握定位,那只怕你比功能测式的小伙伴下班还会要晚!扎心了吧! Selenium常用定 ...

  3. 服务器使用matplotlib绘图

    Two points: 1. change backend of matplotlib: import matplotlib.pyplot as plt plt.switch_backend('agg ...

  4. 势流理论笔记:03 Hess-Smith积分方法

    书接上回势流理论笔记:02 直接法与间接法 Hess-Smith方法 采用面向对象编程的思路,\(Matlab\)程序脚本,实现以下功能: 输入面元(四边形面元顶点坐标) 输出系数矩阵\([H][M] ...

  5. 透过 Chrome 深入理解浏览器导航过程

    网络的导航,是从输入 url 到最终获取到文件的过程.其中牵扯到浏览器架构.操作系统.网络等一系列知识.本文将从各个角度详细论述这一过程,涉及广度与深度.如果您是已经有一定基础的同学,那么本文可以快速 ...

  6. HC(Histogram-based Contrast) 基于直方图对比度的显著性

    HC(Histogram-based Contrast) 基于直方图对比度的显著性 来源于: 2011, Global contrast based salient region detection, ...

  7. CF992E Nastya and King-Shamans(线段树二分+思维)

    这是一道卡常好题 从160s卡到36s qwq 由于题目设计到原数组的单点修改,那么就对应着前缀和数组上的区间加. 很显然能想到用线段树来维护这么个东西. 那么该如果求题目要求的位置呢 我们来看这个题 ...

  8. 2020.5.16-ICPC Central Europe Regional Contest 2019

    A. ABB #include <bits/stdc++.h> using namespace std; #define PB push_back #define ZERO (1e-10) ...

  9. Oracle中常用的to_char用法详解

    Oracle函数to_char转化数字型指定小数点位数的用法 to_char,函数功能,就是将数值型或者日期型转化为字符型. 比如最简单的应用: -- 1.0123=>1.0123 SELECT ...

  10. mysql order by语句流程是怎么样的

    order by流程是怎么样的 注意点: select id, name,age,city from t1 where city='杭州' order by age limit 1000; order ...