从设备运行Android测试项目

时间:2014-10-21 18:17:03

标签: android testing

我创建了一个android机器人测试项目,它在设备和模拟器上从eclipse成功运行。

即使测试应用程序在管理应用程序中出现,其图标也不会出现在主屏幕上。 我需要将它发送给潜在客户(不是程序员),它会安装它并直接从他的手机上运行,​​但如果没有图标可以运行它,他将如何运行它?

我尝试了各种各样的selutions,但我需要的东西不是技术客户。

这是主类的代码:

package genericTest.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Application;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.View;

import com.robotium.solo.Solo;

@SuppressWarnings("rawtypes")
public class Main extends ActivityInstrumentationTestCase2 {
    private FunctonsForViews mFunctonsForViews;
    private Random mRand;
    private Solo mSolo;
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.OnOApps.NRG.MainActivity";

    private final int DELAY = 50; 
    private final int SCREEN_SIZE = 1280;
    private final int ERROR_COUNT_LIMIT = 10;
    private final int CLICK_ON_LOOP_LIMIT = 8;
    private final int WHAITING_FOR_VIEWS_LIMIT = 10;

    private static Class launcherActivityClass;
    private static int error_count = 0;

    static {
        try {
            launcherActivityClass = Class
                    .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressLint("NewApi")
    @SuppressWarnings("unchecked")
    public Main() throws ClassNotFoundException {
        super(launcherActivityClass);

    }

    @SuppressWarnings("deprecation")
    protected void setUp() throws Exception {
        setActivityInitialTouchMode(true);
        mSolo = new Solo(getInstrumentation(), getActivity());

        Context context = getActivity();
        KeyguardManager km = 
                  (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        if (km.inKeyguardRestrictedInputMode())
        {
          KeyguardManager.KeyguardLock lock = km.newKeyguardLock("some_tag");
          lock.disableKeyguard();
          SystemClock.sleep(2000);
        }
        setActivityInitialTouchMode(true);
    }

    /*
     * runs the test for the app.
     */
    public void testMethod()
    {
        mFunctonsForViews = new FunctonsForViews(mSolo);
        mSolo.sleep(DELAY * DELAY);

        mRand = new Random();

        /*
         * the test will take place in the loop, and will be limit in time.
         * in every iteration it will get the vies in activity's, and run a test on a random view.
         */
        for(int i=0 ; ; i += DELAY)
        { 
            Log.i("starting eteration", "" + i);
            mSolo.unlockScreen();
            ArrayList Views = mSolo.getViews();
            int arraySize = Views.size();
            if (arraySize == 0)// now View in activity.
            {
                whenNoViewsInScreen(Views, arraySize);
            }
            if (arraySize != 0)
            {
                int ViewIndexInArray = mRand.nextInt(arraySize + 2);
                if (ViewIndexInArray == arraySize)
                {
                    mSolo.scrollDown();
                }

                else if (ViewIndexInArray == arraySize + 1)     
                {
                    if (!mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals
                            (LAUNCHER_ACTIVITY_FULL_CLASSNAME))
                            {
                                goingBack();
                            }
                }
                else
                {
                    View randomView = (View)(Views.get(ViewIndexInArray));
                    runTestOnOneView(randomView);
                }
            }
        }
    }

    /*
     * performing clicks onScreen()
     */
    public void myClickOnScreen()
    {
        try {
            mSolo.unlockScreen();
            mSolo.clickOnScreen(mRand.nextInt(SCREEN_SIZE), mRand.nextInt(SCREEN_SIZE));
        } catch (Exception e) {
            e.printStackTrace();
            error_count++;
        } catch (Error e2) {
            error_count++;
            e2.printStackTrace();
        }       
    }

    /*
     * there is no Views available.
     * we will try pressing on screen or the goBack function.
     */
    public void whenNoViewsInScreen(ArrayList Views, int arraySize)
    {
        for (int j = 0; j < WHAITING_FOR_VIEWS_LIMIT; j++)
        {
            for (int k= 0; k < CLICK_ON_LOOP_LIMIT; k++)
            {   
                myClickOnScreen();
            }

            Views = mSolo.getViews();
            arraySize = Views.size();
            if (arraySize != 0)
            {
                return;
            }
            mSolo.sleep(DELAY);
            Views = mSolo.getViews();
            arraySize = Views.size();
            if (arraySize != 0)
            {
                return;
            }
        }
        if (!mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals
                (LAUNCHER_ACTIVITY_FULL_CLASSNAME))
                {
                    goingBack();
                }
        mSolo.sleep(DELAY);
        return;
    }

    public void runTestOnOneView(View randomView)
    {
        String rawViewName = randomView.getClass().getName();
        String viewName = parseRawViewName(rawViewName);
        if (viewName.contains("ActionBarContainer"))
        {
            return;
        }
        MyRunnable  myRunnable = mFunctonsForViews.getMethodMap().get(viewName);
        try{
            if (myRunnable != null)
            {
                myRunnable.run((View)randomView);               
            }
            else // view not in map.
            {
                boolean inMap = false;
                Iterator it = mFunctonsForViews.getMethodMap().entrySet().iterator();
                /*
                 * iterating in case the View is a version of one of View in map
                 * example:
                 * View is "CustomEditText", and map contains o view "EditText".
                 */
                while (it.hasNext()) 
                {
                    Map.Entry pairs = (Map.Entry)it.next();
                    if (   viewName.contains((String)pairs.getKey())   )
                    {
                        inMap = true;
                        // next two lines changed
                        myRunnable = (MyRunnable)(pairs.getValue());
                        myRunnable.run((View)randomView);
                        break;
                    }
                }
                if (inMap == false)
                {

                    if (viewName.contains("Layout"))
                    {
                        return;
                    }
                    mSolo.clickOnView((View)randomView);
                }
                error_count = 0;
            }
        }catch(Exception exception)
        {
            exception.printStackTrace();
            if(error_count > ERROR_COUNT_LIMIT &&
                    !(mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals(LAUNCHER_ACTIVITY_FULL_CLASSNAME)))    
            {
                goingBack();
                error_count = 0;
            }
            return;

        }catch(Error error)
        {

            error.printStackTrace();
            error_count ++;
            if(error_count > ERROR_COUNT_LIMIT &&
                    !(mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals(LAUNCHER_ACTIVITY_FULL_CLASSNAME)))    
            {
                goingBack();
                error_count = 0;
            }
            return;
        }   
        mSolo.sleep(DELAY);
    }

    /*
     * performs a goBack command surrounded with catch/try
     */
    public void goingBack()
    {
        try {
            String currentActivity = mSolo.getCurrentActivity().getClass().toString();
            Log.e("in function before", "going back" );
            mSolo.goBack();
            if (mSolo.getCurrentActivity().getClass().toString().equals(currentActivity))
            {
                for (int i = 0; i< 20; i++)
                {
                    myClickOnScreen();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        } catch (Error e) {
            e.printStackTrace();
        }
    }

    /*
     * extract the name of View from raw View name.
     * example:
     * raw View name: android.widget.TextView
     * raw View name:TextView
     */
    public String parseRawViewName(String rawViewName)
    {
        if (rawViewName.contains(" "))
        {
            String [] array = rawViewName.split(" ");
            rawViewName = array [0];
        }

        if (rawViewName.contains(".") || rawViewName.contains("$"))
        {
            String [] array = rawViewName.split("\\.|$");
            rawViewName = array [array.length-1];
        }
        return rawViewName;
    }

    public void tearDown() throws Exception {
        mSolo.finishOpenedActivities();
    }
}

1 个答案:

答案 0 :(得分:0)

无法从主屏幕上的&#34;图标&#34;运行测试。测试由IDE使用InstrumentationRunner进行管理。因此,如果您想允许客户端在他的设备上运行测试,他还需要在Eclipse / Android Studio中签出项目并运行测试。

相关问题