package com.androcid.zomato.activity; 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.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.support.test.uiautomator.Until; import org.hamcrest.CoreMatchers; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; /** * Created by Androcid on 20-04-2017. */ @RunWith(AndroidJUnit4.class) @SdkSuppress(minSdkVersion = 18) public class UIAnimatorTest { private static final String BASIC_SAMPLE_PACKAGE = "com.androcid.zomato"; 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(); 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); } @Test public void checkPreconditions() { assertThat(mDevice, notNullValue()); } @Test public void testChangeText_sameActivity() { UiObject skipButton = mDevice.findObject(new UiSelector() .text("SKIP").className("android.widget.TextView")); // Simulate a user-click on the OK button, if found. try { if (skipButton.exists() && skipButton.isEnabled()) { skipButton.click(); } } catch (UiObjectNotFoundException e) { e.printStackTrace(); } } @Test public void testChangeText_newActivity() { // 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, "activityChangeTextBtn")) .click(); // Verify the test is displayed in the Ui UiObject2 changedText = mDevice .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "show_text_view")), 500 /* wait 500ms */); assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED))); } }