Ui Automator 2.0 project in Android Studio

I want to create a project in Android Studio. However, I do not want an Android app, just a test project.

Following the latest release of UiAutomator , I tried to install a class that extends ActivityInstrumentationTestCase2 and start tests from it.

However, I came across one thing: I can’t understand how to create a project without making it an application.

Possible options for creating a new project:

  • Launch a new Android Studio project
  • Open existing projects
  • Import Projects

I did:

  • Launch a new project, give it a name, install minSDK and select "No Activity"
  • Open build.gradle (in the appendix) and add the dependency and tooling information mentioned at the end of Testing the support library .
  • Opened androidTest under src and changed the main file: changed to ActivityInstrumentationTestCase2, added setUp and tearDown; defined by RunWith Junit4 (as indicated in Testing a Support Library )
  • I'm building a project (creating successful) - click the green arrow next to the line in the action bar

My problems:

  • How to install this in the device?
  • How to run it on the device?
  • Does anything need to be done in AndroidManifest?
  • Am I editing in the right place? Should I do anything under src / main?

I would be grateful that the installation and launch instructions would be in order to do this through Android Studio, and using the command line (if you only know that one of them sent it, please).

Note: this is the first time I use Android Studio

Thanks in advance.

EDIT:

Now I can create and run, but it tells me that I have no tests to run (empty test suite). Here is my hail and my code.

My build.graddle looks like this:

apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "21.1.2" defaultConfig { applicationId "androidexp.com.ceninhas" minSdkVersion 21 targetSdkVersion 22 versionCode 1 versionName "1.0" testInstrumentationRunner="android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions { exclude 'LICENSE.txt' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile 'com.android.support.test:testing-support-lib:0.1' androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0' } 

My source code (under src / androidTest / java / package):

 @RunWith(AndroidJUnit4.class) public class ApplicationTest extends ActivityInstrumentationTestCase2<Activity> { public final static String ACTIVITY_NAME = "com.calculator.Main"; public final static Class<?> autActivityClass; static { try { autActivityClass = Class.forName(ACTIVITY_NAME); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } public ApplicationTest(){ super((Class<Activity>)autActivityClass); } @Before public void setUp() throws Exception{ super.setUp(); injectInstrumentation(InstrumentationRegistry.getInstrumentation()); } @After public void tearDown() throws Exception{ super.tearDown(); } @Test public void cenas(){ assertTrue(true); } } 

Launch log on console:

 Testing started at 18:06 ... Waiting for device. Target device: lge-nexus_5-08e506c10ddef123 Uploading file local path: C:\Users\Ines\workspace\Ceninhas\app\build\outputs\apk\app-debug.apk remote path: /data/local/tmp/androidexp.com.ceninhas No apk changes detected. Skipping file upload, force stopping package instead. DEVICE SHELL COMMAND: am force-stop androidexp.com.ceninhas Uploading file local path: C:\Users\Ines\workspace\Ceninhas\app\build\outputs\apk\app-debug-androidTest-unaligned.apk remote path: /data/local/tmp/androidexp.com.ceninhas.test No apk changes detected. Skipping file upload, force stopping package instead. DEVICE SHELL COMMAND: am force-stop androidexp.com.ceninhas.test Running tests Test running startedFinish Empty test suite. 

What am I doing wrong?

+7
android android-studio android-uiautomator uiautomator android-instrumentation
source share
2 answers

I also use uiautomator 2.0 from AndroidStudio. Here are some answers to your questions.

How to install it on the device? How to run it in the device?

Make sure your device is connected using

 adb devices 

If not, you should connect it using

 adb kill-server adb connect xxx.xxx.xxx.xxx 

Then from AndroidStudio, right-click on your test class and click "Run Program."

Does anything need to be done in AndroidManifest?

I have nothing special in my manifest, but be sure to add

 android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } } 

in build.gradle file

Am I editing in the right place? Do I have to do something under src / main?

Yes, you are editing in the right place. But you can move your code to src/main . To do this, you will need to change androidTestCompile to compile in the build.gradle file.

I have not tried to run the test from the command line yet, but you can see the AndroidStudio commands, maybe this can help.

Hope this helps you.

EDIT 1

I am using this code

build.gradle (projectRoot)

 apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.0" lintOptions { abortOnError false } packagingOptions { exclude 'NOTICE' exclude 'LICENSE.txt' } defaultConfig { minSdkVersion 19 targetSdkVersion 22 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' compile 'com.android.support.test:testing-support-lib:0.1' compile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0' compile project(':aiccore') } 

LoginTestCase (projectRoot / src / main / LoginTestCase.java)

 public class LoginTestCase extends InstrumentationTestCase { protected UiDevice device = null; protected String appName; public LoginTestCase() { this("YourAppName") } public LoginTestCase(String appName) { this.appName = appName; } public void runApp(String appName) throws UiObjectNotFoundException, RemoteException { device = UiDevice.getInstance(getInstrumentation()); device.pressHome(); device.waitForWindowUpdate("", 2000); UiObject2 allAppsButton = device.findObject(By.desc("Apps")); allAppsButton.click(); device.waitForWindowUpdate("", 2000); UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true)); appViews.setAsHorizontalList(); UiObject settingsApp = appViews.getChildByText(new UiSelector().className(TextView.class.getName()), appName); settingsApp.clickAndWaitForNewWindow(); assertTrue("Unable to detect app", settingsApp != null); } @Override public void setUp() throws RemoteException, UiObjectNotFoundException { this.runApp(appName); } @Override public void tearDown() throws RemoteException, UiObjectNotFoundException { //Empty for the moment } public void testUS1() { UiObject2 usernameLabel = device.findObject(By.clazz(TextView.class.getName()).text("Username")); assertTrue("Username label not found", usernameLabel != null); } 
0
source share

Well, in fact, you should not write test code this way. Just save your code in the src / androidTest folder and write the test code as follows:

 @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; @Before public void startMainActivityFromHomeScreen() { // Initialize UiDevice instance mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // Start from the home screen mDevice.pressHome(); // 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); } @Test public void checkPreconditions() { assertThat(mDevice, notNullValue()); } @Test public void testChangeText_sameActivity() { // Type text and then press the button. mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput")) .setText(STRING_TO_BE_TYPED); mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "changeTextBt")) .click(); // Verify the test is displayed in the Ui UiObject2 changedText = mDevice .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "textToBeChanged")), 500 /* wait 500ms */); assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED))); } } 

Look in detail: UIAutomator test example

0
source share

All Articles