Appium 1.7 實現上下、左右滑動頁面方法

Appium 1.7 實現上下、左右滑動頁面方法
說明:之前的文章:Appium 實現上下、左右滑動頁面 只適用於Appium 1.6.4及以下
Appium升級到1.7 後的問題:
(1)以前的driver.swipe方法不能用了。
(2)即便使用TouchAction類的滑動方法也不能用了,因爲waitAction有變化(waitAction(Duration)注意 這裏表是Duration對象,而不是以前直接的數字,如waitAction(1000))。
解決方法:
新建類:SwipeScreen.java
import java.time.Duration;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
public class SwipeScreen{	 
	static Duration duration=Duration.ofSeconds(1);
	public static void swipeUp(AndroidDriver driver)
	 {
		  int width = driver.manage().window().getSize().width;
		  int height = driver.manage().window().getSize().height;
		 TouchAction action1 = new TouchAction(driver).press(width / 2,height * 4/ 5).waitAction(duration).moveTo(width /2, height /4).release();
		 action1.perform();
	 }
	 public static void swipeDown(AndroidDriver driver)// scroll down to refresh
	 {
		  int width = driver.manage().window().getSize().width;
		  int height = driver.manage().window().getSize().height;
		 TouchAction action1 = new TouchAction(driver).press(width / 2,height/4).waitAction(duration).moveTo(width /2, height* 3/4).release();
	        action1.perform();
	 }
	 public static void swipeLeft(AndroidDriver driver)
	 {
		  int width = driver.manage().window().getSize().width;
		  int height = driver.manage().window().getSize().height;
		 TouchAction action1 = new TouchAction(driver).press(width -10,height/2).waitAction(duration).moveTo(width /4, height /2).release();
	        action1.perform();
	 }
	 public static void swipeRight(AndroidDriver driver)
	 {
		  int width = driver.manage().window().getSize().width;
		  int height = driver.manage().window().getSize().height;
		 TouchAction action1 = new TouchAction(driver).press(10,height/2).waitAction(duration).moveTo(width *3/4+10, height /2).release();
	        action1.perform();
	 }
}
使用方法:
SwipeScreen.swipeUp(driver);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章