1. 程式人生 > >Appium自動化測試(五)之 swipe方法封裝

Appium自動化測試(五)之 swipe方法封裝

由於最新版本已不支援swipe方法,所以需要自己手動去封裝一些方法:

package com.appiumTest.appiumdemo;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

//通過定位某個元素實現向左、右、上、下滑動
public class Swipe {
	public static void swipeToLeft(AndroidDriver<WebElement> driver,WebElement element) {
		//元素起始x和y座標
		Point point=element.getLocation();
		int startX=point.x;
		int startY=point.y;
		System.out.println("元素起始x="+startX+",元素起始y="+startY);
		//計算元素的寬和高
		Dimension dimension=element.getSize();
		int width=dimension.getWidth();
		int height=dimension.getHeight();
		System.out.println("元素寬width="+width+",元素高height="+height);
		//計算元素中間座標
		int centerX=startX+width*3/4;
		int centerY=startY+height*3/4;
		System.out.println("元素中心點距邊框寬centerX="+centerX+",高centerY="+centerY);
		TouchAction action=new TouchAction(driver);
		action.press(centerX, centerY).moveTo(5-centerX, 0).release().perform();

         }

	public static void swipeToRight(AndroidDriver<WebElement> driver,WebElement element) {
		//元素起始x和y座標
		Point point=element.getLocation();
		int startX=point.x;
		int startY=point.y;
		System.out.println("元素起始x="+startX+",元素起始y="+startY);
		//計算元素的寬和高
		Dimension dimension=element.getSize();
		int width=dimension.getWidth();
		int height=dimension.getHeight();
		System.out.println("元素寬width="+width+",元素高height="+height);
		//計算元素中間座標
		int centerX=startX+width*1/4;
		int centerY=startY+height/2;
		System.out.println("元素中心點距邊框寬centerX="+centerX+",高centerY="+centerY);
		int widthX=driver.manage().window().getSize().width;
		TouchAction action=new TouchAction(driver);
		action.press(centerX, centerY).moveTo(widthX-centerX-5, 0).release().perform();
         }
	
	public static void swipeToUp(AndroidDriver<WebElement> driver,WebElement element) {
		//元素起始x和y座標
		Point point=element.getLocation();
		int startX=point.x;
		int startY=point.y;
		System.out.println("元素起始x="+startX+",元素起始y="+startY);
		//計算元素的寬和高
		Dimension dimension=element.getSize();
		int width=dimension.getWidth();
		int height=dimension.getHeight();
		System.out.println("元素寬width="+width+",元素高height="+height);
		//計算元素中間座標
		int centerX=startX+width*1/2;
		int centerY=startY+height*3/4;
		System.out.println("元素中心點距邊框寬centerX="+centerX+",高centerY="+centerY);
		//int widthX=driver.manage().window().getSize().width;
		TouchAction action=new TouchAction(driver);
		action.press(centerX, centerY).moveTo(0, centerY-5).release().perform();
         }
	
	public static void swipeToBottom(AndroidDriver<WebElement> driver,WebElement element) {
		//元素起始x和y座標
		Point point=element.getLocation();
		int startX=point.x;
		int startY=point.y;
		System.out.println("元素起始x="+startX+",元素起始y="+startY);
		//計算元素的寬和高
		Dimension dimension=element.getSize();
		int width=dimension.getWidth();
		int height=dimension.getHeight();
		System.out.println("元素寬width="+width+",元素高height="+height);
		//計算元素中間座標
		int centerX=startX+width*1/2;
		int centerY=startY+height*1/4;
		System.out.println("元素中心點距邊框寬centerX="+centerX+",高centerY="+centerY);
		int heightY=driver.manage().window().getSize().height;
		TouchAction action=new TouchAction(driver);
		action.press(centerX, centerY).moveTo(0, heightY-centerY-5).release().perform();
         }
	
}