monkey測試時,經過一個守護app,不斷調起被測app(經過包名去調起)

問題:在作安卓隨機壓力測試時,被測APP常常跑出去,被別的app擋住,此時極可能出現斷網,截圖也截取了別的應用圖,比較影響壓力測試。
如何解決上面的問題呢?
思路能夠有以下幾種:
一,測試執行時,不斷採用adb 命令,啓動app
二,開發一個app,持續不斷調起被測app,經過包名啓動它
開發工具:android studio
用處:
1.經過包名啓動其它app;
2.自動化測試時,有時候須要保證被測APP處於活動狀態,而不是在後臺。java

代碼以下:android

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);
        }
    }
}

頁面佈局文件以下:app

<?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界面以下:
圖片描述ide

相關文章
相關標籤/搜索