How do you test an Android application across multiple Activities?

前端 未结 14 1720
醉梦人生
醉梦人生 2020-12-07 08:01

We are building a complex Android application consisting of many screens and workflows spread across many Activities. Our workflows are similar to what you might see on a Ba

相关标签:
14条回答
  • 2020-12-07 08:55

    You could always use Robotium. It supports blackbox testing just like Selenium but for Android. You will find it at Robotium.org

    0 讨论(0)
  • 2020-12-07 09:02

    This answer is based on the accepted answer but modified to solve the timing issue which for me became consistent after adding about a half dozen tests. @pajato1 gets the credit for solving the timing issue, as cited in the accepted answer comments.

    /**
     * Creates a test Activity for a given fully qualified test class name.
     *
     * @param fullyQualifiedClassName The fully qualified name of test activity class.
     *
     * @return The test activity object or null if it could not be located.
     */
    protected AbstractTestActivity getTestActivity(final String fullyQualifiedClassName) {
        AbstractTestActivity result = null;
    
        // Register our interest in the given activity and start it.
        Log.d(TAG, String.format("Running test (%s) with main class: %s.", getName(), fullyQualifiedClassName));
        instrumentation = getInstrumentation();
    
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClassName(instrumentation.getTargetContext(), fullyQualifiedClassName);
        // Wait for the activity to finish starting
        Activity activity = instrumentation.startActivitySync(intent);
    
        // Perform basic sanity checks.
        assertTrue("The activity is null!  Aborting.", activity != null);
        String format = "The test activity is of the wrong type (%s).";
        assertTrue(String.format(format, activity.getClass().getName()), activity.getClass().getName().equals(fullyQualifiedClassName));
        result = (AbstractTestActivity) activity;
    
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题