手機自動化測試:appium源碼分析之bootstrap九

來源:互聯網
上載者:User

標籤:

poptest是國內唯一一家培養測試開發工程師的培訓機構,以學員能勝任自動化測試,效能測試,測試載入器開發等工作為目標。如果對課程感興趣,請大家諮詢qq:908821478。TouchLongClick

package io.appium.android.bootstrap.handler;

 

import android.os.SystemClock;

import com.android.uiautomator.common.ReflectionUtils;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.Logger;

 

import java.lang.reflect.Method;

 

/**

 * This handler is used to long click elements in the Android UI.

 *

 */

public class TouchLongClick extends TouchEvent {

  /*

   * UiAutomator has a broken longClick, so we‘ll try to implement it using the

   * touchDown / touchUp events.

   */

  private boolean correctLongClick(final int x, final int y, final int duration) {

    try {

      /*

       * bridge.getClass() returns ShellUiAutomatorBridge on API 18/19 so use

       * the super class.

       */

 

      final ReflectionUtils utils = new ReflectionUtils();

      final Method touchDown = utils.getControllerMethod("touchDown", int.class,

          int.class);

      final Method touchUp = utils.getControllerMethod("touchUp", int.class, int.class);

 

      if ((Boolean) touchDown.invoke(utils.getController(), x, y)) {

        SystemClock.sleep(duration);

        if ((Boolean) touchUp.invoke(utils.getController(), x, y)) {

          return true;

        }

      }

      return false;

 

    } catch (final Exception e) {

      Logger.debug("Problem invoking correct long click: " + e);

      return false;

    }

  }

 

  @Override

  protected boolean executeTouchEvent() throws UiObjectNotFoundException {

    final Object paramDuration = params.get("duration");

    int duration = 2000; // two seconds

    if (paramDuration != null) {

      duration = Integer.parseInt(paramDuration.toString());

    }

 

    printEventDebugLine("TouchLongClick", duration);

    if (correctLongClick(clickX, clickY, duration)) {

      return true;

    }

    // if correctLongClick failed and we have an element

    // then uiautomator‘s longClick is used as a fallback.

    if (isElement) {

      Logger.debug("Falling back to broken longClick");

 

      return el.longClick();

    }

    return false;

  }

}

TouchLongClick類繼承於TouchEvent,而TouchEvent繼承於CommandHandler.調用TouchEvent的execute的方法中,調用了executeTouchEvent方法,所以我們來看上面的executeTouchEvent就好了,執行長點擊事件,在uiautomator裡有UiObject.longClick()方法,但是寫過case的人知道,有時候這個方法達不到我們的需求,但是我們是自己了反射調用TouchDown和TouchUp兩個個方法,而在appium裡幫你解決了,它自己就幫你做到了這一點,如果你傳入到是控制項對象,那無可厚非,還是調用UiObject.longClick方法,如果你想根據座標,時間在點擊的話,那麼就調用currectLongClick這個appium給你封裝好的方法。

 

final ReflectionUtils utils = new ReflectionUtils();

      final Method touchDown = utils.getControllerMethod("touchDown", int.class,

          int.class);

      final Method touchUp = utils.getControllerMethod("touchUp", int.class, int.class);

通過反射得到uiautomator裡的沒有公開的類,從而我們想要的方法touchDown和touchUp.

public ReflectionUtils() throws IllegalArgumentException,

      IllegalAccessException, SecurityException, NoSuchFieldException {

    final UiDevice device = UiDevice.getInstance();

    final Object bridge = enableField(device.getClass(), "mUiAutomationBridge")

        .get(device);

    if (API_18) {

      controller = enableField(bridge.getClass().getSuperclass(),

          "mInteractionController").get(bridge);

    } else {

      controller = enableField(bridge.getClass(), "mInteractionController")

          .get(bridge);

    }

  }

因為uiautomator api的改動,在api18以上的版本中,mInteractionController是存在於UiAutomationBridge的父類中的變數,而在18以下的版本中它是存在於本類中的。所以反射時會有一點點小小點差異,但總的來說都是要獲得InteractionController這個類,因為這個類裡面存在有我們要但touch類但方法。最後我們就能輕鬆調用滑鼠的TouchUp和TouchDown他們啦。然後再加上時間,長按就實現啦。

TouchUp

package io.appium.android.bootstrap.handler;

 

import com.android.uiautomator.common.ReflectionUtils;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.Logger;

 

import java.lang.reflect.Method;

 

/**

 * This handler is used to perform a touchDown event on an element in the

 * Android UI.

 *

 */

public class TouchDown extends TouchEvent {

 

  @Override

  protected boolean executeTouchEvent() throws UiObjectNotFoundException {

    printEventDebugLine("TouchDown");

    try {

      final ReflectionUtils utils = new ReflectionUtils();

      final Method touchDown = utils.getControllerMethod("touchDown", int.class,

          int.class);

      return (Boolean) touchDown.invoke(utils.getController(), clickX, clickY);

    } catch (final Exception e) {

      Logger.debug("Problem invoking touchDown: " + e);

      return false;

    }

  }

}

有了上面的分析,對TouchUp和TouchDown還有TouchMove的分析就不用再多說了,都是反射的原理

手機自動化測試:appium源碼分析之bootstrap九

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.