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

The following are Jave code examples for showing how to use findObject() of the android.support.test.uiautomator.UiDevice class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ChimpCheck   File: PermissionGranter.java   View Source Code Vote up 7 votes
public static void allowPermissionsIfNeeded(String permissionNeeded) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(permissionNeeded)) {
            sleep(PERMISSIONS_DIALOG_DELAY);
            UiDevice device = UiDevice.getInstance(getInstrumentation());
            UiObject allowPermissions = device.findObject(new UiSelector()
                    .clickable(true)
                    .checkable(false)
                    .index(GRANT_BUTTON_INDEX));
            if (allowPermissions.exists()) {
                allowPermissions.click();
            }
        }
    } catch (UiObjectNotFoundException e) {
        System.out.println("There is no permissions dialog to interact with");
    }
}
 
Example 2
Project: SimpleMarkdown   File: MainActivityTests.java   View Source Code Vote up 7 votes
@Test
public void openAppTest() throws Exception {
    UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
    mDevice.pressHome();
    // Bring up the default launcher by searching for a UI component
    // that matches the content description for the launcher button.
    UiObject allAppsButton = mDevice
            .findObject(new UiSelector().description("Apps"));

    // Perform a click on the button to load the launcher.
    allAppsButton.clickAndWaitForNewWindow();
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.wbrawner.simplemarkdown", appContext.getPackageName());
    UiScrollable appView = new UiScrollable(new UiSelector().scrollable(true));
    UiSelector simpleMarkdownSelector = new UiSelector().text("Simple Markdown");
    appView.scrollIntoView(simpleMarkdownSelector);
    mDevice.findObject(simpleMarkdownSelector).clickAndWaitForNewWindow();
}
 
Example 3
Project: ibeacon-scanner-android   File: PermissionTestHelper.java   View Source Code Vote up 7 votes
/**
 * Allows location permission if the runtime permission dialog is shown.
 */
public static void allowLocationPermissionWhenAsked()
{
    try
    {
        if (!PermissionUtils.isLocationGranted(InstrumentationRegistry.getTargetContext()))
        {
            TestHelper.sleep(PERMISSIONS_DIALOG_DELAY);

            final UiDevice device = UiDevice.getInstance(getInstrumentation());
            final UiObject allowPermissions = device.findObject(new UiSelector()
                                                                        .clickable(true)
                                                                        .checkable(false)
                                                                        .index(GRANT_BUTTON_INDEX));
            if (allowPermissions.exists())
            {
                allowPermissions.click();
            }
        }
    }
    catch (final UiObjectNotFoundException e)
    {
        Log.e(PermissionTestHelper.class.getSimpleName(), "There is no permissions dialog to interact with.");
    }
}
 
Example 4
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 7 votes
public static boolean viewExistsContains(UiDevice device, String... textToMatch) throws UiObjectNotFoundException
{
    if (textToMatch == null || textToMatch.length < 1)
    {
        return false;
    }
    UiObject view;
    boolean contains = false;
    for (int i = 0; i < textToMatch.length; i++)
    {
        view = device.findObject(new UiSelector().textContains(textToMatch[i]));
        contains = view.exists();
        if (contains) return true;
    }
    return false;
}
 
Example 5
Project: android-testing-guide   File: MainActivityTest.java   View Source Code Vote up 7 votes
@Ignore
@Test
public void testUiAutomatorAPI() throws UiObjectNotFoundException, InterruptedException {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    UiSelector editTextSelector = new UiSelector().className("android.widget.EditText").text("this is a test").focusable(true);
    UiObject editTextWidget = device.findObject(editTextSelector);
    editTextWidget.setText("this is new text");

    Thread.sleep(2000);

    UiSelector buttonSelector = new UiSelector().className("android.widget.Button").text("CLICK ME").clickable(true);
    UiObject buttonWidget = device.findObject(buttonSelector);
    buttonWidget.click();

    Thread.sleep(2000);
}
 
