美文网首页
移动自动化--操作

移动自动化--操作

作者: xiaohan_zhang | 来源:发表于2020-05-15 15:42 被阅读0次
一、操作元素
  • 清空数据
    driver.find_element_by_id('kw').clear()
  • 输入数据
    driver.find_element_by_id('kw').send_keys('百家讲坛')
  • 点击
    driver.find_element_by_id('cn.cntv:id/history_image_button').click()
  • 上传文件
    driver.find_element_by_css_selector('#upfile').send_keys('文件路径')
二、获取元素信息
  • 获取元素的大小
    driver.find_element_by_id('cn.cntv:id/filler_text').size

  • 获取元素的文本
    driver.find_element_by_id('cn.cntv:id/filler_text').text

  • 获取元素在屏幕中的位置
    driver.find_element_by_id('cn.cntv:id/filler_text').location

  • 获取元素属性值
    driver.find_element_by_id('cn.cntv:id/filler_text').get_attribute('name')
    value='name' 返回content-desc / text属性值
    value='text' 返回text的属性值
    value='className' 返回 class属性值,只有 API=>18 才能支持
    value='resourceId' 返回 resource-id属性值,只有 API=>18 才能支持

  • 相对坐标计算
    screen_width = driver.get_window_size().['width'];
    screen_height = driver.get_window_size().['height'];
    绝对坐标转换为相对坐标,假设当前分辨率为720x1280,绝对坐标为(120, 200)
    xd_x = (120/720) * screen_width
    xd_y = (200/1280) * screen_height

  • 获取包名
    driver.current_package

  • 获取启动名
    driver.current_activity

三、事件操作
  • swip滑动事件
    从一个坐标位置滑动到另一个坐标位置,只能是两个点之间的滑动
    可能失败

方法:swipe(start_x, start_y, end_x, end_y, duration=None)
参数:
1.start_x:起点X轴坐标
2.start_y:起点Y轴坐标
3.end_x: 终点X轴坐标
4.end_y,: 终点Y轴坐标
5.duration: 滑动这个操作一共持续的时间长度,单位:ms

    screen_w = driver.get_window_size()['width']
    screen_h = driver.get_window_size()['height']
    driver.swipe(screen_w * 0.5, screen_h * 0.9, screen_w * 0.5, screen_h * 0.1, 5000)
  • scroll滑动事件
    从一个元素滑动到另一个元素,直到页面自动停止

方法:scroll(origin_el, destination_el)
参数:
1.origin_el:滑动开始的元素
2.destination_el:滑动结束的元素

eles = WebDriverWait(driver, 5, 0.5).until(lambda x: x.find_elements_by_id('cn.cntv:id/live_rank_content'))
driver.scroll(eles[2], eles[0])
  • drag拖拽事件
    从一个元素滑动到另一个元素,第二个元素替代第一个元素原本屏幕上的位置

方法:drag_and_drop(origin_el, destination_el)
参数:
1.origin_el:滑动开始的元素
2.destination_el:滑动结束的元素

  • 应用置于后台事件
    APP放置后台,模拟热启动

方法:background_app(seconds)
参数:
seconds:停留在后台的时间,单位:秒

⚠️注:滑动在iOS中可能会不好使,可以使用execute_script代替。

driver.execute_script('mobile:scroll', {'direction': 'down'}) #可能滑动两次
driver.execute_script('mobile:swipe', {'direction': 'up'})
四、模拟手势高级操作

TouchAction是AppiumDriver的辅助类,主要针对手势操作,比如滑动、长按、拖动等,
原理是将一系列的动作放在一个链条中发送到服务器,服务器接受到该链条后,解析各个动作,逐个执行。

  • 手指轻敲操作
    模拟手指轻敲一下屏幕操作
    方法:tap(element=None, x=None, y=None)
    方法:perform() # 发送命令到服务器执行操作
    参数:
    1.element:被定位到的元素
    2.x:相对于元素左上角的坐标,通常会使用元素的X轴坐标
    3.y:通常会使用元素的Y轴坐标
from appium.webdriver.common.touch_action import TouchAction

search_view = WebDriverWait(driver, 10, 0.5).until(lambda x: x.find_element_by_id('cn.cntv:id/filler_text'))
TouchAction(driver).tap(search_view).perform()

