zoukankan      html  css  js  c++  java
  • Appium的使用笔记(二)(基本操作)

    主要内容是app的启动,关闭,安装卸载,置于后台,获取元素文本内容,获取元素位置和大小和属性值,滑动屏幕,拖拽,长按和抬起,分辨率和截图,获取和设置手机网络,通知栏操作

    1.在脚本内启动其他app

    1 #app_package是要打开的包名
    2 #app_activity是要打开的进程名
    3 driver.start_activity(app_package,app_activity)
    

    附:在脚本内再次调起desires_cap中的app的话可以使用driver.launch_app()   (括号中不必再添加参数了)

    2.用appium获取包名和进程名

    1 #一般用于打开一个程序后,输出所显示的应用包名和进程名
    2 print(driver.current_package)
    3 print(driver.current_acivity)

    3.如果需要关闭某个应用程序后,再打开新的程序,那么关闭的方法

    1 #关闭当前操作的app,不会关闭驱动对象(驱动对象就是在前置代码后设置的driver之类的)
    2 driver.close_app()
    3 #关闭驱动对象,同时关闭所有关联的app
    4 driver.quit()

    4.用appium安装和卸载app

     1 """安装app"""
     2 #app_path是apk的路径
     3 driver.install_app(app_path)
     4 
     5 """卸载app"""
     6 #app_id是应用程序的包名
     7 driver.remove_app(app_id)
     8 
     9 """判断app是否安装"""
    10 #返回值为布尔类型
    11 driver.is_app_installed(app_id)

    5.将应用置于后台(模拟home键+进入app)

    有些app在进入后台一段时间后会需要重新登陆/输入验证码/输密码之类的,这个方法可以自动回到前台

    1 #app放置到后台一定时间后再回到前台
    2 #second是在后台停留的秒数
    3 driver.background_app(second)

    6.获取元素的文本内容

    1 """用于获取按钮、文本框、输入框等控件的文本内容"""
    2 #例如打开“设置”并且获取所有resource-id为“com.android.settings:id/title的元素,并打印其文字内容”
    3 titles = driver.find_elements_by_id("com.android.settings:id/title")
    4 for title in titles:
    5     print(title.text)

    7.获取元素的位置和大小

    1 """用于获取元素的位置和大小"""
    2 
    3 #返回值:字典,(元素的x坐标,元素的y坐标)
    4 element.location
    5 
    6 #返回值:字典,(width为宽度,hight为高度)
    7 element.size

    8.获取元素的属性值

     1 """根据特征定位到元素后,使用元素的属性名获得对应的属性值"""
     2 
     3 #例如获取所有“设置”里resource-id为com.android.settings:id/title的元素,并使用get_attribute获取这些元素的enable,text,content-desc,resource-id,class的属性值
     4 
     5 titles = driver.find_elements_by_id("com.android.settings:id/title")
     6 for title in titles:
     7     print(title.get_attribute("enabled"))
     8     print(title.get_attribute("text"))
     9     print(title.get_attribute("name"))
    10     print(title.get_attribute("resourceId"))
    11     print(title.get_attribute("ClassName"))
    12 
    13 #value = "name"返回的是content-desc/text属性值
    14 #value = "ClassName"返回的是class属性值,API >=18才能支持(Android API level详情可百度,大于18就是指安卓版本要4.3以上)
    15 #value = "resourceId"返回的是resourceid属性值,API >18=才能支持

    9.Swipe滑动屏幕

    1 """从一个坐标位置滑动到另一个坐标位置(两个点之间的)"""
    2 
    3 #duration是指滑动这个操作一共持续的时间长度,单位是ms
    4 
    5 driver.swipe(start_x,start_y,end_x,end_y,duration)

    10.scroll滑动

    1 """从一个元素滑动到另一个元素,直到页面自动停止(不能设置持续时间,滑动靠惯性)"""
    2 
    3 #例如从“存储”滑动到“更多”
    4 
    5 save_button = driver.find_element_by_xpath("//*[@text='存储']")
    6 more_button = driver.find_element_by_xpath("//*[@text='更多']")
    7 driver.scroll(save_button,more_button)

    11.拖拽事件

    1 """从一个元素滑动到另一个元素,第二个元素代替第一个元素原本屏幕上的位置"""
    2 
    3 #将“存储”拖拽到“更多”
    4 
    5 save_button = driver.find_element_by_xpath("//*[@text='存储']")
    6 more_button = driver.find_element_by_xpath("//*[@text='更多']")
    7 driver.drag_and_drop(save_button,more_button)

    接下来是一些手势操作,需要导入touchaction模块,执行perform()函数

    12.手指轻点

     1 #模拟手指对元素或坐标的轻敲操作
     2 #代码为TouchAction(driver).tap(element,x,y).perform()
     3 
     4 #例如打开设置,点击“WLAN”
     5 
     6 #先导入模块
     7 from appium.webdriver.common.touch_action import TouchAction
     8 
     9 ele = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
    10 TouchAction(driver).tap(el).perform()

    13.长按操作

     1 """有3种方法"""
     2 """第一种:按下,sleep等待,再release释放"""
     3 TouchAction(driver).press(x=123,y=123).perform()
     4 time.sleep(2)
     5 TouchAction(driver).press(x=123,y=123).release().perform()
     6 
     7 """第二种方法:按下,暂停等待2s也就是2000ms。再释放"""
     8 TouchAction(driver).press(x=123,y=123).perform()
     9 time.sleep(2)
    10 TouchAction(driver).press(x=123,y=123).wait(2000).release().perform()
    11 
    12 """长按"""
    13 TouchAction(driver).press(x=123,y=123).perform()
    14 time.sleep(2)
    15 TouchAction(driver).long_press(x=123,y=123,duration=2000).release().perform()

    14.移动手指的操作(如下图的屏幕图形解锁)

    1 #首先用UIautomatorviewer获得各点的坐标(最大化窗口,红色叉号旁边有点的坐标)
    2 
    3 TouchAction(driver).press(x=xxx,y=xxx).move_to(x=xxx,y=xxx).move_to(...)

    15.分辨率和截图

    1 #获取手机分辨率
    2 driver.get_window_size()
    3 
    4 #截图当前页面,保存到指定路径,命名为指定格式,filename为完整路径,包括路径及文件名
    5 driver.get_screenshot_as_file(filename)

    16.获取和设置手机网络

    1 #获取手机网络
    2 driver.network_connection
    3 
    4 #设置手机网络
    5 driver.set_network_connection(connectionType)

     比如要设置成飞行模式,那么connectionType = 1即可

    17.发送关键代码

    按键对应的编码可参考 https://www.cnblogs.com/sishuiliuyun/p/4432884.html

    1 #代码形式为
    2 driver.press_keycode(号码)

    18.通知栏的额操作

    1 """appium官方并未为我们提供关闭通知的api,所以现实中如何清除/关闭通知,就做如何操作即可"""
    2 #打开手机通知栏
    3 driver.open_notifications()

     19.发送一个按键事件,有两种方式

    1 driver.press_keycode(3)
    2 
    3 driver.keyevent(3)
    

    20.锁住屏幕和模拟摇晃设备

     1 driver.lock(5)
     2 
     3 driver.shake()
    

    21.缩小屏幕和放大屏幕(双指往内移动缩小,双指往外移动放大)

    1 #缩小屏幕
    2 driver.pinch(element=el)
    3 
    4 #放大屏幕
    5 driver.zoom(element=el)

    22.在设备上推入和拉出文件

    1 #向设备推入文件
    2 driver.pull_file("文件位置")
    3 
    4 #把文件从设备中拉出来
    5 driver.push_file("文件位置")

    --end--

  • 相关阅读:
    MySQL之数据的备份与还原
    调用、查看、修改、删除存储过程和函数
    变量的使用、游标的使用、流程控制的使用
    存储过程与函数
    elementUI 表格设置表头样式
    oracle先排序再分页
    postgresql行转列
    crosstab(unknown, unknown) does not exist
    sublime安装php_beautifier来格式化PHP代码
    从今天开始我要经常更新博客
  • 原文地址:https://www.cnblogs.com/RuiRuia/p/12206874.html
Copyright © 2011-2022 走看看