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

來源:互聯網
上載者:User

標籤:

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

package io.appium.android.bootstrap.handler;

 

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

 

/**

 * This handler is used to get the text of elements that support it.

 */

public class GetName extends CommandHandler {

 

  /*

   * @param command The {@link AndroidCommand} used for this handler.

   *

   * @return {@link AndroidCommandResult}

   *

   * @throws JSONException

   *

   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

   * bootstrap.AndroidCommand)

   */

  @Override

  public AndroidCommandResult execute(final AndroidCommand command)

      throws JSONException {

    if (!command.isElementCommand()) {

      return getErrorResult("Unable to get name without an element.");

    }

 

    try {

      final AndroidElement el = command.getElement();

      return getSuccessResult(el.getContentDesc());

    } catch (final UiObjectNotFoundException e) {

      return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());

    } catch (final Exception e) { // handle NullPointerException

      return getErrorResult("Unknown error");

    }

  }

}

最終會調用UiObject.getContentDescription()方法獲得控制項當描述資訊

 

GetAttribute

package io.appium.android.bootstrap.handler;

 

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.*;

import io.appium.android.bootstrap.exceptions.NoAttributeFoundException;

import org.json.JSONException;

 

import java.util.Hashtable;

 

/**

 * This handler is used to get an attribute of an element.

 *

 */

public class GetAttribute extends CommandHandler {

 

  /*

   * @param command The {@link AndroidCommand} used for this handler.

   *

   * @return {@link AndroidCommandResult}

   *

   * @throws JSONException

   *

   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

   * bootstrap.AndroidCommand)

   */

  @Override

  public AndroidCommandResult execute(final AndroidCommand command)

      throws JSONException {

    if (command.isElementCommand()) {

      // only makes sense on an element

      final Hashtable<String, Object> params = command.params();

 

      try {

        final AndroidElement el = command.getElement();

        final String attr = params.get("attribute").toString();

        if (attr.equals("name") || attr.equals("text")

            || attr.equals("className")) {

          return getSuccessResult(el.getStringAttribute(attr));

        } else {

          return getSuccessResult(String.valueOf(el.getBoolAttribute(attr)));

        }

      } catch (final NoAttributeFoundException e) {

        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

            e.getMessage());

      } catch (final UiObjectNotFoundException e) {

        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

            e.getMessage());

      } catch (final Exception e) { // el is null

        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

            e.getMessage());

      }

    } else {

      return getErrorResult("Unable to get attribute without an element.");

    }

  }

}

該事件是為擷取控制項相關資訊設定的,來看看可以擷取哪些資訊,其實就是uiautomatorviewer裡的資訊。

public String getStringAttribute(final String attr)

      throws UiObjectNotFoundException, NoAttributeFoundException {

    String res;

    if (attr.equals("name")) {

      res = getContentDesc();

      if (res.equals("")) {

        res = getText();

      }

    } else if (attr.equals("text")) {

      res = getText();

    } else if (attr.equals("className")) {

      res = getClassName();

    } else {

      throw new NoAttributeFoundException(attr);

    }

    return res;

  }

文本值:content-desc、text、className.

public boolean getBoolAttribute(final String attr)

      throws UiObjectNotFoundException, NoAttributeFoundException {

    boolean res;

    if (attr.equals("enabled")) {

      res = el.isEnabled();

    } else if (attr.equals("checkable")) {

      res = el.isCheckable();

    } else if (attr.equals("checked")) {

      res = el.isChecked();

    } else if (attr.equals("clickable")) {

      res = el.isClickable();

    } else if (attr.equals("focusable")) {

      res = el.isFocusable();

    } else if (attr.equals("focused")) {

      res = el.isFocused();

    } else if (attr.equals("longClickable")) {

      res = el.isLongClickable();

    } else if (attr.equals("scrollable")) {

      res = el.isScrollable();

    } else if (attr.equals("selected")) {

      res = el.isSelected();

    } else if (attr.equals("displayed")) {

      res = el.exists();

    } else {

      throw new NoAttributeFoundException(attr);

    }

    return res;

  }

boolean類型的值:如上等等。

GetDeviceSize

package io.appium.android.bootstrap.handler;

 

import com.android.uiautomator.core.UiDevice;

import io.appium.android.bootstrap.AndroidCommand;

import io.appium.android.bootstrap.AndroidCommandResult;