# 通过坐标方式敲击屏幕
# TouchAction(driver).tap(x=search_view.location["x"], y=search_view.location["x"]).perform()
  • 手指按操作
    模拟手指按下屏幕,按就要对应着离开.

方法: press(el=None, x=None, y=None)
方法:release() # 结束动作,手指离开屏幕
参数:
1.element:被定位到的元素
2.x:通常会使用元素的X轴坐标
3.y:通常会使用元素的Y轴坐标

el = WebDriverWait(driver, 10, 0.5).until(lambda x: x.find_element_by_id('cn.cntv:id/filler_text'))
TouchAction(driver).press(el).release().perform()

# 通过坐标方式按下屏幕
# TouchAction(driver).tap(x=el.location.get("x"), y=el.location.get("y")).release().perform()
  • 等待操作
    类似例如长按某个元素几秒

方法:wait(ms=0)
参数:
ms:暂停的毫秒数

el =driver.find_element_by_id("android:id/title")
# 通过元素定位方式长按元素
TouchAction(driver).press(el).wait(5000).perform()
#TouchAction(driver).press(x=el.location.get("x"), y=el.location.get("y")).wait(5000).release().perform() 
  • 手指长按操作
    模拟手机按下屏幕一段时间,按就要对应着离开.

方法:long_press(el=None, x=None, y=None, duration=1000)
参数:
1.element:被定位到的元素
2.x:通常会使用元素的X轴坐标
3.y:通常会使用元素的Y轴坐标
4.duration:持续时间,默认为1000ms

el =driver.find_element_by_id("android:id/title")
TouchAction(driver).long_press(el,duration=5000).release().perform()
# TouchAction(driver).long_press(x=el.location.get("x"), y=el.location.get("y")).perform()
  • 手指移动操作

模拟手机的滑动操作
方法:move_to(el=None, x=None, y=None)
参数:
1.el:定位的元素
2.x:相对于前一个元素的X轴偏移量
3.y:相对于前一个元素的Y轴偏移量

el = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
el1 = driver.find_element_by_xpath("//*[contains(@text,'更多')]")
TouchAction(driver).press(el).move_to(el1).release().perform()
#TouchAction(driver).press(x=240,y=600).wait(100).move_to(x=240,y=100).release().perform()
# 绘制图案四个坐标 1:(244,967) 2:(723,967) 3:(723,1442) 4:(244,1916)
TouchAction(driver).press(x=244,y=967).wait(100).move_to(x=479,y=0).wait(100).move_to(x=0,y=475).wait(100).move_to(x=-479,y=474).release().perform()

滑动相关功能时,坐标不要写死,如driver.swipe(300, 1000, 300, 300)

screen_width = self.get_window_size()['width']
screen_height = self.get_window_size()['height']    
x = screen_width / 2
starty = h screen_height eight * 0.75
endy = screen_height * 0.20
self.swipe(x, starty, x, endy, 1000)
五、手机操作API
  • 获取手机时间
    driver.device_time
  • 获取手机的宽高
    driver.get_window_size()
  • 发送键到设备
    模拟系统键值的操作,比如操作honme键,音量键,返回键等。

方法:keyevent(keycode, metastate=None):
参数:
keycode:发送给设备的关键代码
metastate:关于被发送的关键代码的元信息,一般为默认值

from appium.webdriver.extensions.android.nativekey import AndroidKey
driver.keyevent(AndroidKey.HOME)

  • 操作手机通知栏
    打开手机的通知栏,可以获取通知栏的相关信息和元素操作
    driver.open_notifications()
  • 获取手机当前网络
    driver.network_connection
  • 设置手机网络
    更改手机的网络模式,模拟特殊网络情况下的测试用例
    from appium.webdriver.connectiontype import ConnectionType
    飞行模式
    driver.set_network_connection(ConnectionType.AIRPLANE_MODE)
  • 手机截图
    get_screenshot_as_file(filename)
    filename:指定路径下,指定格式的图片.
driver.get_screenshot_as_file('./test.png')
import os
driver.get_screenshot_as_file(os.getcwd() + os.sep + './screen.png')
capabilities启动参数列表

相关文章

网友评论

      本文标题:移动自动化--操作

      本文链接:https://www.haomeiwen.com/subject/ufpqohtx.html