python+appium+Android自动化测试环境搭建步骤

第一步、安装node.js,安装javaSDK

第二步、安装.NET Framework

第三步、安装Appium(1.4.16)

第四步、安装appium-doctor

命令:③appium-doctor 安装

npm install appium-doctor -g

第五步、安装android-sdk

配置环境变量:

ANDROID_HOME:android-sdk的安装路径

在Path后加:%ANDROID_HOME%\tools\;%ANDROID_HOME%\platform-tools\;%ANDROID_HOME%\build-tools\29.0.2\

1、如何获取APP的Package和Activity

aapt是sdk自带的一个工具,在sdk\builds-tools\目录下

1.在cmd中,切换至sdk\builds-tools\29.0.2目录下,即aapt.exe目录下

2.命令行中切换到aapt.exe目录cmd命令执行:aapt dump badging C:\Users\foresee\Desktop\微信.apk(apk所在目录位置)

3.运行后的结果中以下两行分别是应用包名package和入口activity名称

package: name='com.tencent.mm'

launchable-activity: name='com.tencent.mm.ui.LauncherUI' 

2、如果使用adb devices命令检测不到设备

使用命令:adb kill-server然后再adb devices

3.INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling问题解决办法:

1).查看装的包

adb shell pm list packge -f | findstr appium

2)卸载掉adb uninstall io.appium.android.ime

adb uninstall io.appium.unlock

adb uninstall io.appium.settings

3)为防止每次启动appium自动装着三个包

找到appium的安装路径,X:\Program Files (x86)\Appium\node_modules\appium\lib\devices\android下的android.js并打开

找到113行和114行,将其注释掉,如图所示:

//    this.pushSettingsApp.bind(this),

//    this.pushUnlock.bind(this),

4.Appium+安卓7.0以上报错:Original error: Command failed: ps: uiautomator和 io.appium.android.ime每次都安装的问题

1).找到appium的安装目录下的adb.js文件,目录在:Appium\node_modules\appium\node_modules\appium-adb\lib 下

2).打开adb.js文件后,找到以下代码:

ADB.prototype.shell = function (cmd, cb) {

  if (cmd.indexOf('"') === -1) {

    cmd = '"' + cmd + '"';

  }

  var execCmd = 'shell ' + cmd;

  this.exec(execCmd, cb);

};

在上段代码的后面,添加如下代码:

ADB.prototype.shell_grep = function (cmd, grep, cb) {

  if (cmd.indexOf('"') === -1) {

    cmd = '"' + cmd + '"';

  }

  var execCmd = 'shell ' + cmd + '| grep ' + grep;

  this.exec(execCmd, cb);

};

3).找到如下代码:

ADB.prototype.getPIDsByName = function (name, cb) {

  logger.debug("Getting all processes with '" + name + "'");

  this.shell("ps '" + name + "'", function (err, stdout) {

    if (err) return cb(err);

    stdout = stdout.trim();

    var procs = [];

    var outlines = stdout.split("\n");

    outlines.shift();

    _.each(outlines, function (outline) {

      if (outline.indexOf(name) !== -1) {

        procs.push(outline);

      }

    });

    if (procs.length < 1) {

      logger.debug("No matching processes found");

      return cb(null, []);

    }

    var pids = [];

    _.each(procs, function (proc) {

      var match = /[^\t ]+[\t ]+([0-9]+)/.exec(proc);

      if (match) {

        pids.push(parseInt(match[1], 10));

      }

    });

    if (pids.length !== procs.length) {

      var msg = "Could not extract PIDs from ps output. PIDS: " +

                JSON.stringify(pids) + ", Procs: " + JSON.stringify(procs);

      return cb(new Error(msg));

    }

    cb(null, pids);

  });

};

将该代码注释掉,换成以下的代码:

ADB.prototype.getPIDsByName = function (name, cb) {

  logger.debug("Getting all processes with '" + name + "'");

  this.shell_grep("ps", name, function (err, stdout) {

    if (err) {

      logger.debug("No matching processes found");

      return cb(null, []);

    }

    var pids = [];

    _.each(procs, function (proc) {

    var match = /[^\t ]+[\t ]+([0-9]+)/.exec(proc);

    if (match) {

    pids.push(parseInt(match[1], 10));

    }

    });

    if (pids.length !== procs.length) {

      var msg = "Could not extract PIDs from ps output. PIDS: " +

      JSON.stringify(pids) + ", Procs: " + JSON.stringify(procs);

      return cb(new Error(msg));

    }

    cb(null, pids);

  });

};

此时解决了每次启动都重复安装setting和unlock的APK文件,那么如何解决重复安装ime.APK文件呢?按照以下步骤即可:

4).解决每次启动Appium重复安装ime.APK文件的问题:

找到该目录下js文件: Appium\node_modules\appium\lib\devices\android\android-common.js

androidCommon.pushUnicodeIME=function(cb){

cb()                (添加这个方法,其他的代码内容直接注释掉)

/*  logger.debug("Pushing unicode ime to device...");  

var imePath = path.resolve(__dirname, "..", "..", "..", "build",      "unicode_ime_apk", "UnicodeIME-debug.apk");

  fs.stat(imePath, function (err) {   

 if (err) {  

    cb(new Error("Could not find Unicode IME apk; please run " +                  "'reset.sh --android' to build it.")); 

   } else {      this.adb.install(imePath, false, cb);  

  }  

}.bind(this));  

*/(注释以上代码)

};

5.重启Appium,这样你就可以随心所欲的进行自动化工具测试啦!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容