monkey测试时,通过一个守护app,不断调起被测app(通过包名去调起)

yuyu00 2019-06-26

问题:在做安卓随机压力测试时,被测APP经常跑出去,被别的app挡住,此时很可能出现断网,截图也截取了别的应用图,比较影响压力测试。
如何解决上面的问题呢?
思路可以有如下几种:
一,测试执行时,不断采用adb 命令,启动app
二,开发一个app,持续不断调起被测app,通过包名启动它
开发工具:android studio
用处:
1.通过包名启动其它app;
2.自动化测试时,有时候需要保证被测APP处于活动状态,而不是在后台。

代码如下:

package com.ming.t;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

//login.java主要就是实现login界面的功能
public class login extends AppCompatActivity {
    private Button TopApp;
    public static Context mContext;
    EditText appPackage;
    String appPackageText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        appPackage=(EditText)super.findViewById(R.id.appPackage);//获取用户输入的包名

        TopApp=(Button)findViewById(R.id.TopApp);
        TopApp.setOnClickListener(new View.OnClickListener()//侦听登录点击事件
                                 {
                                     public void onClick(View v)
                                     {
                                         appPackageText =  appPackage.getText().toString();
                                         if(appPackageText == null || appPackageText.equals(""))
                                         {
                                             Toast.makeText(getApplicationContext(), "包名必须填写", Toast.LENGTH_SHORT).show();//提示包名必须填写
                                         }else{
                                             int x = 1;
                                             while( x < 3600 ) {
                                                 System.out.print("value of x : " + x );
                                                 x++;
                                                 try {
                                                     Thread.sleep(1000);//休眠1秒
                                                 } catch (InterruptedException e) {
                                                     e.printStackTrace();
                                                 }
                                                 openAppWithPackageName(appPackage.getText().toString());
                                                 System.out.print("\n");
                                             }
                                         }

                                     }
                                 }
        );
    }

    private void openAppWithPackageName(String packagename) {
        // 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
        PackageInfo packageinfo = null;
        try {
            packageinfo = getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packageinfo == null) {
            return;
        }

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageinfo.packageName);
        // 通过getPackageManager()的queryIntentActivities方法遍历
        List<ResolveInfo> resolveinfoList = getPackageManager()
                .queryIntentActivities(resolveIntent, 0);

        if (!resolveinfoList.iterator().hasNext()){
            return ;
        }
        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            // packagename = 参数packname
            String packageName = resolveinfo.activityInfo.packageName;
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]
            String className = resolveinfo.activityInfo.name;
            // LAUNCHER Intent
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//重点是加这个

            // 设置ComponentName参数1:packagename参数2:MainActivity路径
            ComponentName cn = new ComponentName(packageName, className);
            intent.setComponent(cn);
            //startActivity(intent);
            mContext = getBaseContext();
            mContext.startActivity(intent);
        }
    }
}

页面布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ming.t.login">

        <TableLayout
            android:layout_width="344dp"
            android:layout_height="495dp"
            android:layout_centerHorizontal="true"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp">

            <EditText
                android:id="@+id/appPackage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/TopApp"
                android:layout_alignParentTop="true"
                android:layout_toStartOf="@+id/TopApp"
                android:ems="10"
                android:hint="此处输入APP包名"
                android:inputType="textPersonName"
                android:text="App包名" />

            <Button
                android:id="@+id/TopApp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="32dp"
                android:text="不断前置此APP"
                android:layout_marginTop="13dp"
                android:layout_below="@+id/editText"
                android:layout_alignParentStart="true"
                android:layout_marginStart="8dp" />

        </TableLayout>

</android.support.constraint.ConstraintLayout>

完成后,app界面如下:
monkey测试时,通过一个守护app,不断调起被测app(通过包名去调起)

相关推荐