appium 一些操作的方法(java)

一、appium版本1.7上下左右页面的滑动

1、根据坐标位置滑动

参考文章: Appium 1.7 实现上下、左右滑动页面方法

完整代码如下:

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();
     }

//页面不断上滑,滑动到出现 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、根据页面元素滑动

参考文章:

 protected static void swipe(String id){
                  List jokes = new ArrayList();
                  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("滑动页面");
                  }

你可能感兴趣的:(appium 一些操作的方法(java))