How to run UiAutomator 2.0 tests from command line

In your projects base folder execute the following runTests.sh file:
adb install -r app/build/outputs/apk/app-debug.apk
adb install -r app/build/outputs/apk/app-debug-androidTest-unaligned.apk
# Grant permissions requested
adb shell pm grant com.example.test.uiautomatortest android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant com.example.test.uiautomatortest android.permission.WRITE_EXTERNAL_STORAGE
# Run Ui tests from test class
adb shell am instrument -w -e class com.example.test.uiautomatortest.UiTests com.example.test.uiautomatortest.test/android.support.test.runner.AndroidJUnitRunner
# Pull all screenshots from device
adb pull sdcard/Screenshots/ .
# Restore device
adb uninstall com.example.test.uiautomatortest
adb uninstall com.example.test.uiautomatortest.test

Remember :
chmod +x runTests.sh
./runTests.sh

Udgivet i Ikke kategoriseret | Kommentarer slået fra

How to take screenshots with UiAutomator 2.0 in Android Studio

Note, this example is a standalone test project.
Using Android Studio create a new project e.g. “com.example.test.uiautomatortest”
- Use option “Add no activity”
- Delete test project androidTest
- Remove resource folders from “res”
- Update manifest according to image below
- Update build.gradle for app according to image below
- Create your UiAutomater test class (UiTests.java here)
- Remember to update BASIC_SAMPLE_PACKAGE to your own app to test
- Run your tests right clicking test class or test case itself (inside class)

From a command prompt execute the following commands to grant the permissions from test app (Android 6.0 +).
This is needed once when device know the test package.
adb shell pm grant com.example.test.uiautomatortest android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant com.example.test.uiautomatortest android.permission.WRITE_EXTERNAL_STORAGE

Check permissions using :
adb shell dumpsys package com.example.test.uiautomatortest

Run your tests again. Now you can find your screenshots here : “/sdcard/Screenshots/”


build.gradle for app


AndroidManifest.xml

Pull your screenshot using :
adb pull sdcard/Screenshots/image0.png

UiTests.java :
package com.example.test.uiautomatortest;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.support.test.uiautomator.By;

import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;

import java.io.File;
import java.util.regex.Pattern;

/**
* Created by ohc on 16/10/2016.
*/

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)

public class UiTests {
private static final String BASIC_SAMPLE_PACKAGE = "com.yourown.app";
private static final int LAUNCH_TIMEOUT = 5000;
final static private String IMAGE_DIR = "/sdcard/Screenshots/";
final static private String IMAGE_NAME = "image";
private UiDevice mDevice;
private static int cnt = 0;
private int width = 1000; // Default width
private int height = 3000; // Default height

public UiTests() throws Exception {
super();
}

/**
* Wake up device and remove screenlock if needed
* Set portrait orientation
*/
@Before
public void setUp() throws Exception {
System.out.println("New testcase starts here ======================>");
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
Assert.assertThat(mDevice, CoreMatchers.notNullValue());

if (!mDevice.isScreenOn()) {
// Just in case, starting up from scratch
mDevice.wakeUp();
sleep(3000);
// Lockscreen pattern for clock
Pattern p = Pattern.compile("[0,1,2]\\d:[0,1,2,3,4,5]\\d");
UiObject2 lockscreen = mDevice.findObject(By.text(p));
if (lockscreen != null) {
swipeUp();
}
sleep(1000);
}
// Default is natural orientation
mDevice.setOrientationNatural();
// Start from the home screen
mDevice.pressHome();

try {
// Wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
Assert.assertThat(launcherPackage, CoreMatchers.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);
}
catch (Exception e) {
System.out.println("Launch failed ================>");
e.printStackTrace();
}
System.out.println("Launch successfull ================>");
}

@After
public void tearDown() throws Exception {
// Default is natural orientation (if a testcase fails)
mDevice.setOrientationNatural();
mDevice.pressHome();
System.out.println("Testcase stops here <===========================");
}

/**
* Take a screenshot and store on sdcard
*
*/
@org.junit.Test
public void testHomeScreens() {
getScreenShot();
}

public void getScreenShot() {
sleep(500); // Wait for screen to change
File imageDir = new File(IMAGE_DIR);
if (!imageDir.exists()) imageDir.mkdir();
mDevice.takeScreenshot(new File(IMAGE_DIR + IMAGE_NAME + Integer.toString(cnt++) + ".png"), 0.5f, 25);
System.out.println("Screenshot #" + Integer.toString(cnt - 1));
}

// Used e.g. when unlocking screen
public void swipeUp() {
mDevice.swipe(width / 2, height*4/5, width/2, height/5, 10);
}

private void sleep(int time) {
try {
Thread.sleep(time);
}
catch(Exception e) {
e.printStackTrace();
}
}
}

Udgivet i Ikke kategoriseret | Kommentarer slået fra