1. 程式人生 > >Appium-Java滑動操作

Appium-Java滑動操作

結束 etc png pstore type 代碼 rem als setup

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 */ 15 public void swipeToUp(AndroidDriver<AndroidElement> driver,int during,int num){ 16 int width = driver.manage().window().getSize().width;//獲取當前屏幕的寬度 17 int height = driver.manage().window().getSize().height;//獲取當前屏幕的高度 18 for(int i = 0;i < num;i++){ 19 driver.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 */ 31 public void swipeToDown(AndroidDriver<AndroidElement> driver,int during,int num){ 32 int width = driver.manage().window().getSize().width;//獲取當前屏幕的寬度 33 int height = driver.manage().window().getSize().height;//獲取當前屏幕的高度 34 for(int i = 0;i < num;i++){ 35 driver.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 */ 47 public void swipeToLeft(AndroidDriver<AndroidElement> driver,int during,int num){ 48 int width = driver.manage().window().getSize().width;//獲取當前屏幕的寬度 49 int height = driver.manage().window().getSize().height;//獲取當前屏幕的高度 50 for(int i = 0;i < num;i++){ 51 driver.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 */ 63 public void swipeToRight(AndroidDriver<AndroidElement> driver,int during,int num){ 64 int width = driver.manage().window().getSize().width;//獲取當前屏幕的寬度 65 int height = driver.manage().window().getSize().height;//獲取當前屏幕的高度 66 for(int i = 0;i < num;i++){ 67 driver.swipe(width/4, height/2, width*3/4, height/2, during); 68 } 69 } 70 }

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 {
14     private AndroidDriver<AndroidElement> driver;
15     private boolean isInstall = false;
16     private MyNumSwipe myNumSwipe;
17     @Before
18     public void setUp()throws Exception{
19         //set up appium
20         DesiredCapabilities capbilities = new DesiredCapabilities();
21         capbilities.setCapability(CapabilityType.BROWSER_NAME, "");
22         capbilities.setCapability("platformName", "Android");
23         capbilities.setCapability("deviceName", "Android Emulator");
24         capbilities.setCapability("platformVersion", "5.0");
25         // if no need install don‘t add this
26         if(isInstall){
27             File classpathRoot=new File(System.getProperty("user.dir"));
28             File appDir = new File(classpathRoot,"apps");
29             File app = new File(appDir,"360.apk");
30             capbilities.setCapability("app", app.getAbsolutePath());
31         }
32         capbilities.setCapability("appPackage", "");
33         capbilities.setCapability("appPackage", "com.qihoo.appstore");
34          // no need sign
35         capbilities.setCapability("noSign", "True");
36         capbilities.setCapability("appActivity", "com.qihoo.appstore.home.MainActivity");
37         driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),
38                 capbilities);
39       }
40     
41     @After  
42     public void tearDown() throws Exception {  
43        driver.quit();  
44     }
45     
46     public void sleep(long s) {
47         try {
48             Thread.sleep(s);
49         } catch (InterruptedException e) {
50             e.printStackTrace();
51         }
52     }
53     
54     @Test
55     public void swiptest(){
56         myNumSwipe = new MyNumSwipe();
57         sleep(10000);
58         swipeToLeft(driver, 500,3);
59         sleep(1000);
60         myNumSwipe.swipeToRight(driver, 500,3);
61     }     
62 
63     public void swipeToLeft(AndroidDriver<AndroidElement> driver,int during,int num){
64         int width = driver.manage().window().getSize().width;//獲取當前屏幕的寬度
65         int height = driver.manage().window().getSize().height;//獲取當前屏幕的高度
66         for(int i=0;i<num;i++){
67             driver.swipe(width*3/4, height/2, width/4, height/2, during);
68         }
69     }
70 }

另附上執行代碼所需要的環境配置截圖

技術分享

Appium-Java滑動操作