今天遇到一个问题,想要点击某个控件的某个位置(左上,左下,右上,右下,中间),稍微做了封装,具体代码如下,有不妥欢迎指出:

/**
 * 单个手指,点击屏幕某个控件位置(左上,左下,右上,右下,中间)
 */
public enum Location {
    UPLEFT, LOWLEFT, UPRIGHT, LOWRIGHT, CENTRE
}

/**
 * 点击控件某个地方
 *
 * @param step
 * @param by
 * @param location
 */
public void clickControl(String step, By by, Location location) {
    log.info(step);
    // 获取控件开始位置的坐标轴
    Point start = this.findElement(driver, by).getLocation();
    int startX = start.x;
    int startY = start.y;
    // 获取控件坐标轴差
    Dimension q = this.findElement(driver,by).getSize();
    int x = q.getWidth();
    int y = q.getHeight();
    // 计算出控件结束坐标
    int endX = x + startX;
    int endY = y + startY;

    switch (location) {
        // 左上 点击
        case UPLEFT:
            driver.tap(1, startX + 10, startY + 10, 100);
            TaquTools.sleep(2);
            break;
        // 右上 点击
        case UPRIGHT:
            driver.tap(1, endX - 10, startY + 10, 100);
            TaquTools.sleep(2);
            break;
        // 左下 点击
        case LOWLEFT:
            driver.tap(1, startX + 10, endY - 10, 100);
            TaquTools.sleep(2);
            break;
        // 右下 点击
        case LOWRIGHT:
            driver.tap(1, endX - 10, endY - 10, 100);
            TaquTools.sleep(2);
            break;
        // 中间 点击
        case CENTRE:
            driver.tap(1, (endX + startX) / 2, (endY + startY) / 2, 100);
            TaquTools.sleep(2);
            break;
    }
}


↙↙↙阅读原文可查看相关链接,并与作者交流