ScrollTo and ScrollToExact equivalent for webView in appium for android

僤鯓⒐⒋嵵緔 提交于 2019-12-13 16:08:19

问题


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


回答1:


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= " +dimensions);
            int scrollStart = (int) (dimensions.getHeight() * 0.5);
            System.out.println("Size of scrollStart= " +scrollStart);
            int scrollEnd = (int) (dimensions.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");
            }

      }



回答2:


Try switching to native context driver.context("NATIVE_APP"); before scroll . If it does not work then give a try for JavascriptExecutor like below sample

MobileElement element = driver.findElement(By.xpath("element-xpath"));
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put("direction", "down");
scrollObject.put("element", element);
js.executeScript("mobile: scroll", scrollObject);


来源:https://stackoverflow.com/questions/34847990/scrollto-and-scrolltoexact-equivalent-for-webview-in-appium-for-android

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