zoukankan      html  css  js  c++  java
  • 手机自动化测试:appium源码分析之bootstrap十三

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

     

      

    poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478。

    Wake

    package io.appium.android.bootstrap.handler;

    import android.os.RemoteException;

    import com.android.uiautomator.core.UiDevice;

    import io.appium.android.bootstrap.AndroidCommand;

    import io.appium.android.bootstrap.AndroidCommandResult;

    import io.appium.android.bootstrap.CommandHandler;

    /**

     * This handler is used to power on the device if it's not currently awake.

     *

     */

    public class Wake 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) {

        // only makes sense on a device

        try {

          UiDevice.getInstance().wakeUp();

          return getSuccessResult(true);

        } catch (final RemoteException e) {

          return getErrorResult("Error waking up device");

        }

      }

    }

    wake事件为唤醒手机,有的时候手机处于睡眠状态就需要点亮屏幕,如果此时屏幕就是亮的,那么不做任何操作。

    PressBack

    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;

    /**

     * This handler is used to press back.

     *

     */

    public class PressBack 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) {

        UiDevice.getInstance().pressBack();

        // Press back returns false even when back was successfully pressed.

        // Always return true.

        return getSuccessResult(true);

      }

    }

    在手机上的返回键。

     

    TakeScreenshot

     

    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 java.io.File;

    /**

     * This handler is used to TakeScreenshot.

     *

     */

    public class TakeScreenshot 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) {

        final File screenshot = new File("/data/local/tmp/screenshot.png");

        try {

          screenshot.getParentFile().mkdirs();

        } catch (final Exception e) {

        }

        if (screenshot.exists()) {

          screenshot.delete();

        }

        UiDevice.getInstance().takeScreenshot(screenshot);

        return getSuccessResult(screenshot.exists());

      }

    }

    截屏并将图片保存在/data/local/tmp路径下的screenshot.png文件中。

    OpenNotification

    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 static io.appium.android.bootstrap.utils.API.API_18;

    /**

     * This handler is used to open the notification shade on the device.

     *

     */

    public class OpenNotification extends CommandHandler {

      /*

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

       *

       * @return {@link AndroidCommandResult}

       *

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

       * bootstrap.AndroidCommand)

       */

      @Override

      public AndroidCommandResult execute(final AndroidCommand command) {

        // method was only introduced in API Level 18

        if (!API_18) {

          return getErrorResult("Unable to open notifications on device below API level 18");

        }

        // does not make sense on an element

        if (command.isElementCommand()) {

          return getErrorResult("Unable to open notifications on an element.");

        }

        final UiDevice device = UiDevice.getInstance();

        if (device.openNotification()) {

          return getSuccessResult(true);

        } else {

          return getErrorResult("Device failed to open notifications.");

        }

      }

    }

    打开通知栏操作,api18以后在UiDevice中添加了openNotification()方法,打开通知栏。所以该事件就是去调用该方法。

  • 相关阅读:
    正则式记录
    限制键盘只能按数字键、小键盘数字键、退格键
    windows服务安装记录
    CheckBox使用记录
    you need to be root to perform this command
    Code First 更新数据库 记录
    EF查询记录
    sqlserver数据库存储汉字出现?
    【转】THE ROAD TO SUCCESS--听ERIC XING讲课记录
    Nice Computer Vision package collections
  • 原文地址:https://www.cnblogs.com/poptest/p/4953263.html
Copyright © 2011-2022 走看看