Search code examples
androidtestingwebviewautomated-testsappium

ScrollTo and ScrollToExact equivalent for webView in appium for android


I am automating android app using appium (java).
For scrolling on native components, I use scrollTo() and scrollToExact() methods.
But in case of webView inside the app ; these methods are not working.

UPDATE: I have tried these two methods also:

public static void scrollNavigation(AppiumDriver<MobileElement> wd, String widID, String target, String direction)
    {
        JavascriptExecutor js = (JavascriptExecutor) wd;
        HashMap<String, String> swipeObject = new HashMap<String, String>();
        swipeObject.put("text", target);
        swipeObject.put("direction", direction);
        swipeObject.put("element", widID);
        js.executeScript("mobile: scrollTo", swipeObject);
        wait(200);
    }

    public static void swipeUpElement(AppiumDriver<MobileElement> driver, MobileElement element, int duration)
    {
        int topY = element.getLocation().getY();
        int bottomY = topY + element.getSize().getHeight();
        int centerX = element.getLocation().getX() + (element.getSize().getWidth()/2);
        driver.swipe(centerX, bottomY, centerX, topY, duration);
    }

But in both these I need element- but on the page I cannot find any element except the webview.
I am using UiAutomator to capture the IDs.
Any suggestions / link / workaround will be helpful


Solution

  • ELements arent displaced uniquely in web view. U need to develop mathematical logic to get screen size and scroll according to them, use the below code,

    public void scroll() throws IOException {
              try {
                Dimension dimensions = driver.manage().window().getSize();
                System.out.println("Size of Window= " +dimension);
                int scrollStart = (int) (dimension.getHeight() * 0.5);
                System.out.println("Size of scrollStart= " +scrollStart);
                int scrollEnd = (int) (dimension.getHeight() * 0.2);
                System.out.println("Size of cscrollEnd= " + scrollEnd);             
                driver.swipe(0,scrollStart,0,scrollEnd,1000);           
                Application_Log.info("Screen Swiped " );            
                
                } catch (IOException e) {
                    
                    Assert.fail("Swipe failed");
                }
                
          }