`
fantaxy025025
  • 浏览: 1247544 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Appium+Native滑动到元素或滑动到页面底部

 
阅读更多

=

小结:

webview可以find到不在view内的元素。

但是Appium的native_view不能find到不在view内的元素。这就造成了想swipe到某个元素的方案,有问题。

 

webview可以运行js来滑动。

native_view不能用js。

=

参考:

http://www.cnblogs.com/tobecrazy/p/4612133.html

http://blog.csdn.net/JuniorKang/article/details/52880458?locationNum=4&fps=1

http://blog.sina.com.cn/s/blog_bd6a57440102wm1x.html

=

代码:

driver(就是把appiudriver对象传进来)

during(这里是填写毫秒数,这里的 毫秒数越小 滑动的速度越快~ 一般设定在500~1000,如果你想快速滑动 那就可以设置的更加小)

num(是只滑动的次数,本人在做相册 翻页测试什么的 滑动  或者滑动到列表底部。就直接输入次数就行了)

    /**
     * 上滑
     *
     * @param driver
     * @param during
     * @param swipeCount
     */
    public static void swipeToUp(AppiumDriver driver,int during, int swipeCount) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        for (int i = 0; i < swipeCount; i++) {
            driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during);
            CommonUtil.sleep(1000, "swipeToUp sleep when swipeCount > 0, now swipeCount=" + swipeCount + ",");
        }
    }

    /**
     * 下拉
     *
     * @param driver
     * @param during
     * @param swipeCount
     */
    public static void swipeToDown(AppiumDriver driver,int during, int swipeCount) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        System.out.println(width);
        System.out.println(height);
        for (int i = 0; i < swipeCount; i++) {
            driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
            CommonUtil.sleep(1000, "swipeToDown sleep when swipeCount > 0, now swipeCount=" + swipeCount + ",");
        }
    }

    /**
     * 向左滑动
     *
     * @param driver
     * @param during
     * @param swipeCount
     */
    public static void swipeToLeft(AppiumDriver driver,int during, int swipeCount) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        for (int i = 0; i < swipeCount; i++) {
            driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
            CommonUtil.sleep(1000, "swipeToLeft sleep when swipeCount > 0, now swipeCount=" + swipeCount + ",");
        }
    }

    /**
     * 向右滑动
     *
     * @param driver
     * @param during
     * @param swipeCount
     */
    public static void swipeToRight(AppiumDriver driver, int during, int swipeCount) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        for (int i = 0; i < swipeCount; i++) {
            driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
            CommonUtil.sleep(1000, "swipeToRight sleep when swipeCount > 0, now swipeCount=" + swipeCount + ",");
        }
    }

 

 

=

from:http://blog.sina.com.cn/s/blog_bd6a57440102wm1x.html

在web上滑动到页面底部、滑动到xxx元素我们可以很简单的用js就可以实现,但是到了Native app会发现不管我们是做scrollToElement操作还是要对listview最后一条记录做断言的时候都不好操作,
第一、简单的swipe操作很难适用于动态的数据或者想做通用平台的app自动化。因为我们不能确定要执行多少次滑动操作。
第二、swipe操作并不自带断言功能,当网络不好的时候页面不能滑动的时候swipe也是默然成功的。
我在查找解决方式的时候发现有的童鞋是获取listview的最后一个元素,在滑动之后再次获取最后一个元素然后用string的等于匹配第一次获取的文本来判断是否滑动到了底部。这种方式可能在一些特定的场景还是可用的但是对于不是listview或者不能通过getsize()-1来获取最后一个元素的组件这种方式显得很无力,这里我还是把这种方式的代码贴一下但是真心用处不多:
    // 第一次滑动前,获取最后一个元素
        List infolists1 = driver.findElementsById("resourceId");
        String originalinfo = infolists1.get(infolists1.size() - 1).getAttribute("text");
        System.out.println(originalinfo);
        Thread.sleep(1000);
        boolean isSwipe = true;
        String currentinfo;
        // 滑动
        while (isSwipe) {
            swipeToUp(1000);
            List infolists2 = driver.findElementsById("resourceId");
            currentinfo= infolists2.get(infolists2.size() - 1).getAttribute("text");
            if (!currentinfo.equals(originalinfo))
                originalinfo= currentinfo;
            else {
                isSwipe = false;
                System.out.println(currentinfo);
                System.out.println("This is the buttom");
            }
        }
还有一个更通用的解决方式是用appium自带的getPageSource方法来判断页面是否滑动到了底部或者是否滑动成功。非常好用,可以封装成通用的方法用于各类native app。
对于webview的话也可以加一个if分支做个context切换然后用js实现也非常简单。
下面给两个代码片段用于解决appium测试swipe滑动是否成功和判断是否滑动到页面底部
1、判断是否滑动成功:
 //获取当前手机的分辨率
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
 boolean Swiped;
String   beforeswipe=androidDriver.getPageSource(); androidDriver.swipe(width / 2, height / 4, width / 2, height * 3/ 4, 1000);
 String afterswipe=androidDriver.getPageSource();
Swiped=beforeswipe.equals(afterswipe)?true:false;
  iTestAssert.assertFalse(Swiped,String.format(resourceBundle.getString("swipe_to_fail")));
 androidUtils.takesScreenshot(String.format(resourceBundle.getString("after_swipe"), direction));
2、判断是否滑动到底部:
//获取当前手机的分辨率
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
String str1;String str2;
//  do while 循环
do{   
//滑动前获取pagesource
 str1=driver.getPageSource();  
  driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, 1000);    
Thread.sleep(2000);  
//滑动后获取pagesource
  str2=driver.getPageSource(); }while (!str1.equals(str2));

 

 

=

=

=

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics