码迷,mamicode.com
首页 > 移动开发 > 详细

Appium-Java滑动操作

时间:2017-06-09 11:16:52      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:rup   duration   bsp   long   滑动   apk   uri   ide   orm   

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滑动操作

标签:rup   duration   bsp   long   滑动   apk   uri   ide   orm   

原文地址:http://www.cnblogs.com/biyuting/p/6970716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!