Appium+Native swipe to element or swipe to bottom of page

=

summary:

A webview can find elements that are not inside the view.

But Appium's native_view cannot find elements that are not inside the view. This creates a problem in the scheme of wanting to swipe to an element.

 

webview can run js to slide.

native_view cannot use js.

=

refer to:

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

=

Code:

driver (that is, pass in the appiudriver object)

During (here is to fill in the number of milliseconds, the smaller the number of milliseconds here, the faster the sliding speed~ Generally set at 500~1000, if you want to slide quickly, you can set it to a smaller value)

num (it’s only the number of times of swiping, I’m doing a photo album page-turning test or swiping to the bottom of the list. Just enter the number of times directly)

    /**
     * Swipe up
     *
     * @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 + ",");
        }
    }

    /**
     * drop down
     *
     * @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 + ",");
        }
    }

    /**
     * Swipe left
     *
     * @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 + ",");
        }
    }

    /**
     * swipe right
     *
     * @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

We can easily use js to slide to the bottom of the page on the web and slide to the xxx element, but when it comes to the Native app, we will find that it is not easy to operate whether we are doing the scrollToElement operation or the last record of the listview. ,
First, simple swipe operations are difficult to apply to dynamic data or app automation that wants to be a common platform. Because we can't be sure how many swipes to perform.
Second, the swipe operation does not have its own assertion function. When the network is not good, the swipe also succeeds silently when the page cannot be swiped.
When I was looking for a solution, I found that some children's shoes are to get the last element of the listview, get the last element again after sliding, and then use the string equal to match the text obtained for the first time to judge whether it has slid to the bottom. This method may still be available in some specific scenarios, but it is very weak for components that are not listviews or cannot get the last element through getsize()-1. Here I still paste the code of this method, but Really not much use:
    // Get the last element before the first swipe
        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));

 

 

=

=

=

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326281790&siteId=291194637