稿APP自动化测试(11)-利用swip滑动屏幕

        在操作app的时候,我们操作得最多的是定位元素,然后直接对元素进行操作,但有的时候需要滑动屏幕,此时就要用到swip了

一、屏幕坐标介绍

如上图,最左上角的坐标为(0,0),而右下角是(x,y),x和y具体是多少我们不知道,需要用driver.get_window_size()活动宽和高,然后再进行移动

二、滑动实现代码

直接上代码,已经封装好的滑动屏幕代码

'''
实现屏幕的滑动
1、首先要活动屏幕宽和高的坐标
2、根据X或Y轴的移动实现屏幕滑动'''

from common.ComLog import LOGGER
class SwipeClass:
    """屏幕滑动"""
    def __init__(self,driver):
        """屏幕滑动_

        Args:
            driver (_type_): Appium 的 WebDriver对象
        """
        self.driver = driver
        #获取屏幕的宽和高
        size = self.driver.get_window_size()
        LOGGER.info(size)
        print(size)
        self.width = size["width"]
        self.height = size["height"]
        
    def swipe_up(self):
        """向上移动
        """
        LOGGER.info("向上移动")
        #x轴保持不变,y轴从0.75(下方)移动到0.25(上方),实现屏幕向上滑动
        start_x = self.width * 0.5
        start_y = self.height * 0.75
        end_x = self.width * 0.5
        end_y = self.height * 0.25
        self.driver.swip(start_x, start_y, end_x, end_y, 1000)
        
    def swipe_down(self):
        """向下移动
        """
        LOGGER.info("向下移动")
        #x轴保持不变,y轴从0.25(上方)移动到0.75(下方),实现屏幕向下滑动
        start_x = self.width * 0.5
        start_y = self.height * 0.25
        end_x = self.width * 0.5
        end_y = self.height * 0.75
        self.driver.swip(start_x, start_y, end_x, end_y, 1000)
        
    def swipe_left(self):
        """向左移动
        """
        LOGGER.info("向左移动")
        #Y轴保持不变,X轴从0.75(右侧)移动到0.25(左侧),实现屏幕向左滑动
        start_x = self.width * 0.75
        start_y = self.height * 0.5
        end_x = self.width * 0.25
        end_y = self.height * 0.5
        self.driver.swip(start_x, start_y, end_x, end_y, 1000)
        
    def swipe_right(self):
        """向右移动
        """
        LOGGER.info("向右移动")
        #Y轴保持不变,X轴从0.25(左侧)移动到0.75(右侧),实现屏幕向右滑动
        start_x = self.width * 0.25
        start_y = self.height * 0.5
        end_x = self.width * 0.75
        end_y = self.height * 0.5
        self.driver.swip(start_x, start_y, end_x, end_y, 1000)
        
    def swipe_by_director(self,director):
        """滑动屏幕,根据传入的方向实现不同方向滑动

        Args:
            director (str): 滑动方向
        """
        if director == "up":
            self.swipe_up()
        elif director == "down":
            self.swipe_down()
        elif director == "left":
            self.swipe_left()
        elif director == "right":
            self.swipe_right()
        else:
            LOGGER.warning("{} 该方向移动不支持".format(director))
            return False

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六天测试工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值