【APPUIAuto】5.触屏操作,toast控件识别

TouchAntion 用法

官方文档

        action = TouchAction(self.driver)
        # 滑动操作
        # 获取屏幕 宽和高
        window_rect = self.driver.get_window_rect()
        width = window_rect['width']
        height = window_rect['height']
        # 根据屏幕 宽高设置滑动坐标点
        x1 = int(width/2)
        y1 = int(height*0.8)
        y2 = int(height*0.2)
        action.press(x = x1,y=y1).move_to(x=x1,y=y2).release().perform()

显示等待

  • WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exception+None)
    timetout:最长超时时间,单位秒
    poll_frequency:检测间隔时长,默认0.5
    ignored_exceptions 超时后抛出的异常信息,默认抛NoSuchElementException
  • WebDriverWait的until和until_not方法

WebDriverWait.unitl(expected_conditions.element_to_be_clickable(ele))
element_to_be_clickable,元素是否可点击
WebDriverWait(driver,timeout).unitl(lambda x: x.find_element())

定位toast

# 定位toast
        self.driver.find_element_by_xpath('//*[@class="android.widget.Toast"]')
        self.driver.find_element_by_xpath('//*[contains(@text,"xxxxx")]')

获取元素属性、断言

get Attribute
获取属性

search = self.driver.find_element_by_id("com.xueqiu.android:id/home_search")
        print(search.get_attribute('content-desc'))
  • Hamcrest断言
    github
    安装 pip install PyHamcrest
    def test_hamrest(self):
        # 判断相等
        assert_that(10, equal_to(10),'提示')
        # 判断是否在某个范围内,例如  是否在 10+-2 内
        assert_that(8.2, close_to(10 ,2))
        # 判断是否包含
        assert_that('contains', contains_string('tain'))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容