如何使用Appium放大移动网络

时间:2017-11-06 13:00:29

标签: appium mobile-website

我的项目中有一个场景,我需要使用Appium自动化执行放大操作Android Mobile web。我使用了以下方法。

try {
        Dimension size = driver.manage().window().getSize();
        int x0 = (int) (size.getWidth()*0.5);
        int y0 = (int) (size.getHeight()*0.5);
        System.out.println(x0+" "+y0);
        driver.zoom(100, 500);
        reportStep("The Application is zoomed.", "PASS");
    } catch (Exception e) {
        e.printStackTrace();
        reportStep("The Application could not be zoomed.", "FAIL");
    }

但是这种方法在移动应用程序中运行良好,而不在Mobile Web中运行。 是否有任何特定的方法或替代方法来处理Mobile web zoom?

我正在使用Appium Java客户端版本5.0.4和Selenium版本3.6.0。

最新版本不提供缩放和缩放方法。

1 个答案:

答案 0 :(得分:0)

没有zoom方法的示例不多,但将MultiTouchAction类与TouchAction类结合使用应该如图所示here。请注意,在moveTo中使用WebElement和偏移x,在该线程中建议使用y坐标而不是绝对坐标。

    driver = new AndroidDriver(new URL(nodeURL), capability);

    TouchAction  touchAction = new TouchAction(driver);
    MultiTouchAction  multiTouchAction = new MultiTouchAction(driver);

    WebElement search = (WebElement)   driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.google.android.apps.maps:id/search_omnibox_container\")"));
     touchAction .tap(search).perform();

        int leftX = search.getLocation().getX();
        int rightX = search.getSize().getWidth() + leftX;


        int upperY = search.getLocation().getY();
        int lowerY = search.getSize().getHeight() + upperY;

       a1 = touchAction.press(search,x1,y1).waitAction(2000).moveTo(search,x3,y1).release();
       a2 = touchAction.press(search,x2,y1).waitAction(2000).moveTo(search,x4,y1).release();

       multiTouchAction.add(a1).add(a2).perform();

另外,请查看here以获取appium MultiTouch和here,以便将偏移量x,y坐标传递给moveTo