Example 6
Project: Demos   File: UiTest.java   View Source Code Vote up 6 votes
/**
 * 测试CollapsingToolbarLayout
 * 被测Demo下载地址:https://github.com/alidili/DesignSupportDemo
 *
 * @throws UiObjectNotFoundException
 */
public void testA() throws UiObjectNotFoundException {
    // 获取设备对象
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    UiDevice uiDevice = UiDevice.getInstance(instrumentation);
    // 获取上下文
    Context context = instrumentation.getContext();

    // 启动测试App
    Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.yang.designsupportdemo");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // 打开CollapsingToolbarLayout
    String resourceId = "com.yang.designsupportdemo:id/CollapsingToolbarLayout";
    UiObject collapsingToolbarLayout = uiDevice.findObject(new UiSelector().resourceId(resourceId));
    collapsingToolbarLayout.click();

    for (int i = 0; i < 5; i++) {
        // 向上移动
        uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(),
                uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2, 10);

        // 向下移动
        uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2,
                uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(), 10);
    }

    // 点击应用返回按钮
    UiObject back = uiDevice.findObject(new UiSelector().description("Navigate up"));
    back.click();

    // 点击设备返回按钮
    uiDevice.pressBack();
}
 
Example 7
Project: Demos   File: UiTest.java   View Source Code Vote up 6 votes
/**
 * 滑动界面,打开About phone选项
 * 测试环境为标准Android 7.1.1版本,不同设备控件查找方式会有不同
 *
 * @throws UiObjectNotFoundException
 */
public void testB() throws UiObjectNotFoundException {
    // 获取设备对象
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    UiDevice uiDevice = UiDevice.getInstance(instrumentation);
    // 获取上下文
    Context context = instrumentation.getContext();

    // 点击Settings按钮
    UiObject uiObject = uiDevice.findObject(new UiSelector().description("Settings"));
    uiObject.click();

    // 滑动列表到最后,点击About phone选项
    UiScrollable settings = new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView"));
    UiObject about = settings.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "About phone");
    about.click();

    // 点击设备返回按钮
    uiDevice.pressBack();
    uiDevice.pressBack();
}
 
Example 8
Project: civify-app   File: CreateIssueActivityTest.java   View Source Code Vote up 5 votes
private static void takeCameraPhoto() throws UiObjectNotFoundException {
    UiDevice device = UiDevice.getInstance(getInstrumentation());
    UiSelector shutterSelector =
            new UiSelector().resourceId("com.android.camera:id/shutter_button");
    UiObject shutterButton = device.findObject(shutterSelector);
    shutterButton.click();

    UiSelector doneSelector = new UiSelector().resourceId("com.android.camera:id/btn_done");
    UiObject doneButton = device.findObject(doneSelector);
    doneButton.click();
}
 
Example 9
Project: ChimpCheck   File: PermissionGranter.java   View Source Code Vote up 5 votes
public static void allowPermissionsIfNeeded() {
    if (Build.VERSION.SDK_INT >= 23) {
        UiDevice device = UiDevice.getInstance(getInstrumentation());
        UiObject allowPermissions = device.findObject(new UiSelector().text("Allow"));
        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException e) {
                Log.e(e.getStackTrace()[0].toString(), "There is no permissions dialog to interact with ");
            }
        }
    }
}
 
Example 10
Project: UIAutomatorWD   File: UIAutomatorWD.java   View Source Code Vote up 5 votes
public void skipPermission(JSONArray permissionPatterns, int scanningCount) {
    UiDevice mDevice = Elements.getGlobal().getmDevice();

    // if permission list is empty, avoid execution
    if (permissionPatterns.size() == 0) {
        return;
    }

    // regular check for permission scanning
    try {
        for (int i = 0; i < scanningCount; i++) {
            inner:
            for (int j = 0; j < permissionPatterns.size(); j++) {
                String text = permissionPatterns.getString(j);
                UiObject object = mDevice.findObject(new UiSelector().text(text));
                if (object.exists()) {
                    object.click();
                    break inner;
                }
            }

            Thread.sleep(3000);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause().toString());
    }
}
 
