From ef38d5d6058fbfb64f68cdb9af61409476e0c08b Mon Sep 17 00:00:00 2001 From: Quddus Chong Date: Tue, 10 Nov 2015 14:15:00 -0800 Subject: [PATCH] docs: Updates to UI testing training docs: * Fixed code snippet in UI Automator training. * Updated build.gradle configuration to use latest Testing Support Library APIs. bug: 25195089 Change-Id: I7cf62ef0ff42e58335debdca5fb285fe0a902585 --- .../testing/ui-testing/espresso-testing.jd | 7 +- .../testing/ui-testing/uiautomator-testing.jd | 72 +++++++++++++++---- .../unit-testing/instrumented-unit-tests.jd | 7 +- 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/docs/html/training/testing/ui-testing/espresso-testing.jd b/docs/html/training/testing/ui-testing/espresso-testing.jd index 01e74b06878b4..72689ff49f1f9 100644 --- a/docs/html/training/testing/ui-testing/espresso-testing.jd +++ b/docs/html/training/testing/ui-testing/espresso-testing.jd @@ -120,9 +120,12 @@ trainingnavtop=true
 dependencies {
-    androidTestCompile 'com.android.support.test:runner:0.4'
-    androidTestCompile 'com.android.support.test:rules:0.4'
+    androidTestCompile 'com.android.support:support-annotations:23.0.1'
+    androidTestCompile 'com.android.support.test:runner:0.4.1'
+    androidTestCompile 'com.android.support.test:rules:0.4.1'
     androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
+    // Set this dependency if you want to use Hamcrest matching
+    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
 }
 
diff --git a/docs/html/training/testing/ui-testing/uiautomator-testing.jd b/docs/html/training/testing/ui-testing/uiautomator-testing.jd index a0050a8c82dd4..ea15d8b2d1f32 100644 --- a/docs/html/training/testing/ui-testing/uiautomator-testing.jd +++ b/docs/html/training/testing/ui-testing/uiautomator-testing.jd @@ -89,9 +89,11 @@ test runner.
 dependencies {
-    androidTestCompile 'com.android.support.test:runner:0.3'
-    androidTestCompile 'com.android.support.test:rules:0.3'
+    androidTestCompile 'com.android.support:support-annotations:23.0.1'
+    androidTestCompile 'com.android.support.test:runner:0.4.1'
     androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
+    // Set this dependency if you want to use Hamcrest matching
+    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
 }
 
@@ -186,9 +188,21 @@ extend an existing container implementation

Create a UI Automator Test Class

-

To build a UI Automator test, create a class that extends -{@link android.test.InstrumentationTestCase}. Implement the following programming model in your -UI Automator test class:

+

+Your UI Automator test class should be written the same way as a JUnit 4 test class. To learn more +about creating JUnit 4 test classes and using JUnit 4 assertions and annotations, see + +Create an Instrumented Unit Test Class. +

+

Add the {@code @RunWith(AndroidJUnit4.class)} annotation at the beginning of your test class +definition. You also need to specify the + +{@code AndroidJUnitRunner} class +provided in the Android Testing Support Library as your default test runner. This step is described +in more detail in Run UI Automator Tests on a Device or Emulator. +

+ +

Implement the following programming model in your UI Automator test class:

  1. Get a @@ -241,21 +255,52 @@ the UI Automator API to select and interact with specific UI elements.

    and simulate a Home button press:

    -import android.test.InstrumentationTestCase;
    +import org.junit.Before;
    +import android.support.test.runner.AndroidJUnit4;
    +import android.support.test.InstrumentationRegistry;
     import android.support.test.uiautomator.UiDevice;
     import android.support.test.uiautomator.By;
    +import android.support.test.uiautomator.Until;
    +import android.content.Context;
    +import android.content.Intent;
    +import android.content.pm.PackageManager;
    +import static org.junit.Assert.assertThat;
     
    -public class CalculatorUiTest extends InstrumentationTestCase {
    +@RunWith(AndroidJUnit4.class)
    +@SdkSuppress(minSdkVersion = 18)
    +public class ChangeTextBehaviorTest {
     
    +    private static final String BASIC_SAMPLE_PACKAGE
    +            = "com.example.android.testing.uiautomator.BasicSample";
    +    private static final int LAUNCH_TIMEOUT = 5000;
    +    private static final String STRING_TO_BE_TYPED = "UiAutomator";
         private UiDevice mDevice;
     
    -    public void setUp() {
    +    @Before
    +    public void startMainActivityFromHomeScreen() {
             // Initialize UiDevice instance
    -        mDevice = UiDevice.getInstance(getInstrumentation());
    +        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
     
             // Start from the home screen
             mDevice.pressHome();
    -        mDevice.wait(Until.hasObject(By.pkg(getHomeScreenPackage()).depth(0)),
    +
    +        // Wait for launcher
    +        final String launcherPackage = mDevice.getLauncherPackageName();
    +        assertThat(launcherPackage, notNullValue());
    +        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
    +                LAUNCH_TIMEOUT);
    +
    +        // Launch the app
    +        Context context = InstrumentationRegistry.getContext();
    +        final Intent intent = context.getPackageManager()
    +                .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    +        // Clear out any previous instances
    +        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    +        context.startActivity(intent);
    +
    +        // Wait for the app to appear
    +        mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
    +                LAUNCH_TIMEOUT);
         }
     }
     
    @@ -292,8 +337,7 @@ if(okButton.exists() && okButton.isEnabled()) {

    Specifying a selector

    If you want to access a specific UI component in an app, use the {@code UiSelector} -class. This class represents a query for specific elements in the -currently displayed UI.

    +class. This class represents a query for specific elements in the currently displayed UI.

    If more than one matching element is found, the first matching element in the layout hierarchy is returned as the target @@ -306,8 +350,8 @@ found, a {@code UiAutomatorObjectNotFoundException} is thrown.

    You can use the -{@code childSelector()} -method to nest multiple + +{@code childSelector()} method to nest multiple {@code UiSelector} instances. For example, the following code example shows how your test might specify a search to find the first {@link android.widget.ListView} in the currently displayed UI, then search within that diff --git a/docs/html/training/testing/unit-testing/instrumented-unit-tests.jd b/docs/html/training/testing/unit-testing/instrumented-unit-tests.jd index eb159dff7fab2..db4cc8ca395f2 100644 --- a/docs/html/training/testing/unit-testing/instrumented-unit-tests.jd +++ b/docs/html/training/testing/unit-testing/instrumented-unit-tests.jd @@ -76,10 +76,11 @@ and supporting APIs, such as the Android Testing Support Library.

     dependencies {
    -    androidTestCompile 'com.android.support.test:runner:0.3'
    -    androidTestCompile 'com.android.support.test:rules:0.3'
    +    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    +    androidTestCompile 'com.android.support.test:runner:0.4.1'
    +    androidTestCompile 'com.android.support.test:rules:0.4.1'
         // Set this dependency if you want to use Hamcrest matching
    -    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    +    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
     }