首页 > 其他分享 >Appium的触屏操作&设备交互api

Appium的触屏操作&设备交互api

时间:2022-12-30 18:13:13浏览次数:37  
标签:Appium get self driver element window api rect 触屏

Appium的触屏操作

TouchAction---Appium 2.0以上不在可以使用

官网地址:https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.md

案例一:

self.driver.find_element(By.XPATH, "//*[@text='推荐']").click()
        window_rect = self.driver.get_window_rect()
        x = int(window_rect['width'] * 1/2)
        start_y = int(window_rect['height'] * 4/5)
        end_y = int(window_rect['height'] * 1/5)
        # print(start_y, end_y, window_rect, x)
        for i in range(5):
            time.sleep(1)
            self.driver.swipe(x, start_y, x, end_y, 1000)
View Code

 

设备交互api

  设备交互是指模拟手机电话、发送短信,切换流量等等,详情见官网: http://appium.io/docs/en/about-appium/intro/

 

 

案例一:模拟打电话,参照文档:http://appium.io/docs/en/commands/device/network/gsm-call/

    def test_make_gsm_call(self, phone):
        ###模拟打电话
        self.driver.make_gsm_call(phone, GsmCallActions.CALL)   ###GsmCallActions.CALL,make_gsm_call方法有案例demo
        ###模拟发短信
        self.driver.send_sms(phone, "test test...")
        ###模拟设置网络,只支持android
        self.driver.set_network_connection(1)
        ###截屏
        self.driver.get_screenshot_as_file('./img.png')
View Code

 参数备注详解:

get_screenshot_as_file(filename) 保存图片为.png 格式,filename 图片路径
save_screenshot(filename) 保存图片为.png 格式,filename 图片路径
get_screenshot_as_png() 保存图片为二进制格式
get_screenshot_as_base64() 将图片保存为 base64 格式。通常用在 html 里添加截图
driver.page_source通过获取页面源码,分析页面的 dom 结构

案例二:截图

def test_search(self):
    '''使用 xpath 定位'''
    logging.info("搜索用例")
    element = self.driver.find_element(MobileBy.XPATH,"//*[@resource-id='com.xueqiu.android:id/tv_search']")
    search_enabled = element.is_enabled()

    logging.info(f"搜索框的文本:{element.text},搜索框的坐标:{element.location},搜索框的size:{element.size}")

    if search_enabled == True:
        logging.info("点击搜索框")
        element.click()
        logging.info(f"输入搜索内容:alibaba")
        self.driver.find_element(MobileBy.XPATH,
                                 "//*[@resource-id='com.xueqiu.android:id/search_input_text']").\
            send_keys("alibaba")

        alibaba_element = self.driver.find_element(MobileBy.XPATH, "//*[@text='阿里巴巴']")

        # alibaba_element.is_displayed()
        displayed = alibaba_element.get_attribute("displayed")
        logging.info(f"是否可见:{displayed}")
        self.driver.save_screenshot("./image/search_result.png")
        assert displayed == "true
View Code

 

标签:Appium,get,self,driver,element,window,api,rect,触屏
From: https://www.cnblogs.com/margret/p/16339154.html

相关文章