一、元素定位
xpath, id, class name, accessibility id, css selector, -android uiautomator
1、根据元素的resource-id来查找元素
webelement_object = driver.find_element(MobileBy.ID, 定位表达式)
2、xpath定位
webelement_object = driver.find_element(MobileBy.XPATH, 定位表达式)
//*[@content-desc="转到上一层级"]
4、accessibility id定位 -- content-desc属性
favirate_desc_loc = (MobileBy.ACCESSIBILITY_ID, '收藏')
5、-android uiautomator
安卓系统中uiautomator自动化测试框架中,提供的元素定位类UiSelector里提供的方法。
UiSelector类是java语言实现的。
官网文章:http://appium.io/docs/en/writing-running-appium/android/uiautomator-uiselector/
description(String desc) content-desc属性
特别注意:在java语言当中,字符串是双引号。
用法:
java当中字符串是双引号。
uiautomator_loc = (MobileBy.ANDROID_UIAUTOMATOR, 'text("MySQL数据库")')
java当中字符串是双引号 - 通过id和content-desc2个属性同时定位
uiautomator_loc = (MobileBy.ANDROID_UIAUTOMATOR,'resourceId("com.lemon.lemonban:id/action_favourite").description("收藏")')
3、class name定位 - 元素类型
webelement_object = driver.find_element(MobileBy.CLASS_NAME, 定位表达式)
二、WebElement对象的基本操作:
1、点击 click()
2、输入 send_keys()
3、获取属性 get_attribute()
4、获取文本 text
三、web自动化的等待方式 - app全面通用:
sleep
implicity_wait
WebDriverWait + expected_condition
写个小demo
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_caps = {
"platformName": "Android",
"platformVersion": "7.1.2",
"deviceName": "emulator-5554",
"appPackage": "com.lemon.lemonban",
"appActivity": "com.lemon.lemonban.activity.WelcomeActivity",
"noReset": True,
"noSign": True,
}
# 跟appium之间建立会话
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
wait = WebDriverWait(driver,20)
# 等待要操作的元素可见
# 题库元素
id_loc = (MobileBy.ID,"com.lemon.lemonban:id/navigation_tiku")
# # xpath
# xpath_loc = (MobileBy.XPATH, '//*[@resource-id="com.lemon.lemonban:id/navigation_tiku"]')
wait.until(EC.visibility_of_element_located(id_loc))
# id - 点击题库
ele = driver.find_element(*id_loc)
ele.click()
# 选择题库类型、题库难度
xpath_loc = (MobileBy.XPATH, '//*[@text="MySQL数据库"]')
ele = wait.until(EC.visibility_of_element_located(xpath_loc))
ele.click()
# 难度级别-中级
id_loc = (MobileBy.ID, 'com.lemon.lemonban:id/second_level')
ele = wait.until(EC.visibility_of_element_located(id_loc))
ele.click()
# 选择题库套题 -- 元素定位写错了。
suite_loc = (MobileBy.XPATH, 'com.lemon.lemonban.activity.SuitSubjectActivity')
ele = wait.until(EC.visibility_of_element_located(suite_loc))
ele.click()
# 点击收藏按钮
# java当中字符串是双引号 - 通过id和content-desc2个属性同时定位
uiautomator_loc = (MobileBy.ANDROID_UIAUTOMATOR,
'resourceId("com.lemon.lemonban:id/action_favourite").description("收藏")')
ele = wait.until(EC.visibility_of_element_located(uiautomator_loc))
ele.click()
# 转到上一级
content_desc_loc = (MobileBy.ACCESSIBILITY_ID, '转到上一层级')
ele = wait.until(EC.visibility_of_element_located(content_desc_loc))
ele.click()