【问题标题】:ScrollTo and ScrollToExact equivalent for webView in appium for androidScrollTo 和 ScrollToExact 等价于 appium for android 中的 webView
【发布时间】:2016-04-23 05:25:57
【问题描述】:

我正在使用 appium (java) 自动化 android 应用程序。
为了在原生组件上滚动,我使用 scrollTo()scrollToExact() 方法。
但如果是应用程序内部的 webView;这些方法不起作用。

更新: 这两种方法我也试过了:

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);
    }

但是在这两个中我都需要元素-但在页面上我找不到除 webview 之外的任何元素。
我正在使用 UiAutomator 来捕获 ID。
任何建议/链接/解决方法都会有所帮助

【问题讨论】:

    标签: android testing webview automated-tests appium


    【解决方案1】:

    尝试在滚动之前切换到本机上下文driver.context("NATIVE_APP");。 如果它不起作用,请尝试JavascriptExecutor,如下例所示

    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);
    

    【讨论】:

    • 这里的元素是什么?我的意思是假设我必须滚动到带有提示密码的文本字段。那么我应该如何调用上面的代码。非常感谢
    • 如果您有 Text Field 或 xpath 的 id,则元素为 MobileElement element = driver.findElement(By.id("text-field-id"));
    • 我没有得到任何 id - 在这个内部 - android.webkit.WebView ,UIAutomator 中没有捕获任何内容
    • 你需要切换到webview。手动尝试点击 webview 。有时也会发生这种情况。然后刷新 UIAutomator
    • 这不起作用 swami ,还有其他方式吗?我已经更新了我迄今为止尝试过的任何问题
    【解决方案2】:

    元素不会在 Web 视图中唯一地移位。你需要开发数学逻辑来获取屏幕大小并根据它们滚动,使用下面的代码,

    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");
                }
                
          }
    

    【讨论】:

    • 谢谢,我会试试这个,顺便说一下 IOException?它会抛出 IOException 吗?
    • 我有点困惑如何使用这种方法滚动特定维度,只是一个小小的帮助会有所帮助
    • @AnswerDroid 'code' 维度维度 = driver.manage().window().getSize();将获得屏幕尺寸。你可以通过编辑 scrollstart 和 scrollEnd 乘法因子来操作你想要的特定尺寸
    • 好的,我会试试的。这将在两个屏幕上工作?原生和混合?谢谢
    • 是的..这是通用的,只取设备屏幕尺寸
    猜你喜欢
    • 2016-09-08
    • 2016-11-08
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2015-04-07
    • 2012-04-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多