一嚼贡、appium版本1.7上下左右頁面的滑動
1熏纯、根據(jù)坐標位置滑動
參考文章: Appium 1.7 實現(xiàn)上下、左右滑動頁面方法
完整代碼如下:
package icyapp.appDemoTest;
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();
}
//頁面不斷上滑粤策,滑動到出現(xiàn) THE END為止
public static void swipeToEnd(AndroidDriver driver){
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
boolean isSwipe = true;
String endString = "THE END";
while (isSwipe) {
swipeUp(driver);//向上滑動屏幕
String temp =driver.getPageSource();
if(temp.contains(endString))
isSwipe = false;
}
}
}
2樟澜、根據(jù)頁面元素滑動
參考文章:
protected static void swipe(String id){
List<AndroidElement> jokes = new ArrayList<AndroidElement>();
jokes = driver.findElementsByXPath(id);
AndroidElement firstJoke = jokes.get(0);
AndroidElement lastJoke = jokes.get(jokes.size()-2);
new TouchAction(driver).press(lastJoke).waitAction(Duration.ofSeconds(1)).moveTo(firstJoke).release().perform();
System.out.println("滑動頁面");
}