手机自动化测试:appium源码分析之bootstrap十

手机自动化测试:appium源码分析之bootstrap十

 

   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键的话,有些输入法是会出一些问题的,比如汉语它是先出你输入的字符代表的汉字然后你按enter它才会正确的显示在文本框里的,不象英文输入法直接在输入框里。所以appium考虑到里这些已经帮我们处理啦。

 

getText

 

没什么可说到,就是简单到调用了UiObject的getText方法获取编辑框里的数据。

原文地址:https://www.cnblogs.com/poptest/p/4950613.html