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

來源:互聯網
上載者:User

標籤:

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

package io.appium.android.bootstrap.handler;

 

import com.android.uiautomator.core.UiDevice;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

 

import java.util.Hashtable;

 

/**

 * This handler is used to set text in elements that support it.

 *

 */

public class SetText 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

      try {

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

        final AndroidElement el = command.getElement();

        String text = params.get("text").toString();

        Boolean pressEnter = false;

        if (text.endsWith("\\n")) {

          pressEnter = true;

          text = text.replace("\\n", "");

          Logger.debug("Will press enter after setting text");

        }

        final Boolean result = el.setText(text);

        if (pressEnter) {

          final UiDevice d = UiDevice.getInstance();

          d.pressEnter();

        }

        return getSuccessResult(result);

      } catch (final UiObjectNotFoundException e) {

        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

            e.getMessage());

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

        return getErrorResult("Unknown error");

      }

    } else {

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

    }

  }

}

寫了這麼多篇啦,上面的前幾行代碼都熟悉都不能再熟悉了,就不像第一篇文章那樣一一介紹了,setText方法就是在可編輯都文字框中輸入資料,所以也沒什麼特殊地方,無非就是調用UiObject的setText方法,不過仔細看看會發現,它做了一些處理,比如字串的分行符號刪掉,輸入完畢以後按一下enter鍵完成輸入。那麼我之前寫case的時候這些也需要自己做,因為如果不按enter鍵的話,有些IME是會出一些問題的,比如漢語它是先出你輸入的字元代表的漢字然後你按enter它才會正確的顯示在文字框裡的,不象英文IME直接在輸入框裡。所以appium考慮到裡這些已經幫我們處理啦。

 

getText

 

沒什麼可說到,就是簡單到調用了UiObject的getText方法擷取編輯框裡的資料。

 

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