其他分享
首页 > 其他分享> > 安卓apk自动化测试下载,安卓,打开,卸载

安卓apk自动化测试下载,安卓,打开,卸载

作者:互联网

文章目录


前言

本次测试主要是针对应用商店已上架的应用资源是否有效进行测试,测试是否能够查询到应用,能否获取对应应用的下载链接,下载链接是否能下载,下载后的apk是否能安装,安装后的apk是否能够打开,打开成功后的apk能否卸载

一、测试需求分析

由于apk安装,打开应用以及卸载都是需要在手机上进行,而apk的下载可以在pc或者手机上运行,所以为保证测试的质量和效率以及真实性
最终我们选择在手机端进行下载,安装,打开,以及卸载测试

二、测试工具实现步骤

1.实现获取下载链接和下载apk

使用HttpURLConnection 发送http请求,获取下载链接,和进行下载

代码如下(示例):

public String getAppDownloadUrlRequest(String httpUrl) {
        HttpURLConnection connection = null;
        InputStream in = null;
        BufferedReader br = null;
        StringBuilder result = new StringBuilder();
        try {
            URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方法
            connection.setRequestMethod("GET");
            //设置连接超时时间(毫秒)
            connection.setConnectTimeout(60 * 1000);
            //设置读取超时时间(毫秒)
            connection.setReadTimeout(60 * 1000);
            //返回输入流
            in = connection.getInputStream();
            br = new BufferedReader(new InputStreamReader(in));

            String line;
            while ((line = br.readLine()) != null) {
                result.append(line);
            }
            //Log.i("app test url" + i, result.toString());
            in.close();
            br.close();
        } catch (IOException e) {
            return e.toString();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return result.toString();
    }

2.实现apk的静默安装

通过使用packageInstaller 进行静默安装,需要该进程申请安装和卸载的权限

    public void installApp(String apkFilePath, ResultBean resultBean) {
        //Log.d(TAG, "installApp()------->" + apkFilePath);
        try {
            File apkFile = new File(apkFilePath);
            if (!apkFile.exists()) {
                resultBean.setInstallSuccess(false);
                resultBean.setInstallFailMsg("安装异常,请手动检查apk文件是否完整");
                TaskQueue.putOpenList(resultBean);
                ResultUtil.getInstance().addOpenResult(resultBean);
                return;
            }

            PackageInfo packageInfo = mContext.getPackageManager().getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
            if (packageInfo != null) {
                String packageName = packageInfo.packageName;
                int versionCode = packageInfo.versionCode;
                String versionName = packageInfo.versionName;
                Log.d("ApkActivity", "packageName=" + packageName + ", versionCode=" + versionCode + ", versionName=" + versionName);
            }
            Log.i(TAG, "开始安装" + resultBean.getAppName());
            PackageInstaller packageInstaller = mContext.getPackageManager().getPackageInstaller();
            PackageInstaller.SessionParams sessionParams
                    = new PackageInstaller.SessionParams(PackageInstaller
                    .SessionParams.MODE_FULL_INSTALL);
            //Log.d(TAG, "apkFile length" + apkFile.length());
            sessionParams.setSize(apkFile.length());
            mSessionId = packageInstaller.createSession(sessionParams);
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setInstallSuccess(false);
            resultBean.setInstallFailMsg("安装异常,请手动检查apk文件是否完整");
            TaskQueue.putOpenList(resultBean);
            ResultUtil.getInstance().addOpenResult(resultBean);
            return;
        }

        //Log.d(TAG, "sessionId---->" + mSessionId);
        if (mSessionId != -1) {
            boolean copySuccess = onTransfesApkFile(apkFilePath);
            // Log.d(TAG, "copySuccess---->" + copySuccess);
            if (copySuccess) {
                execInstallAPP();
            }
        }
    }

    private boolean onTransfesApkFile(String apkFilePath) {
        // Log.d(TAG, "---------->onTransfesApkFile()<---------------------");
        InputStream in = null;
        OutputStream out = null;
        PackageInstaller.Session session = null;
        boolean success = false;
        try {
            File apkFile = new File(apkFilePath);
            session = mContext.getPackageManager().getPackageInstaller().openSession(mSessionId);
            out = session.openWrite("base.apk", 0, apkFile.length());
            in = new FileInputStream(apkFile);
            int total = 0, c;
            byte[] buffer = new byte[1024 * 1024];
            while ((c = in.read(buffer)) != -1) {
                total += c;
                out.write(buffer, 0, c);
            }
            session.fsync(out);
            //Log.d(TAG, "streamed " + total + " bytes");
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != session) {
                session.close();
            }
            try {
                if (null != out) {
                    out.close();
                }
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return success;
    }

    private void execInstallAPP() {
        close();
        try (PackageInstaller.Session session = mContext.getPackageManager().getPackageInstaller().openSession(mSessionId)) {
            Intent installIntent = new Intent(mContext, InstallResultReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
                    1, installIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            session.commit(pendingIntent.getIntentSender());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.实现打开应用

根据包名启动应用

    public boolean assertLaunchByIntent(String packageName) {
        try {
            Thread.sleep(5 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent == null) {
            return false;
        } else {
            intent.addFlags(268468224);
            mContext.startActivity(intent);
            return isAppOpenSuccess(mContext, packageName);
        }

4.实现应用卸载

同安装类似

    public void unInstall(String packageName) {
        Intent unInstallIntent = new Intent(mContext, UnInstallResultReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 1,
                unInstallIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PackageInstaller packageInstaller = mContext.getPackageManager().getPackageInstaller();
        packageInstaller.uninstall(packageName, pendingIntent.getIntentSender());
        close();
    }

总结

主要是对,http请求,文件操作,静默安装,以及intent启动activity的用法的结合,有疑问请联系

标签:mContext,Log,安卓,apk,resultBean,卸载,new,null
来源: https://blog.csdn.net/weixin_42233460/article/details/112570677