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. python学习2-博客-蓝图

    #!/usr/bin/env python # -*- coding: UTF-8 -*- from flask import Blueprint,Flask #这里创建了一个名称为 'admin' ...

  2. vue 主次页面区分

    1.路由设定,增加meta参数 { path: '/', name: 'Home', component: Home, meta: { index: 0, showFooter: true //由这个 ...

  3. [转载]用redis实现跨服务器session

    地址:http://blog.chinaunix.net/uid-11121450-id-3284875.html 这个月我们新开发了一个项目,由于使用到了4台机器做web,使用dns做负载均衡, 上 ...

  4. 鸿蒙内核源码分析(调度队列篇) | 内核有多少个调度队列 | 百篇博客分析OpenHarmony源码 | v6.05

    百篇博客系列篇.本篇为: v06.xx 鸿蒙内核源码分析(调度队列篇) | 内核有多少个调度队列 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调度 ...

  5. Redis之品鉴之旅(七)

    分布式锁 1)阻塞锁: 尝试在redis中创建一个字符串结构缓存,方法传入的key,value为锁的过期时间timeout的时间戳. 若redis中没有这个key,则创建成功(即抢到锁),然后立即返回 ...

  6. Qt和JavaScript使用QWebChannel交互一——和Qt内嵌网页交互

    Qt和JavaScript使用QWebChannel交互一--和Qt内嵌网页交互 目录 Qt和JavaScript使用QWebChannel交互一--和Qt内嵌网页交互 前言 一.效果 二.实现过程 ...

  7. Django整理(五) - 请求与响应 - request对象

    请求对象 一.客户端传参的几种方式 1. 通过URL路径(path)传递,例如:http://127.0.0.1:8000/news/1/2,两个参数:id和page 2. 通过 query stri ...

  8. 详解build-gradle文件

    目录 gradle 两个build.gradle文件 最外层目录下的build.gradle文件 jcenter dependencies闭包 app目录下的build.gradle文件 com.an ...

  9. C 函数指针 函数指针数组 转移表

    内容来自<c和指针>,整理后方便个人理解 高级声明 cdel程序可以方便的给出声明的释义 指向函数的指针 int ( *f ) ( int n_values, float amount ) ...

  10. Java(12)方法的重载

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201592.html 博客主页:https://www.cnblogs.com/testero ...