appium笔记-python
driver
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
获取屏幕分辨率
size = driver.get_window_size()
width = size['width']
height = size['height']
截图
driver.get_screenshot_as_file(pic_path)
滑动屏幕
start_x
,start_y
,end_x
,end_y
TouchAction(driver).press(x=start_x, y=start_y).move_to(x=end_x, y=end_y).release().perform()
driver.swipe(start_x, start_y, end_x, end_y, 400) #最后的数字越大,滑动步数越多,滑动越慢
点击坐标
click_x
,click_y
TouchAction(driver).tap(x=click_x, y=click_y).perform()
其他
driver.quit()
driver.back()
driver.switch_to.alert.accept()
查找控件
find_elements返回类型list,没有找到时返回[]
基本
driver.find_elements_by_accessibility_id(content-desc)
driver.find_elements_by_id(resource-id)
driver.find_elements_by_xpath(xpath)
driver.find_elements_by_android_uiautomator(UiSelector)
xpath
//class//class[contains(@index,index)]
//class//class[contains(@text,text)]
//class//class[contains(@content-desc,content-desc)]
//class//class[contains(@resource-id,resource-id)]
//class[contains(@content-desc, content-desc)]/following-sibling::class
//class[contains(@content-desc, content-desc)]/preceding-sibling::class
//class[contains(@content-desc, content-desc)]/parent::class
//class[contains(@content-desc, content-desc)]/child::class
_by_xpath('//xxxx//xxxxxx[x]')
_by_xpath('(//xxxxxx[])[x]')
uiautomator
driver.find_elements_by_android_uiautomator("new UiSelector().text(\"" + text + "\")")
driver.find_elements_by_android_uiautomator(
"new UiSelector().resourceId(\"xxxxxxx\").text(\"xxxxx\")")
driver.find_elements_by_android_uiautomator("new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().text(\""+i+"\"))")
获取控件属性
ele.get_attribute("contentDescription")
ele.get_attribute("text")
""可为 checkable clickable resourceId className 等
获取控件边界大小
ele.rect
返回字典 {‘height’:xxxx,‘width’:xxxxx,‘x’:xxxx,‘y’:xxxxxx}
x y 左上角坐标
参考文档:
https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
http://cs.szpt.edu.cn/android/tools/help/uiautomator/UiSelector.html
https://www.cnblogs.com/fenqing3108/p/5740266.html
https://testerhome.com/topics/node23
http://www.testclass.net/appium/
http://www.runoob.com/python/python-tutorial.html
https://www.jianshu.com/p/2200c278cec9
iOS
driver.find_elements_by_name(tabname)
driver.find_elements_by_ios_predicate("name == '"+str(tmpname)+"'")
xpath_exchange = '//XCUIElementTypeButton[contains(@name,"月")]/preceding-sibling::XCUIElementTypeButton[1]'
ele = driver.find_elements_by_xpath(xpath_exchange)
type类似于安卓的className
name内容,大概类似于text或者contentDescription
获取属性
ele.get_attribute("name")
滑动、点击
driver.execute_script("mobile: dragFromToForDuration",{"element": None, "fromX": start_x, "fromY": start_y,"toX": end_x, "toY": end_y,"duration": 1})
driver.execute_script("mobile: tap", {"x": tap_x, "y": tap_y})
up
down
right
left
driver.execute_script("mobile: scroll", {"direction":'up'})
driver.execute_script("mobile: scroll", [{"direction":'down', 'element': ele_object}])
使用swipe、tap等函数报错
获取iOS设备的信息
instruments -s devices
返回:
deviceName (platformVersion) [udid]