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

The following examples show how to use android.support.test.uiautomator.UiDevice#getInstance() . These examples are extracted from open source projects. 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 Project: AppCrawler   File: UiHelper.java    License: 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 2
@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 3
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 4
@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
@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 6
Source Project: twittererer   File: LoginActivityTest.java    License: 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 7
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 Project: SmoothClicker   File: ItSettingsActivity.java    License: 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 Project: SmoothClicker   File: AbstractTest.java    License: 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 10
Source Project: AppCrawler   File: UiHelper.java    License: 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 11
static void rotateDevice() {
  UiDevice uiDevice = UiDevice
      .getInstance(InstrumentationRegistry.getInstrumentation());
  try {
    uiDevice.setOrientationLeft();
    waitTime();
    uiDevice.setOrientationNatural();
    waitTime();
  } catch (RemoteException e) {
    e.printStackTrace();
  }
}
 
Example 12
Source Project: SmoothClicker   File: ItStatusBarNotifier.java    License: 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 13
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 14
public AutomatorServiceImpl() {

        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        this.uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        Device.getInstance().init(device, uiAutomation);
    }
 
Example 15
Source Project: UIAutomatorWD   File: UiAutomatorBridge.java    License: MIT License 4 votes vote down vote up
public static final UiDevice getUiDevice() {
    if (uiDevice == null) {
        uiDevice = UiDevice.getInstance();
    }
    return uiDevice;
}
 
Example 16
Source Project: Forage   File: CacheListTest.java    License: 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 17
@Before
public void setUp() {
    instrumentation = InstrumentationRegistry.getInstrumentation();
    activity = testRule.getActivity();
    uiDevice = UiDevice.getInstance(instrumentation);
}
 
Example 18
Source Project: AndroidProjects   File: DemoEspressoTest.java    License: MIT License 4 votes vote down vote up
@Before
public void before() {
    mDevice = UiDevice.getInstance(getInstrumentation());
    data = "开始测试,初始化操作";
    Log.e("Edwin", data);
}
 
Example 19
Source Project: AppCrawler   File: UiScreen.java    License: 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 20
@Override
protected void setUp() throws Exception
{
    super.setUp();

    testActivity = getActivity();

    bleManager = testActivity.getManager();

    bleAdapter = BluetoothAdapter.getDefaultAdapter();

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