Appium [心得] 有關於點擊坐標在 iOS 報錯超出範圍

Giga · 2014年11月17日 · 1567 次阅读
本帖已被设为精华帖!

首先,這是一個常見的點擊螢幕上一個坐標的語法:

public static void ClickPoint(AppiumDriver wd, final double x, final double y) throws InterruptedException {
        JavascriptExecutor js = (JavascriptExecutor) wd;
        HashMap<String, Double> tapObject = new HashMap<String, Double>();
        // set point and time
        // how many times
        tapObject.put("tapCount", 1.0);
        // how many fingers
        tapObject.put("touchCount", 1.0);
        // how long in seconds
        tapObject.put("duration", 0.3);
        tapObject.put("x", x);
        tapObject.put("y", y);
        // do tap
        js.executeScript("mobile: tap", tapObject);
        Thread.sleep(delay);
    }

如果你跟我一樣是用 iOS,且你的坐標點是用 Appium Inspector 找出來,像我這樣子:

可是程式跟你報錯說超出螢幕範圍 (明明就在螢幕裡面呀),那你可能跟我一樣踩到同個坑

簡單來說,iOS app 最底層有一個 logical point,iOS Device 會依據自身的解析度去放大 app,使其符合 Device 螢幕
這個網站有很詳細的說明:http://www.iosres.com/

Appium 似乎是點擊在 logical resolution 上,而不是 device 的 resolution 上,而 Appium Inspector 抓到的是 device resolution 而不是 logical resolution
所以如果要在 iPad retina 上點坐標的話,要把 Appium Inspector 抓到的坐標除以 2,才不會超出螢幕範圍 (iPhone 6+ 要除以 3)
我都是用 iPad,還沒空測試 iPhone 是不是也是這樣

以上是被這雷搞了兩天,來發一下心得.....

共收到 1 条回复 时间 点赞
匿名 #3 · 2014年11月17日

原来还有这样的区别。。。
不过话说个人始终觉得通过 inspector 来找坐标终归不靠谱啊 😥
楼主可以把 page_source 打出来看看,里面应该都有各个控件的坐标了,当然前提是控件能识别的情况下。。

print driver.page_source
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册