appium常用API

编程入门 行业动态 更新时间:2024-10-07 04:35:56

appium<a href=https://www.elefans.com/category/jswz/34/1769776.html style=常用API"/>

appium常用API

1.跳转app

场景:从一个app跳转到另一个app,比如美团支付订单

sleep(4)
driver.start_activity("包名","界面名")

包名查看

adb shell dumpsys window | grep mCurrentFocus
adb shell dumpsys window | findstr mCurrentFocus

2.获取app的包名和界面名

print(f"包名:'{driver.current_package}'")         
print(f"界面名:'{driver.current_activity}'")       

3.关闭app和驱动对象

driver.close_app()
driver.quit()     

4.app安装卸载

# 1.0 安装app
driver.install_app(r"app路径")    
# 1.1 验证已安装
if driver.is_app_installed("com.tal.kaoyan"):print("已经安装好了!")
else:print("已经卸载了!")
# 1.2 关闭当前app
sleep(2)
driver.close_app()
# 1.3 卸载app
sleep(2)
driver.remove_app("com.tal.kaoyan")
# 1.4 验证已卸载
sleep(2)
if driver.is_app_installed("com.tal.kaoyan"):print("已经安装好了!")
else:print("已经卸载了!")

5.应用放在后台

**场景:**应用放在后台,再进入前台,模拟热启动,eg:银行app从后台切到前台需要输入密码,自动化需模拟这种情况

# 表示进入后台5s,之后返回到app的最后一个页面
driver.background_app(5) 

6.文本模糊定位

该控件有文本属性,比如:命令按钮文本为"跳过"。

driver.find_element(AppiumBy.XPATH, '//*[contains(@text,"跳过")]').click()

7 锁屏图案

from appium.webdrivermon.touch_action import TouchAction
# 做图点的坐标
x1 = 116;y1 = 285
x2 = 411;y2 = y1
x3 = x2;y3 = 582
x4 = x1;y4 = y3
# 连续操作两次
for i in range(2):TouchAction(driver).press(x=x1, y=y1).wait(1000).\move_to(x=x2, y=y2).wait(1000).\move_to(x=x3, y=y3).wait(1000).\move_to(x=x4, y=y4).wait(1000).\release().perform()sleep(2)

8 用元素来滑动

start = driver.find_element(AppiumBy.XPATH, '//*[@text="图表浏览"]')
end = driver.find_element(AppiumBy.XPATH, '//*[@text="账本自定义"]')
# 惯性滑动
driver.scroll(start, end)

9 屏幕缩放

# 百度地图缩放size = driver.get_window_size()
width = size['width']
height = size['height']
x1 = 0.4 * width;y1 = 0.4 * height
x2 = 0.1 * width;y2 = 0.1 * heightx3 = 0.6 * width;y3 = 0.6 * height
x4 = 0.9 * width;y4 = 0.9 * heighta1 = TouchAction(driver).press(x=x1, y=y1).wait(1000).move_to(x=x2, y=y2).release()
a2 = TouchAction(driver).press(x=x3, y=y3).wait(1000).move_to(x=x4, y=y4).release()
# 两个touchaction合在一起
m1 = MultiAction(driver)
m1.add(a1,a2)
m1.perform()
sleep(2)
class AppOperation(object):@staticmethoddef swipe_way(driver_object, swipe_direction):def simple_swipe(x1_coe, y1_coe, x2_coe, y2_coe):size1 = driver_object.get_window_size()width1 = size1.get("width")height1 = size1.get("height")x1 = width1 * x1_coey1 = height1 * y1_coex2 = width1 * x2_coey2 = height1 * y2_coedriver_object.swipe(x1, y1, x2, y2)if swipe_direction == "right":simple_swipe(0.1, 0.5, 0.9, 0.5)if swipe_direction == "left":simple_swipe(0.9, 0.5, 0.1, 0.5)

10.输入框

输入

element.send_keys("内容")

清空

element.clear()

默认输入中文无效,但不会报错,需添加一下参数

desired_caps["unicodeKeyboard"] = True
desired_caps["resetKeyboard"] = True

11.获取元素文本内容

element.text
# 示例应用
titles = driver.find_elements_by_id("...")
for i in titles:print(i.text)

12.获取元素位置和大小

element.location
element.size

13.获取元素属性值

element.get_attribute("")

14.swipe滑动

driver.swipe(start_x,end_x,duration=None)

持续时间长,惯性小

duration = ##,滑动持续的时间长度,单位:ms

15.滑动方式选择

scorll(起点元素,终点元素)无法设置持续时间,惯性大
drag_and_drop(起点元素,终点元素)无法设置持续时间,无惯性
swipe(起点坐标,终点坐标,持续时间)持续时间长,惯性小持续时间短,惯性大

16.手势操作

(1)创建TouchAction对象

(2)通过对象调用手势

(3)perform()执行

a.手指轻敲(按下并快速抬起)

TouchAction(driver).tap(element,x,y).perform()

b.按下和抬起

TouchAction(driver).press(element,x,y).perform()
TouchAction(driver).release(element,x,y).perform()
# 元素定位或坐标定位,选择一种即可,坐标写法(x=100,y=100)

c.长按

TouchAction(driver).wait(ms=0).perform()
TouchAction(driver).long_press(element,x,y,duration=1000).perform()

d.移动

TouchAction(driver).move_to(element,x,y).perform()

17.获取手机分辨率

driver.get_window_size()
# 返回{"height":800,"width":450}

18.截图

driver.get_screenshot_as_file(os.getcwd()+os.sep+"./screen.png")
import os
print(os.getcwd())
print(os.sep)
print(os.getcwd() + r"\screen.png")

19.获取和设置手机网络

driverwork_connection
driver.set_network_connection(connectionType)+--------------------+------+------+---------------+| Value (Alias)      | Data | Wifi | Airplane Mode |+====================+======+======+===============+| 0 (None)           | 0    | 0    | 0             |+--------------------+------+------+---------------+| 1 (Airplane Mode)  | 0    | 0    | 1             |+--------------------+------+------+---------------+| 2 (Wifi only)      | 0    | 1    | 0             |+--------------------+------+------+---------------+| 4 (Data only)      | 1    | 0    | 0             |+--------------------+------+------+---------------+| 6 (All network on) | 1    | 1    | 0             |+--------------------+------+------+---------------+

20.发送"键"到设备

driver.press_key(keycode,metastate=None)
# keycode 百度
# metastate 一般不改

21.操作通知栏

更多推荐

appium常用API

本文发布于:2024-03-06 21:13:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1716392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常用   appium   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!