Example 11
Project: Leku   File: PermissionGranter.java   View Source Code Vote up 5 votes
public void denyPermissionsIfNeeded(Activity activity, String permissionNeeded) {
  try {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(activity, permissionNeeded)) {
      waitInMillis(PERMISSIONS_DIALOG_DELAY);
      UiDevice device = UiDevice.getInstance(getInstrumentation());
      UiObject denyPermissions = device.findObject(new UiSelector().clickable(true).index(DENY_BUTTON_INDEX));
      if (denyPermissions.exists()) {
        denyPermissions.click();
      }
    }
  } catch (UiObjectNotFoundException e) {
    logNoPermissionDialogError();
  }
}
 
Example 12
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 5 votes
/**
 * Clicks a button with the given text. This will try to find the widget exactly as the text is given, and
 * will try upper casing the text if it is not found.
 */
public static void clickButton(UiDevice device, String buttonText) throws UiObjectNotFoundException
{
    UiObject accept = device.findObject(new UiSelector().text(buttonText));
    if (accept.exists())
    {
        accept.click();
        return;
    }
    accept = device.findObject(new UiSelector().text(buttonText.toUpperCase()));
    accept.click();
}
 
Example 13
Project: Android-BlogExample   File: MainFragmentExtendTest.java   View Source Code Vote up 5 votes
private void assertViewWithTextIsVisible(UiDevice device, String text) throws UiObjectNotFoundException {
    UiObject allowButton = device.findObject(new UiSelector().text(text));
    if (!allowButton.exists()) {
        throw new AssertionError("View with text <" + text + "> not found!");
    }
    allowButton.click();
}
 
Example 14
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 5 votes
/**
 * Clicks a button with the given text. This will try to find the widget exactly as the text is given, and
 * will try upper casing the text if it is not found.
 */
public static void clickButton(UiDevice device, String buttonText) throws UiObjectNotFoundException
{
    UiObject accept = device.findObject(new UiSelector().text(buttonText));
    if (accept.exists())
    {
        accept.click();
        return;
    }
    accept = device.findObject(new UiSelector().text(buttonText.toUpperCase()));
    accept.click();
}
 
Example 15
Project: espresso-macchiato   File: EspPermissionDialog.java   View Source Code Vote up 5 votes
protected void click(String targetId) {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject button = device.findObject(new UiSelector().resourceId(targetId));

    try {
        button.waitForExists(3000);
        button.click();
    } catch (UiObjectNotFoundException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 16
Project: Forage   File: UiAutomatorUtils.java   View Source Code Vote up 5 votes
public static void allowPermissionsIfNeeded(UiDevice device) {
    if (Build.VERSION.SDK_INT >= 23) {
        UiObject allowPermissions = device.findObject(new UiSelector().text(TEXT_ALLOW));

        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException ignored) {
            }
        }
    }
}
 
Example 17
Project: bigbang   File: SingleActivityInstrumentationTest.java   View Source Code Vote up 5 votes
protected void allowPermissionsIfNeeded() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    UiDevice device = UiDevice.getInstance(getInstrumentation());
    UiObject allowPermissions = device.findObject(new UiSelector().clickable(true).index(1));
    if (allowPermissions.exists()) {
      try {
        allowPermissions.click();
      } catch (UiObjectNotFoundException e) {
        Timber.e(e, "There is no permissions dialog to interact with ");
      }
    }
  }
}
 
Example 18
Project: RxPermission   File: PermissionActivityTest.java   View Source Code Vote up 4 votes
private static UiObject getButton(final String text) {
  final UiDevice device = UiDevice.getInstance(getInstrumentation());
  return device.findObject(new UiSelector().text(text));
}
 
