Appium-Java滑动操作

笛里谁知壮士心,沙头空照征人骨。这篇文章主要讲述Appium-Java滑动操作相关的知识,希望能为你提供帮助。
java滑动操作,通常可以直接使用API中androidDriver类中的swipe方法,直接进行调用

swipe(int startx, int starty, int endx, int endy, int duration)

但是,现在屏幕各式各样,滑动的结束位置均不太好掌控,因此,为了方便,可以将swipe方法进行如下改善
1、不可配置滑动次数
public void swipeToUp(AndroidDriver< AndroidElement> driver,int during){ int width = driver.manage().window().getSize().width; //获取当前屏幕的宽度 int height = driver.manage().window().getSize().height; //获取当前屏幕的高度 driver.swipe(width/2, height*3/4, width/2, height/4, during); }

2、可配置滑动次数
public void swipeToDown(AndroidDriver< AndroidElement> driver,int during,int num){ int width = driver.manage().window().getSize().width; //获取当前屏幕的宽度 int height = driver.manage().window().getSize().height; //获取当前屏幕的高度 for(int i=0; i< num; i++){ driver.swipe(width/2, height/4, width/2, height*3/4, during); } }

可以滑动的方法直接写在需要使用的类中,直接调用,也可以将方法封装在类中
1 package com.test; 2 3 import io.appium.java_client.android.AndroidDriver; 4 import io.appium.java_client.android.AndroidElement; 5 6 public class MyNumSwipe { 7/** 8* This Method for swipe up 9* 10* @author zt 11* @param driver 12* @param during 13* @param num 14*/ 15public void swipeToUp(AndroidDriver< AndroidElement> driver,int during,int num){ 16int width = driver.manage().window().getSize().width; //获取当前屏幕的宽度 17int height = driver.manage().window().getSize().height; //获取当前屏幕的高度 18for(int i = 0; i < num; i++){ 19driver.swipe(width/2, height*3/4, width/2, height/4, during); 20} 21} 22 23/** 24* This Method for swipe down 25* 26* @author zt 27* @param driver 28* @param during 29* @param num 30*/ 31public void swipeToDown(AndroidDriver< AndroidElement> driver,int during,int num){ 32int width = driver.manage().window().getSize().width; //获取当前屏幕的宽度 33int height = driver.manage().window().getSize().height; //获取当前屏幕的高度 34for(int i = 0; i < num; i++){ 35driver.swipe(width/2, height/4, width/2, height*3/4, during); 36} 37} 38 39/** 40* This Method for swipe Left 41* 42* @author zt 43* @param driver 44* @param during 45* @param num 46*/ 47public void swipeToLeft(AndroidDriver< AndroidElement> driver,int during,int num){ 48int width = driver.manage().window().getSize().width; //获取当前屏幕的宽度 49int height = driver.manage().window().getSize().height; //获取当前屏幕的高度 50for(int i = 0; i < num; i++){ 51driver.swipe(width*3/4, height/2, width/4, height/2, during); 52} 53} 54 55/** 56* This Method for swipe right 57* 58* @author zt 59* @param driver 60* @param during 61* @param num 62*/ 63public void swipeToRight(AndroidDriver< AndroidElement> driver,int during,int num){ 64int width = driver.manage().window().getSize().width; //获取当前屏幕的宽度 65int height = driver.manage().window().getSize().height; //获取当前屏幕的高度 66for(int i = 0; i < num; i++){ 67driver.swipe(width/4, height/2, width*3/4, height/2, during); 68} 69} 70 }

【Appium-Java滑动操作】swipe方法调用实例,以下实例已经使用360助手apk测试运行通过,可以执行滑动操作
1 package com.test; 2 3 import org.junit.After; 4 import org.junit.Before; 5 import org.junit.Test; 6 import org.openqa.selenium.remote.CapabilityType; 7 import org.openqa.selenium.remote.DesiredCapabilities; 8 import io.appium.java_client.android.AndroidDriver; 9 import io.appium.java_client.android.AndroidElement; 10 import java.io.File; 11 import java.net.URL; 12 13 public class MySwipeTest2 { 14private AndroidDriver< AndroidElement> driver; 15private boolean isInstall = false; 16private MyNumSwipe myNumSwipe; 17@Before 18public void setUp()throws Exception{ 19//set up appium 20DesiredCapabilities capbilities = new DesiredCapabilities(); 21capbilities.setCapability(CapabilityType.BROWSER_NAME, ""); 22capbilities.setCapability("platformName", "Android"); 23capbilities.setCapability("deviceName", "Android Emulator"); 24capbilities.setCapability("platformVersion", "5.0"); 25// if no need install don\'t add this 26if(isInstall){ 27File classpathRoot=new File(System.getProperty("user.dir")); 28File appDir = new File(classpathRoot,"apps"); 29File app = new File(appDir,"360.apk"); 30capbilities.setCapability("app", app.getAbsolutePath()); 31} 32capbilities.setCapability("appPackage", ""); 33capbilities.setCapability("appPackage", "com.qihoo.appstore"); 34// no need sign 35capbilities.setCapability("noSign", "True"); 36capbilities.setCapability("appActivity", "com.qihoo.appstore.home.MainActivity"); 37driver = new AndroidDriver< AndroidElement> (new URL("http://127.0.0.1:4723/wd/hub"), 38capbilities); 39} 40 41@After 42public void tearDown() throws Exception { 43driver.quit(); 44} 45 46public void sleep(long s) { 47try { 48Thread.sleep(s); 49} catch (InterruptedException e) { 50e.printStackTrace(); 51} 52} 53 54@Test 55public void swiptest(){ 56myNumSwipe = new MyNumSwipe(); 57sleep(10000); 58swipeToLeft(driver, 500,3); 59sleep(1000); 60myNumSwipe.swipeToRight(driver, 500,3); 61} 62 63public void swipeToLeft(AndroidDriver< AndroidElement> driver,int during,int num){ 64int width = driver.manage().window().getSize().width; //获取当前屏幕的宽度 65int height = driver.manage().window().getSize().height; //获取当前屏幕的高度 66for(int i=0; i< num; i++){ 67driver.swipe(width*3/4, height/2, width/4, height/2, during); 68} 69} 70 }

  另附上执行代码所需要的环境配置截图
Appium-Java滑动操作

文章图片

 

    推荐阅读