android-sdk-windows\tools\uiautomatorviewer.bat
双击即可启动,启动过程中组件所打开的CMD
窗口不用关闭;手机开启USB调试,或打开模拟器即可使用Appium
的时候,需要填写Desired Capabilities
,这里使用Android SDK
自带的uiautomatorviewer.bat
控件可以便捷的获取所需信息通过截屏并分析XML布局文件的方式,为用户提供控件信息查看服务
The first button:Open:Screenshot;UI XML Dump
打开本地截图和打开.uix格式文件(uix文件需安装Binary Data
打开)
Second button:Device Screenshot(uiautomator dump)设备屏幕截图(uiautomator转储)
Third button:Device Screenshot with Compressed Hierarchy(uiautomator dump --compressed)具有压缩层次结构的设备屏幕截图
Save:保存至本地指定位置
在Android Studio中可以通过Android Device Monitor 调用UI Automator Viewer
First button on the right:+
Expand All-全部展开
Second button on the right:Toggle NAF Nodes-切换NAF节点,点击后展示不可被识别的控件
Third button on the right:Clear search results-清除搜索结果
通过调用android uiautomator使用className进行定位
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")')
ele.send_keys('132')
通过className正则匹配进行定位
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches (".*EditText")')
ele.send_keys('132')
textMatches故名思义就是通过正则的来进行查找定位,他也是通过text的属性来进行正则匹配
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^请输入手.*")')
ele.send_keys("123")
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("请输入")')
driver.find_element_by_class_name("com.jingdong.app.mall:id/ic").click()
com.jingdong.app.mall:id/ic
;driver.find_elements_by_class_name("com.jingdong.app.mall:id/ic")[1].click()
,或WebElement button = driver.findElement(By.className(“com.jingdong.app.mall:id/ic”));
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").childSelector(text("密码"))')
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").fromParent(text("密码"))')
self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找的元素文本").instance(0));')
参数取content-desc的值:driver.find_element_by_accessibility_id('Accessibility').click()
或findElement(By.AccessibilityId("sharebutton"))
driver.find_element_by_xpath('//*[@text="Accessibility"]').click()
driver.find_element_by_xpath('//*[@resource-id="android:id/text1"]').click()
com.jingdong.app.mall:id/ic
driver.findElement(By.xpath("//com.jingdong.app.mall:id/ic"))
driver.find_element_by_xpath('//*[@resource-id="android:id/text1"]').click()
agree_continue_xpath = "//*[@text='退出登录']"
WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
用于获取toast时,toast文本内容较长,可采用contains
包含部分文本的匹配方式;以用来模糊匹配上面的文本属性“退出登录”
agree_continue_xpath = "//android.widget.Button[contains(@text, '退出')]"
WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
AndroidUIAutomator是一个强有力的元素定位方式,它是通过Android UIAutomator类库去找元素,定位方式:findElement(By.AndroidUIAutomator(String UIAuto));
可以选择id,nameclassName,description作为传入的字符串
WebElement el =
driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.tencent.mm:id/do\")");
resourceld定位和appium封装好的id定位一样,差别在写法变成了uiautomator的写法
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/et_phone_edit")')
ele.send_keys('234')
通过id进行正则匹配定位
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".+et_phone_edit")')
ele.send_keys('123')
agree_continue_xpath = "//*[@class='android.widget.TextView' and @text='退出登录']"
WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
android uiautomator原理是通过android 自带的android uiautomator的类库去查找元素,其实和appium的定位一样,或者说他比appium的定位方式更佳多以及更佳适用,它也支持id、className、text、模糊匹配等进行定位
1.driver.find_element_by_xpath("//android.widget.ListView/android.widget.TextView").click()
2.driver.find_element_by_xpath("//android.widget.ListView/*[1]").click()
3.driver.find_element_by_xpath('//*[@resource-id="android:id/list"]/*[1]').click()
在父元素API Demos下,Accessibility是第1个子元素;xpath的索引从1开始
4.driver.find_element_by_xpath('//*[@resource-id="android:id/list"]/*[@resource-id="android:id/text1"]').click()
5.driver.find_element_by_xpath('//android.widget.TextView[contains(@text,"Acce")]').click()
//class[@属性=“属性值”]/class[]/class
driver.find_element_by_xpath('//android.widget.TextView[contains(@text,"Acce")]').click()
①(参数取resource-id的值)driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")').click()
简化(默认是UiSelector对象):driver.find_element_by_android_uiautomator('.resourceId("android:id/text1")').click()
例如:WebElement element = driver.findElement(By.id("com.tencent.mm:id/do"));
或driver.findElementById("com.tencent.mm:id/do")
②(参数取class的值)driver.find_element_by_android_uiautomator('.className("android.widget.TextView")').click()
③(参数取text的值)driver.find_element_by_android_uiautomator('.text("Accessibility")').click()
appium是app端,用appium需导出selenium包
selenium是web端
frame | Element positioning method | remarks |
---|---|---|
appium | id, className, AccessibilityId, xpath, AndroidUIAutomator | for H5,Same support selenium name,link,text,css |
selenium | id, className, name, tag name, link text, paratial link text, xpath ,css |