Example 19
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 4 votes
public static boolean turnOnPermissionDialogShowing(UiDevice device)
{
    UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
    return allowButton.exists();
}
 
Example 20
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 4 votes
public static void allowPermission(UiDevice device) throws UiObjectNotFoundException
{
    UiObject allow = device.findObject(new UiSelector().text(TEXT_ALLOW));
    allow.click();
}
 
Example 21
Project: android-testing-runtime-permissions   File: UiAutomatorUtils.java   View Source Code Vote up 4 votes
public static void openPermissions(UiDevice device) throws UiObjectNotFoundException {
    UiObject permissions = device.findObject(new UiSelector().text(TEXT_PERMISSIONS));
    permissions.click();
}
 
Example 22
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 4 votes
public static boolean viewExistsContains(UiDevice device, String textToMatch) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().textContains(textToMatch));
    return view.exists();
}
 
Example 23
Project: android-testing-runtime-permissions   File: UiAutomatorUtils.java   View Source Code Vote up 4 votes
public static void allowCurrentPermission(UiDevice device) throws UiObjectNotFoundException {
    UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
    allowButton.click();
}
 
Example 24
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 4 votes
public static void denyPermission(UiDevice device) throws UiObjectNotFoundException
{
    UiObject deny = device.findObject(new UiSelector().text(TEXT_DENY));
    deny.click();
}
 
Example 25
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 4 votes
public static void allowPermission(UiDevice device) throws UiObjectNotFoundException
{
    UiObject allow = device.findObject(new UiSelector().text(TEXT_ALLOW));
    allow.click();
}
 
Example 26
Project: AsteroidOSSync   File: UIUtil.java   View Source Code Vote up 4 votes
public static boolean viewExistsExact(UiDevice device, String messageText) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().text(messageText));
    return view.exists();
}
 
Example 27
Project: android-testing-runtime-permissions   File: UiAutomatorUtils.java   View Source Code Vote up 4 votes
public static void grantPermission(UiDevice device, String permissionTitle) throws UiObjectNotFoundException {
    UiObject permissionEntry = device.findObject(new UiSelector().text(permissionTitle));
    permissionEntry.click();
}
 
Example 28
Project: brickator   File: Resolver.java   View Source Code Vote up 2 votes
@Override
public Class<? extends BrickatorState> resolveCurrentState(String currentFocusedPackage, String currentFocusedActivity, UiDevice uiDevice) {

    Brickator.print("current package is: " + currentFocusedPackage);
    Brickator.print("current activity is " + currentFocusedActivity);

    if (BrickatorUtilities.isAppInstalled(Brickator.getInstance().getAppPackage())) {

        if (!currentFocusedPackage.equals(Brickator.getInstance().getAppPackage())) {
            return BackgroundState.class;
        }

    } else {

        return UninstalledState.class;

    }

    if (currentFocusedActivity.equals(PermissionState.getActivityName())) {
        return PermissionState.class;
    }

    if (currentFocusedActivity.equals(MainState.getActivityName())) {

        Context context = InstrumentationRegistry.getTargetContext();

        UiObject mainMenu = uiDevice.findObject(new UiSelector().text(context.getResources().getString(R.string.gallery)));

        if (mainMenu != null && mainMenu.exists()) {
            return MainMenuState.class;

        }

        UiObject mainState = uiDevice.findObject(new UiSelector().description(context.getResources().getString(R.string.fragment_main)));

        if (mainState != null && mainState.exists()) {
            return MainState.class;

        }

        UiObject galleryState = uiDevice.findObject(new UiSelector().description(context.getResources().getString(R.string.fragment_gallery)));

        if (galleryState != null && galleryState.exists()) {
            return GalleryState.class;

        }

    }

    // Remember to return UnknownState.class; at the very end! //TODO this should probably require calling super class at the end would make things for convenient
    return UnknownState.class;
}