import io.appium.android.bootstrap.CommandHandler;

import org.json.JSONException;

import org.json.JSONObject;

 

/**

 * This handler is used to get the size of the screen.

 *

 */

public class GetDeviceSize extends CommandHandler {

 

  /*

   * @param command The {@link AndroidCommand} used for this handler.

   *

   * @return {@link AndroidCommandResult}

   *

   * @throws JSONException

   *

   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

   * bootstrap.AndroidCommand)

   */

  @Override

  public AndroidCommandResult execute(final AndroidCommand command) {

    if (!command.isElementCommand()) {

      // only makes sense on a device

      final UiDevice d = UiDevice.getInstance();

      final JSONObject res = new JSONObject();

      try {

        res.put("height", d.getDisplayHeight());

        res.put("width", d.getDisplayWidth());

      } catch (final JSONException e) {

        getErrorResult("Error serializing height/width data into JSON");

      }

      return getSuccessResult(res);

    } else {

      return getErrorResult("Unable to get device size on an element.");

    }

  }

}

擷取螢幕的長和寬,調用的是UiDevice的方法:getDisplayHeight()和getDisplayWidth()

 

GetSize

 

package io.appium.android.bootstrap.handler;

 

import android.graphics.Rect;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

import org.json.JSONObject;

 

/**

 * This handler is used to get the size of elements that support it.

 *

 */

public class GetSize extends CommandHandler {

 

  /*

   * @param command The {@link AndroidCommand} used for this handler.

   *

   * @return {@link AndroidCommandResult}

   *

   * @throws JSONException

   *

   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

   * bootstrap.AndroidCommand)

   */

  @Override

  public AndroidCommandResult execute(final AndroidCommand command)

      throws JSONException {

 

    if (command.isElementCommand()) {

      // Only makes sense on an element

      final JSONObject res = new JSONObject();

      try {

        final AndroidElement el = command.getElement();

        final Rect rect = el.getBounds();

        res.put("width", rect.width());

        res.put("height", rect.height());

      } catch (final UiObjectNotFoundException e) {

        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

            e.getMessage());

      } catch (final Exception e) { // handle NullPointerException

        return getErrorResult("Unknown error");

      }

      return getSuccessResult(res);

    } else {

      return getErrorResult("Unable to get text without an element.");

    }

  }

}

擷取控制項的寬和高,調用的是UiObject的getBounds()。得到一個矩形,然後獲得其寬和高。

 

GetLocation

package io.appium.android.bootstrap.handler;

 

import android.graphics.Rect;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

import org.json.JSONObject;

 

/**

 * This handler is used to get the text of elements that support it.

 *

 */

public class GetLocation extends CommandHandler {

 

  /*

   * @param command The {@link AndroidCommand} used for this handler.

   *

   * @return {@link AndroidCommandResult}

   *

   * @throws JSONException

   *

   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

   * bootstrap.AndroidCommand)

   */

  @Override

  public AndroidCommandResult execute(final AndroidCommand command)

      throws JSONException {

    if (!command.isElementCommand()) {

      return getErrorResult("Unable to get location without an element.");

    }

 

    try {

      final JSONObject res = new JSONObject();

      final AndroidElement el = command.getElement();

      final Rect bounds = el.getBounds();

      res.put("x", bounds.left);

      res.put("y", bounds.top);

      return getSuccessResult(res);

    } catch (final Exception e) {

      return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());

    }

  }

}

擷取控制項的起始點座標。調用的也是getBounds,然後得到其起始點的x,y 座標

 

GetDataDir

 

package io.appium.android.bootstrap.handler;

 

import android.os.Environment;

import io.appium.android.bootstrap.AndroidCommand;

import io.appium.android.bootstrap.AndroidCommandResult;

import io.appium.android.bootstrap.CommandHandler;

 

/**

 * This handler is used to get the data dir.

 *

 */

public class GetDataDir extends CommandHandler {

 

  /*

   * @param command The {@link AndroidCommand} used for this handler.

   *

   * @return {@link AndroidCommandResult}

   *

   * @throws JSONException

   *

   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

   * bootstrap.AndroidCommand)

   */

  @Override

  public AndroidCommandResult execute(final AndroidCommand command) {

    return getSuccessResult(Environment.getDataDirectory());

  }

}

擷取data的根目錄。調用的是android的api:Environment.getDataDirectory()

手機自動化測試: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.