appium操作

1.获取driver属性

  • current_package,包名,aapt,元素定位给
  • current_activity,后面切换有用,==>url
  • context,上下文,web窗口切换,h5测试,获取上下文,driver.switch_to.context
  • contexts,所有的上下文,==>window_handlers
  • current_context,获取现在的上下文,获取当前窗口current_window_handler
  • page_source,源代码,html,driver.page_source
  • capabilities
  • device_time,设备时间
  • location,定位
# 获取现在的app页面的名称
print(driver.current_activity)
# 获取现在的app页面的源代码 xml
print(driver.page_source)
# 定位
print(driver.location)
# 获取caps参数信息
print(driver.capabilities)
# 获取窗口大小,屏幕尺寸
print(driver.get_window_size())

2.获取元素属性

  • el.rect,获取元素的尺寸,包括元素的宽度高度和起始点的坐标
  • el.text,获取元素的文本
  • el.get_attribute(),获取元素的属性值
el = driver.find_element("id", "com.lemon.lemonban:id/navigation_my")
# 获取元素的文本
print(el.text)
# 获取元素的尺寸,包含元素的高度宽度和起始点的坐标
print(el.rect)

3.用户操作

  • el.click()
  • el.send_keys(),注意,appium中输入中文,必须要配置caps中的参数
    "unicodeKeyboard": True,
    "resetKeyboard": True,
    使用真机操作时,需要修改手机配置的输入法后再进行输入

4.触屏操作

  • tap,相当于全局的click,适合封装在basepage中
  • move_to
  • press
  • long_press
  • release
  • wait
  • flick

5.swipe滑动

  • driver.swipe()

 在 Appium 中提供 swipe() 方法来模拟用户滑动屏幕。

 swipe() 实现过程 是先通过在屏幕上标记两个坐标,然后再从开始坐标移动到结束坐标。

# 滑动操作
# get_window_size()获取屏幕尺寸
width = driver.get_window_size()['width']
height = driver.get_window_size()['height']
driver.swipe(start_x=width * 0.9, start_y=height * 0.5, end_x=width * 0.1, end_y=height * 0.5)
driver.swipe(start_x=width * 0.9, start_y=height * 0.5, end_x=width * 0.1, end_y=height * 0.5)
driver.swipe(start_x=width * 0.9, start_y=height * 0.5, end_x=width * 0.1, end_y=height * 0.5)

封装后:

class Page:

    def __init__(self, driver):
        self.driver = driver

    def width(self):
        """获取app的宽度"""
        return self.driver.get_window_size()['width']

    def height(self):
        """获取app的高度"""
        return self.driver.get_window_size()['height']

    def swipe_left(self):
        """从右往左滑动"""
        width = self.width()
        height = self.height()
        self.driver.swipe(start_x=width * 0.9, start_y=height * 0.5, end_x=width * 0.1, end_y=height * 0.5)

    def swipe_right(self):
        """从左往右滑动"""
        width = self.width()
        height = self.height()
        self.driver.swipe(start_x=width * 0.1, start_y=height * 0.5, end_x=width * 0.9, end_y=height * 0.5)

    def swipe_up(self):
        """从下往上滑动"""
        width = self.width()
        height = self.height()
        self.driver.swipe(start_x=width * 0.5, start_y=height * 0.9, end_x=width * 0.5, end_y=height * 0.1)

    def swipe_down(self):
        """从上往下滑动"""
        width = self.width()
        height = self.height()
        self.driver.swipe(start_x=width * 0.5, start_y=height * 0.1, end_x=width * 0.5, end_y=height * 0.9)

6.按键操作

driver.press_keycode(8)

按键的数字代表的意义可参考:

Appium---Android的keycode键值 - 简书

7.toast弹框

前提

登录:Toast是viewer识别不到的,但是我们需要确认文本时候正确。

  • appium server 1.6.3以上
  • 代码中必须定制automationName为:UiAutomator2
  • UiAutomator2只支持安卓版本5.0+
  • jdk1.8 64位以上,配置JAVA_HOME环境变量

定位

a)表达式

  • xpath定位://*[contains(@text,'手机号码不能为空')]
  • find_element_by_xpath("//android.widget.Toast")

b)等待方式

注意:wait只能用pesence_of_element_located,不能用visibility

8.start_activity进入页面

app的路径依赖是很严重的,如果需要测试某个单纯的页面,但是进入这个页面有很长的路径,则会严重影响效率,而且路径过长,会让测试程序不稳定。

start_activity可以快速进入指定界面,只需要指定activity的名称。

获取当前页面的名称:

adb shell dumpsys activity | find "mResumedActivity"

appium操作

driver.start_activity('com.lemon.lemonban','.activity.LoginActActivity')

上一篇:spring aop简介


下一篇:appium学习笔记09-使用脚本完成简单的登录