Scrolling is working very fast


#1

Hello all,


** In my script scrolling is happening very first ,so that I am not able to perform some operation on the desired element. How to stop scrolling when the element is visible.**
Please have a look on my code.

File f=new File(“ApiDemos-debug.apk”);
File fs=new File(f,"");
System.out.println(fs.getAbsolutePath());
driver=DemoInRealDevice.runAppiumInRealDevice();
TouchAction ta=new TouchAction(driver);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElementByXPath("//android.widget.TextView[@text=‘Views’]").click();
Thread.sleep(3000);
driver.findElementByAndroidUIAutomator(“new UiScrollable(new UiSelector()).scrollIntoView(text(“Grid”));”);

here instead of scrolling to the grid it moving very fast, so that not able to do anything on the grid element.


#2

@suryakanta939 is you scroll view horizontal or vertical ?


#3

@Aleksei Its vertical and it’s downwards


#4

@suryakanta939 i remember! this client. found something… try:

   tapCellByTitle("Grid");

    public Boolean tapCellByTitle(String title) {
        System.out.println("  tapCellByTitle(): " + title);
        List<AndroidElement> elementList = driver.findElements(MobileBy.AndroidUIAutomator(
                "new UiScrollable(new UiSelector().resourceIdMatches(\".*id/list\")).setMaxSearchSwipes(5).scrollIntoView("
                        + "new UiSelector().text(\"" + title + "\"))"));
        if (elementList.isEmpty())
            return false;
        else {
           try {
             new TouchAction((MobileDriver) driver).press(elementList.get(0)).waitAction(Duration.ofMillis(70)).release().perform();
             return true;
            } catch (Exception e) {
              e.printStacktrace();
            }
        }
    }

#5

@Aleksei Thanks,
You are awesome.Let me try with that sample


#6

@Aleksei it’s working like charm…So I think this is the best way to scroll down compare to findByuiAutomator, as per the text and there will be no worries of moving fast and missed some element. But it’s taping on that particular element .how can I avoid tapping ,I want only to scroll…Thanks In advance


#7

@suryakanta939

replace with “return true;” and it will just scroll.


#8

@Aleksei Nope I tried all the possible options and it’s doing both the operation like scroll and click


#9

@suryakanta939 i do not understand you. if you need just scroll:

    public Boolean scrollToCellByTitle(String title) {
        System.out.println("  scrollToCellByTitle(): " + title);
        List<AndroidElement> elementList = driver.findElements(MobileBy.AndroidUIAutomator(
                "new UiScrollable(new UiSelector().resourceIdMatches(\".*id/list\")).setMaxSearchSwipes(5).scrollIntoView("
                        + "new UiSelector().text(\"" + title + "\"))"));
        if (elementList.isEmpty())
            return false;
        else {
          return true;
        }
    }

#10

@Aleksei sorry I just did it the wrong way and its working fine now… .we can move to up and down with this single piece of code which looks more better than what I was trying before.Thanks a lot::smiley::smiley:

And one more doubt: to move both lefts and right in a horizontal way, then can we use the touch action functions like getting the x and y value. or If there is any other way to do. if yes can you please provide the sample code …thanks in advance


#11

@suryakanta939 for horizontal use:

            List<AndroidElement> elementList = driver.findElements(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector().resourceIdMatches(\".*id/list\")).setAsHorizontalList().setMaxSearchSwipes(5).scrollIntoView("
                            + "new UiSelector().text(\"" + title + "\"))"));

#12

@Aleksei Thanks…:smiley::smiley::smiley:


#13

Hi @suryakanta939,

This code will work for vertical scroll,but for horizontal scroll unable to perform until exact text found.
Dimension dsize=driver.manage().window().getSize();
int startx =(int)(dsize.width0.80);
int endx =(int)(dsize.width
0.10);
int starty =(int)(dsize.height/2);
for(int i=0;i<2;i++){
driver.swipe(startx, starty, endx, 0, 2000);
}
Please find the attached screenshot here have to scroll from category Burger -Horizontal scroll


#14

@Vijaysimha there are MANY horizontal scroll lists on your screen.

for example: how you find “Burgers” scroll view with your page and how you try scroll it horizontally?


#15

Yes am trying to find solution by getting elements location x and y coordinates. It has to work based on the coordinates.


#16

I solved Horizontal scroll by this approach:

public void HorizontalSwipe (AndroidDriver driver,int times,double percentage)
{
Dimension size =driver.manage().window().getsize();
int height = size.getHeight();
int width= size.getWidth();
int y=(int)(height * percentage);
int startx=(int)(width * 0.75);
int endx=(int)(width * 0.20);

for(int i=0;i<times;i++)
{
driver.swipe(startx,y,endx,y,1000);
}

Usage:
HorizontalSwipe(driver,3,0.40);
3 -No of times to be scrolled
Here 0.40 means 40% of screen Height(Y axis)
Y axis can be calculated based on 0 to 100 percentage scale ,where Y axis must be changed depend on height.
For Example:
Attached screenshot we have Category Chicken which comes under 40% of screen height.