Appium九宫格滑动解锁研究

       九宫格滑动解锁,目前发现有两种一种是每个可点的方格都是一个imageview,九宫格则对应有9个imageview,此类解锁已有前人研究解决了,可以参照tobecrazy的博客

今天我要说的是另一种,九宫格显示在一个view中,类似下图所示:

《Appium九宫格滑动解锁研究》

对于此类滑动解锁,只能使用坐标点来滑动了,要使用坐标点九必须得到1~9这几个点在屏幕上的相对位置,要得到相对位置首先要得到元素view的起始坐标(就是左上角的x和y坐标点),然后再获取到元素的宽和高,由此可以得到1到9的这9个坐标点的x和y坐标,再利用touchaction类的方法进行封装即可,具体实现如下:

	/**
	 * 针对应用的九宫格滑动解锁,每个滑动点么有独立的元素,只能通过相对的坐标点进行滑动
	 * @param driver
	 * @param element
	 */
	public void unlockApp(AppiumDriver driver,WebElement element) throws Exception{
		TouchAction ta=new TouchAction(driver);
		//元素的起始x和y坐标
		int x=element.getLocation().getX();
		int y=element.getLocation().getY();
		//元素的宽和高
		int width=element.getSize().getWidth();
		int height=element.getSize().getHeight();
		//九宫格图案,对应的1-9个数字的位置
		int num1x=x+width/4;
		int num1y=y+height/4;
		int num4x=num1x;
		int num4y=y+height/2;
		int num7x=num1x;
		int num7y=y+height*3/4;
		int num2x=x+width/2;
		int num2y=y+height/4;
		int num5x=num2x;
		int num5y=y+height/2;
		int num8x=num2x;
		int num8y=y+height*3/4;
		int num3x=x+width*3/4;
		int num3y=y+height/4;
		int num6x=num3x;
		int num6y=y+height/2;
		int num9x=num3x;
		int num9y=y+height*3/4;
		//moveTo(x,y)滑动时需要提供相对于press坐标点的相对位置,按住一个点后,如果向左或向右滑动那么x坐标为元素宽度的1/4,y坐标为0,相对坐标值为正数时向右滑动,为负数时向左滑动
		//上下滑动时,x相对坐标为0,y的相对坐标为高度的height/4,相对坐标值为正数时向下滑动,为负数时向上滑动
		/*Z字形解锁图案*/
		ta.press(num1x, num1y).waitAction(500).moveTo(width/4, 0).moveTo(width/4, 0).moveTo(-width/4, height/4).moveTo(-width/4, height/4).moveTo(width/4, 0).moveTo(width/4, 0).release().perform();
		/*7字形解锁图案*/
		ta.press(num1x, num1y).waitAction(500).moveTo(width/4, 0).moveTo(width/4, 0).moveTo(0, height/4).moveTo(0, height/4).release().perform();
	}

滑动解锁效果图:

《Appium九宫格滑动解锁研究》  《Appium九宫格滑动解锁研究》

    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/wsbl52006/article/details/51236305
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