How to scroll top to botton using Android driver

烂漫一生 提交于 2019-12-12 18:26:32

问题


I tried scrollTo() to scroll my app page using andriod driver, but not worked because it is deprecated from javaclient 4.1.2.

I have gone through below link but couldn't find any solution . How can I scroll an android app page from top to bottom using appium driver?

Please provide me solution


回答1:


My example is from python but it will work for Java as well just use java syntax to find element like

driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains("**/Put some text which is at bottom of screen to scroll screen/**").instance(0))')

for more details you can go-through the https://developer.android.com/reference/android/support/test/uiautomator/UiScrollable.html and https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html




回答2:


User swipe method as below:

driver.swipe(startx, starty, endx, endy, duration);

e.g. for swipe down

driver.swipe(100, 100, 100, 900, 3000);

According you can change the x and y co-ordinates.




回答3:


you can try this below code, i was trying this code on settings page..

AppiumDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap1);
driver.scrollTo("About phone");

Pass the String which is present in the bottom of your app page.

driver.scrollTo("Enter your value");

Use appropriate wait Statements.




回答4:


@Test
    public void testScroll()throws Exception
    {
        for(int i=0;i<4;i++)
        {
            Thread.sleep(2000);
            if (driver.findElement(By.name("end_item")).isDisplayed())
            {
                driver.findElement(By.name("end_item")).click();
                break;
            }
            else
            {
                verticalScroll();
            }

        }
    }
    public void verticalScroll()
    {
        size=driver.manage().window().getSize();
        int y_start=(int)(size.height*0.60);
        int y_end=(int)(size.height*0.30);
        int x=size.width/2;
        driver.swipe(x,y_start,x,y_end,4000);
    }

The above example works with vertical scroll and it is based on the example given at this blog for horizontal scroll http://qaautomated.blogspot.in/2016/02/how-to-do-horizontal-scroll-in-appium.html I hope this works for you.




回答5:


We can use the following the code snippet to scroll down but please make sure you catch the exceptions.

        try {
            driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"\"))");
        }catch(Exception e) {
            System.out.println("whatever");
        }


来源:https://stackoverflow.com/questions/39658626/how-to-scroll-top-to-botton-using-android-driver

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!