Appium自动化测试第四篇日志讲解与常见定位方式

本文详细解读了如何通过Appium启动Android应用,并介绍常见的定位元素方法,包括XPath、UIAutomator和CSS选择器。重点讲解了XPath的关键字使用和Uiautomator2的选择器实例。

1.1Appium日志分析

启动APPium服务:

这里配置的是雪球app:

配置如下:

{
  "platformName": "Android",
  "platformVersion": "10",
  "deviceName ": "ELE-AL00",
  "appPackage": "com.huawei.calculator",
  "appActivity": ".Calculator"
}

启动APPium服务器成功

[Appium] Welcome to Appium v1.17.1

Appium REST http interface listener started on 0.0.0.0:4723

post请求,数据是自己配置的其他的是默认的,将desiredCapabilities参数全部传递给服务器

[HTTP] --> POST /wd/hub/session
[HTTP] {"desiredCapabilities":{"appActivity":".view.WelcomeActivityAlias","appPackage":"com.xueqiu.android","deviceName ":"ELE-AL00","platformName":"Android","platformVersion":"10","newCommandTimeout":0,"connectHardwareKeyboard":true}}
[MJSONWP] Calling AppiumDriver.createSession() with args: [{"appActivity":".view.WelcomeActivityAlias","appPackage":"com.xueqiu.android","deviceName ":"ELE-AL00","platformName":"Android","platformVersion":"10","newCommandTimeout":0,"connectHardwareKeyboard":true},null,null]

[BaseDriver] Event 'newSessionRequested' logged at 1603543722539 (20:48:42 GMT+0800 (中国标准时间))

//创建session


[Appium] Appium v1.17.1 creating new AndroidUiautomator2Driver (v1.44.2) session
[BaseDriver] Creating session with MJSONWP desired capabilities: {
[BaseDriver]   "appActivity": ".view.WelcomeActivityAlias",
[BaseDriver]   "appPackage": "com.xueqiu.android",
[BaseDriver]   "deviceName ": "ELE-AL00",
[BaseDriver]   "platformName": "Android",
[BaseDriver]   "platformVersion": "10",
[BaseDriver]   "newCommandTimeout": 0,
[BaseDriver]   "connectHardwareKeyboard": true
[BaseDriver] }

//生成sessionid

[BaseDriver] Session created with session id: 1ce5e6a4-e24c-4bb6-b19c-e0c86afcd5f9

检查查看adb

[ADB] Using 'adb.exe' from 'C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe'


//通过adb devices查看当前连接的设备

[AndroidDriver] Retrieving device list

[ADB] Trying to find a connected android device

[ADB] Getting connected devices...

[ADB] Connected devices: [{"udid":"8KE5T19711002800","state":"device"}]

[AndroidDriver] Looking for a device with Android '10.0.0'

[ADB] Setting device id to 8KE5T19711002800

[ADB] Getting device platform version

之后会检测手机是否安装APPium Setting APP是否安装

检测手机端是否存在雪球app,之后会通过Bootstrap.jar来建立Appium服务器和手机端的通信,Bootstrap.jar负责运行测试命令。 

 

1.2常见定位方式:

  • Name属性 findElementByName("Text控件")//该方法已经取消
  • Id属性      findEle,mentById("resource-id属性")
  • ClassName属性   findElementByClassName("class属性")
  • AccessibilityId属性   findElementByAccessibilityId("content-desc属性")
  • 通过Xpath识别  findElement(By.xpath("路径"))

   xpath识别分为绝对路径和相对路径:

           绝对路径使用/ 如/html/body/div[1]/nav/div[1]/div[1]/div/a[2]

          相对路径使用//如://*[@id="app"]/nav/div[1]/div[1]/div/a[2]

这里讲解其他的Xpath使用方法:

使用contains关键字进行定位  driver.findElement(By.xpath("//android.widget.Button[contains(@text,'show progress')]"));这句话是寻找页面中text属性包含show progress这个单词的所有android.widget.Button元素 

使用start-with关键字进行定位driver.findElement(By.xpath("//android.widget.Button[start-with(@text,''show progress')]"))

使用text关键字进行定位driver.findElement(By.xpath("//*[text()=''show progress]"))

还有UIAutomator cssSelector  LinkText PartialLinkText 等这里主要是针对webapp测试的

Uiaotomator:
text文本定位:
new UiSelector().text("text文本")
new UiSelector().textContains("包含text文本")
new UiSelector().textStartsWith("以text文本开头")
new UiSelector().textMatches("正则表达式")

resourceId id定位
new UiSelector().resourceId("id")

class属性
new UiSelector().className("className")

contenet-des属性
new UiSelector().description("contenet-des属性")

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员路同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值