Java Code Examples for android.support.test.uiautomator.UiDevice#getInstance()

The following examples show how to use android.support.test.uiautomator.UiDevice#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PermissionsTest.java    From espresso-samples with Apache License 2.0 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    // Initialize UiDevice instance
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    device.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    MatcherAssert.assertThat(launcherPackage, IsNull.notNullValue());
    device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager()
                                 .getLaunchIntentForPackage(APP_PACKAGE_NAME);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    device.wait(Until.hasObject(By.pkg(APP_PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT);
}
 
Example 2
Source File: BasicUITests.java    From restcomm-android-sdk with GNU Affero General Public License v3.0 6 votes vote down vote up
private void allowPermissionsIfNeeded() {
    if (Build.VERSION.SDK_INT >= 23) {
        // Initialize UiDevice instance
        UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // notice that we typically receive 2-3 permission prompts, hence the loop
        while (true) {
            UiObject allowPermissions = uiDevice.findObject(new UiSelector().text("Allow"));
            if (allowPermissions.exists()) {
                try {
                    allowPermissions.click();
                } catch (UiObjectNotFoundException e) {
                    e.printStackTrace();
                    Log.e(TAG, "There is no permissions dialog to interact with ");
                }
            } else {
                break;
            }
        }
    }
}
 
Example 3
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
Example 4
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
Example 5
Source File: LoginActivityTest.java    From twittererer with Apache License 2.0 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    device.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(APP_PACKAGE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    device.wait(Until.hasObject(By.pkg(APP_PACKAGE).depth(0)), TIMEOUT);
}
 
Example 6
Source File: UiHelper.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
public static boolean handleCommonDialog() {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject button = null;
    for (String keyword : Config.COMMON_BUTTONS) {
        button = device.findObject(new UiSelector().text(keyword).enabled(true));
        if (button != null && button.exists()) {
            break;
        }
    }
    try {
        // sometimes it takes a while for the OK button to become enabled
        if (button != null && button.exists()) {
            button.waitForExists(5000);
            button.click();
            Log.i("AppCrawlerAction", "{Click} " + button.getText() + " Button succeeded");
            return true; // triggered
        }
    } catch (UiObjectNotFoundException e) {
        Log.w(TAG, "UiObject disappear");
    }
    return false; // no trigger
}
 
Example 7
Source File: PermissionGranter.java    From mobile-android-samples with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void allowPermissionsIfNeeded() {
    try {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            sleep(PERMISSIONS_DIALOG_DELAY);
            UiDevice device = UiDevice.getInstance(getInstrumentation());
            UiSelector selector = new UiSelector()
                    .clickable(true)
                    .checkable(false)
                    .index(GRANT_BUTTON_INDEX);
            UiObject allowPermissions = device.findObject(selector);

            if (allowPermissions.exists()) {
                allowPermissions.click();
            }
        }

    } catch (UiObjectNotFoundException e) {
        System.out.println("There is no permissions dialog to interact with");
    }
}
 
Example 8
Source File: ItSettingsActivity.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * Starts the main activity to test from the home screen
 */
@Before
public void startMainActivityFromHomeScreen() {

    l(this, "@Before startMainActivityFromHomeScreen");

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(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_MS);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_APP_PATH);

    // 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(PACKAGE_APP_PATH).depth(0)), LAUNCH_TIMEOUT_MS);

    // Prepare for tests
    openSettingsScreenFromMenu();

}
 
Example 9
Source File: PermissionGranter.java    From NoNonsense-FilePicker with Mozilla Public License 2.0 5 votes vote down vote up
public static void allowPermissionsIfNeeded(Activity activity, String permissionNeeded) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(activity, permissionNeeded)) {
            sleep(PERMISSIONS_DIALOG_DELAY);
            UiDevice device = UiDevice.getInstance(getInstrumentation());
            UiObject allowPermissions = device.findObject(new UiSelector().clickable(true).index(GRANT_BUTTON_INDEX));
            if (allowPermissions.exists()) {
                allowPermissions.click();
            }
        }
    } catch (UiObjectNotFoundException e) {
        System.out.println("There is no permissions dialog to interact with");
    }
}
 
Example 10
Source File: AbstractTest.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * <i>The device should be at its initial state when all tests are done</i>
 */
@AfterClass
public static void end(){
    l("AbstractTest","@AfterClass end");
    UiDevice d = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    d.pressHome();
}
 
Example 11
Source File: UiHelper.java    From AppCrawler with Apache License 2.0 5 votes vote down vote up
public static boolean isInTheSameScreen(UiScreen target) {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject root = device.findObject(new UiSelector().packageName(Config.sTargetPackage));
    if (root == null || !root.exists()) {
        Log.e(TAG, "Fail to get screen root object");
        return false;
    }
    UiScreen current = new UiScreen(null, null, root);
    boolean result = current.equals(target);
    return result;
}
 
Example 12
Source File: UiAutomatorHelper.java    From SwipeCoordinator with Apache License 2.0 5 votes vote down vote up
static void rotateDevice() {
  UiDevice uiDevice = UiDevice
      .getInstance(InstrumentationRegistry.getInstrumentation());
  try {
    uiDevice.setOrientationLeft();
    waitTime();
    uiDevice.setOrientationNatural();
    waitTime();
  } catch (RemoteException e) {
    e.printStackTrace();
  }
}
 
Example 13
Source File: ItStatusBarNotifier.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * Initializes the NotificationManager
 *
 * <i>The tests have to start on the home screen</i>
 */
@Before
public void init(){
    l(this,"@Before init");
    mContext = mActivityRule.getActivity().getApplicationContext();
    mSbn = new StatusBarNotifier(mContext);
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();
}
 
Example 14
Source File: UiScreen.java    From AppCrawler with Apache License 2.0 4 votes vote down vote up
public UiScreen(UiScreen parent, UiWidget widget) {
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject root = device.findObject(new UiSelector().index(0));
    init(parent, widget, root);
}
 
Example 15
Source File: DemoEspressoTest.java    From AndroidProjects with MIT License 4 votes vote down vote up
@Before
public void before() {
    mDevice = UiDevice.getInstance(getInstrumentation());
    data = "开始测试,初始化操作";
    Log.e("Edwin", data);
}
 
Example 16
Source File: SupportFragmentHelperTest.java    From OPFIab with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    instrumentation = InstrumentationRegistry.getInstrumentation();
    activity = testRule.getActivity();
    uiDevice = UiDevice.getInstance(instrumentation);
}
 
Example 17
Source File: CacheListTest.java    From Forage with Mozilla Public License 2.0 4 votes vote down vote up
@Before
public void beforeEachTest() {
    cacheListScreen = new CacheListScreen();
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    databaseInteractor = TestUtils.app().getComponent().databaseInteractor();
}
 
Example 18
Source File: UiAutomatorBridge.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
public static final UiDevice getUiDevice() {
    if (uiDevice == null) {
        uiDevice = UiDevice.getInstance();
    }
    return uiDevice;
}
 
Example 19
Source File: AutomatorServiceImpl.java    From android-uiautomator-server with MIT License 4 votes vote down vote up
public AutomatorServiceImpl() {

        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        this.uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        Device.getInstance().init(device, uiAutomation);
    }
 
Example 20
Source File: ABluetoothPowerTest.java    From SweetBlue with GNU General Public License v3.0 3 votes vote down vote up
@Override
protected void setUp() throws Exception
{
    super.setUp();

    testActivity = getActivity();

    bleManager = testActivity.getManager();

    bleAdapter = BluetoothAdapter.getDefaultAdapter();

    uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}