Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

java - How to reach the end of a scroll bar in appium?

I'm trying to automate scrolling of a horizontal bar,where the elements of the bar are dynamic and are getting fetched from an API.

Is there a way to automate it in appium?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have any element or text in the bottom of page then you can use UiAutomator2.

add in desired capability 'UiAutomator2' if you are using appium.

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");

Now use below functions if you have element's id

 public void scrollByID(String Id, int index) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId(""+Id+"").instance("+index+"));")); 

        } catch (Exception e) {
           e.printStackTrace();
        }
    }

Use this if you have element's text.

public void scrollByText(String menuText) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches("" + menuText + "").instance(0));")); 
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

If you don't know any element in botton then you have to use screen size

public void scrollToBottom() {

      int  x = driver.manage().window().getSize().width / 2;
      int start_y = (int) (driver.manage().window().getSize().height * 0.2);
      int end_y = (int) (driver.manage().window().getSize().height * 0.8);
        TouchAction dragNDrop = new TouchAction(driver)
                        .press(PointOption.point(x,start_y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
                        .moveTo(PointOption.point(x, end_y))
                        .release();
        dragNDrop.perform();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...