Java Code Examples for android.support.test.uiautomator.UiObject#click()
The following examples show how to use
android.support.test.uiautomator.UiObject#click() .
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: UIAnimatorTest.java From Expert-Android-Programming with MIT License | 7 votes |
@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(); } }
Example 2
Source File: PermissionGranter.java From mobile-android-samples with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void allowPermissionsIfNeeded() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 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 3
Source File: UiHelper.java From AppCrawler with Apache License 2.0 | 6 votes |
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 4
Source File: MainActivityTest.java From android-testing-guide with MIT License | 6 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 5
Source File: BasicUITests.java From restcomm-android-sdk with GNU Affero General Public License v3.0 | 6 votes |
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 6
Source File: UIAutomatorWD.java From UIAutomatorWD with MIT License | 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 7
Source File: UIUtil.java From SweetBlue with GNU General Public License v3.0 | 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 8
Source File: ItClickerActivity.java From SmoothClicker with MIT License | 5 votes |
/** * Test if the switch changes (for the start type) modifies well the delay field * * <i>If the switch for the start type is ON, the delay field is enabled.</i> * <i>If the switch for the start type is OFF, the delay field is disabled.</i> */ @Test public void changeDelayOnSwitchChanges(){ l(this, "@Test changeDelayOnSwitchChanges"); try { UiObject startSwitch = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/sTypeOfStartDelayed") ); UiObject delayField = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/etDelay") ); // Check and uncheck the switch for ( int i = 1; i <= 2; i++ ) { startSwitch.click(); if (startSwitch.isChecked()) { assertTrue(delayField.isEnabled()); } else { assertFalse(delayField.isEnabled()); } } } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail( uonfe.getMessage() ); } }
Example 9
Source File: ItClickerActivity.java From SmoothClicker with MIT License | 5 votes |
/** * Test the start button without having defined clicks * * <i>If the button to start the click process is clicked, and no point has been defined, a snackbar with an error message has to be displayed</i> */ @Test public void startButtonWithoutDefinedPoints(){ l(this, "@Test startButtonWithoutDefinedPoints"); try { // Open the arc menu UiObject arcMenu = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/fabAction") ); arcMenu.click(); // Click on the button UiObject startFab = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/fabStart") ); startFab.click(); // Check the snackbar UiObject snack = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/snackbar_text") ); String expectedString = InstrumentationRegistry.getTargetContext().getString(R.string.error_message_no_click_defined); assertEquals(expectedString, snack.getText()); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail( uonfe.getMessage() ); } }
Example 10
Source File: SignInActivityTest.java From ribot-app-android with Apache License 2.0 | 5 votes |
private void allowPermissionsIfNeeded() { if (Build.VERSION.SDK_INT >= 23) { UiObject allowPermissions = mDevice.findObject(new UiSelector().text("Allow")); if (allowPermissions.exists()) { try { allowPermissions.click(); } catch (UiObjectNotFoundException e) { Timber.w(e, "There is no permissions dialog to interact with "); } } } }
Example 11
Source File: ItClickerActivity.java From SmoothClicker with MIT License | 5 votes |
/** * Tests the long clicks on the floating action button for stop in the arc menu * * <i>A long click on the button to use to stop the process should display a snackbar with an explain message</i> */ @Test public void longClickOnArcMenuStopItem(){ l(this, "@Test longClickOnArcMenuStopItem"); String expectedString = InstrumentationRegistry.getTargetContext().getString(R.string.info_message_stop); try { /* * Display the floating action buttons in the arc menu */ UiObject arcMenu = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/fabAction") ); arcMenu.click(); arcMenu.waitForExists(WAIT_FOR_EXISTS_TIMEOUT); /* * The floating action button */ UiObject fab = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH+":id/fabStop") ); fab.waitForExists(WAIT_FOR_EXISTS_TIMEOUT); assertTrue(fab.isLongClickable()); fab.swipeLeft(100); //fab.longClick() makes clicks sometimes, so swipeLeft() is a trick to make always a longclick UiObject snack = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/snackbar_text") ); assertEquals(expectedString, snack.getText()); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail( uonfe.getMessage() ); } }
Example 12
Source File: ItClickerActivity.java From SmoothClicker with MIT License | 5 votes |
/** * Test the click on the button for the "clean points" feature * * <i>If the button to clean points is clicked, the list of points has to be cleaned</i> * <i>If the button to clean points is clicked and no values has been changed, the values still remain the default ones</i> */ @Test public void clickCleanPoints() { l(this, "@Test clickCleanPoints"); try { // Get the menu item UiObject mi = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/action_clean_all") ); w(5000); // If there is no wait, Espresso fails to get the floating action button // Bind the list fillSpinnerAsUser(); // Click on the menu item mi.click(); // Check if the values have been made empty // onView(withId(R.id.clickerActivityMainLayout)).perform(swipeUp()); // onView(withId(R.id.sPointsToClick)).perform(click()); // onView(withId(R.id.sPointsToClick)).check(ViewAssertions.matches(withListSize(1))); // Test again to check if the default values remain mi.click(); // onView(withId(R.id.sPointsToClick)).check(ViewAssertions.matches(withListSize(1))); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail( uonfe.getMessage() ); } }
Example 13
Source File: ItSettingsActivity.java From SmoothClicker with MIT License | 5 votes |
/** * Opens the settings screen from the menu */ private void openSettingsScreenFromMenu(){ try { // Clicks the three-points-icon UiObject menu = mDevice.findObject( new UiSelector() .className("android.widget.ImageView") .packageName(PACKAGE_APP_PATH) //.descriptionContains("Plus d'options") // WARNING FIXME French string used, use instead system R values .descriptionContains("Autres options") // WARNING FIXME French string used, use instead system R values ); menu.click(); // Clicks on the settings item String s = InstrumentationRegistry.getTargetContext().getString(R.string.action_settings); UiObject itemParams = mDevice.findObject( new UiSelector() .className("android.widget.TextView") .packageName(PACKAGE_APP_PATH) .resourceId(PACKAGE_APP_PATH + ":id/title") .text(s) ); itemParams.click(); w(1000); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail(uonfe.getMessage()); } w(1000); }
Example 14
Source File: UiAutomatorUtils.java From Forage with Mozilla Public License 2.0 | 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 15
Source File: ItClickerActivity.java From SmoothClicker with MIT License | 5 votes |
/** * Tests the import feature with its snackbar * * <i>If the configuration is imported in the JSON files, a snackbar with a good message is displayed</i> */ @Test public void importConfig(){ l(this, "@Test importConfig"); try { // Display the pop-up UiObject menu = mDevice.findObject( new UiSelector().className("android.widget.ImageView").description("Autres options") // FIXME Raw french string ); menu.click(); UiObject submenu = mDevice.findObject( new UiSelector().className("android.widget.TextView").text(InstrumentationRegistry.getTargetContext().getString(R.string.action_configuration)) ); submenu.click(); UiObject menuItem = mDevice.findObject( new UiSelector().className("android.widget.TextView").text(InstrumentationRegistry.getTargetContext().getString(R.string.action_import)) ); menuItem.click(); // Check the snackbar UiObject snackbar = mDevice.findObject( new UiSelector().className("android.widget.TextView") .resourceId(PACKAGE_APP_PATH + ":id/snackbar_text") ); assertTrue(snackbar.exists()); assertEquals(InstrumentationRegistry.getTargetContext().getString(R.string.info_import_success), snackbar.getText()); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail(uonfe.getMessage()); } }
Example 16
Source File: ItSettingsActivity.java From SmoothClicker with MIT License | 4 votes |
/** * @param index - The idnex of the field in the list (start at 0) * @param text - The text of the field to get */ private void testFieldWithName( int index, String text ){ if ( text == null || text.length() <= 0 || index < 0){ fail("Wrong test"); } final String DUMMY_TEXT = "Hello world"; try { // Display the dialog UiObject field = mDevice.findObject( new UiSelector() .className("android.widget.TextView") .packageName(PACKAGE_APP_PATH) .resourceId("android:id/title") .text(text) ); field.click(); // Change the value field = mDevice.findObject( new UiSelector() .className("android.widget.EditText") .packageName(PACKAGE_APP_PATH) .resourceId("android:id/edit") ); final String BACKUP_TEXT = field.getText(); field.setText(DUMMY_TEXT); // Confirm UiObject button = mDevice.findObject( new UiSelector() .className("android.widget.Button") .packageName(PACKAGE_APP_PATH) .resourceId("android:id/button1") ); button.click(); w(1000); // Check the summary BySelector checkboxSettingsSelector = By.clazz("android.widget.TextView"); List<UiObject2> summaries = mDevice.findObjects(checkboxSettingsSelector); UiObject2 field2 = summaries.get( index * 4 + 2 ); assertEquals(DUMMY_TEXT, field2.getText()); // Reset the value resetFieldWithText( DUMMY_TEXT, BACKUP_TEXT ); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail(uonfe.getMessage()); } }
Example 17
Source File: ItSettingsActivity.java From SmoothClicker with MIT License | 4 votes |
/** * Tests the files names fields * * <i>If we change the name of the file in sue, we have to see the summary of the dedicated field be updated</i> */ @Test public void filesNames(){ l(this, "@Test filesNames"); try { // Swipe to the bottom of the view to get the fields field in an inner preference screen UiObject list = mDevice.findObject( new UiSelector() .className("android.widget.ListView") .packageName(PACKAGE_APP_PATH) .resourceId("android:id/list") ); list.swipeUp(100); String innerPreferenceScreenTitle = InstrumentationRegistry.getTargetContext().getString(R.string.pref_app_files_title); UiObject filesRow = mDevice.findObject( new UiSelector() .className("android.widget.TextView") .packageName(PACKAGE_APP_PATH) .resourceId("android:id/title") .text(innerPreferenceScreenTitle) ); filesRow.click(); // Update the name of file containing the points testFieldWithName(0, InstrumentationRegistry.getTargetContext().getString(R.string.pref_app_files_points_name)); // Update the name of file containing the config testFieldWithName(1, InstrumentationRegistry.getTargetContext().getString(R.string.pref_app_files_config_name)); // Update the name of file containing the unlock script testFieldWithName(2, InstrumentationRegistry.getTargetContext().getString(R.string.pref_app_files_unlock_name)); // Update the name of file containing the picture testFieldWithName(3, InstrumentationRegistry.getTargetContext().getString(R.string.pref_app_files_trigger_name)); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail(uonfe.getMessage()); } }
Example 18
Source File: ItStandaloneModeDialog.java From SmoothClicker with MIT License | 4 votes |
/** * Tests if the notification is displayed when the standalone mode is running * * <i>If the standalone mode is running, a dedicated notification is displayed</i> */ @Test public void notificationOnGoing(){ l(this, "@Test notificationOnGoing"); prepareNeededFiles(); // Start a standalone mode try { // Choose the OCR-like standalone mode (the alst of the list) for ( int i = 1; i <= steps; i++ ){ UiObject rightArrow = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/swipeselector_layout_rightButton") ); rightArrow.click(); } // Click on the OK button UiObject btOk = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/btCreate") ); btOk.click(); // Check the notification String notificationText = InstrumentationRegistry.getTargetContext().getString(R.string.notif_content_text_clicks_on_going_standalone); UiObject notification = null; if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH ){ notification = mDevice.findObject( new UiSelector() .resourceId("android:id/text") .className("android.widget.TextView") .packageName("pylapp.smoothclicker.android") .textContains(notificationText)); } else { notification = mDevice.findObject( new UiSelector() .resourceId("android:id/text") .className("android.widget.TextView") .packageName("com.android.systemui") .textContains(notificationText)); } mDevice.openNotification(); notification.waitForExists(2000); w(10000); assertTrue(notification.exists()); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail( uonfe.getMessage() ); } }
Example 19
Source File: ItClickerActivity.java From SmoothClicker with MIT License | 4 votes |
/** * Test the click on the button for the "clean all" feature * * <i>If the button to clean all is clicked, the configuration / values ion the fields have to be the default values, and the list of points has to be cleaned</i> * <i>If the button to clean all is clicked and no values has been changed, the values still remain the default ones</i> */ @Test public void clickCleanAll(){ l(this, "@Test clickCleanAll"); try { // Get the menu item UiObject mi = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/action_clean_all") ); // Add some values UiObject delayField = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/etDelay") ); delayField.setText("007"); Espresso.closeSoftKeyboard(); w(1000); UiObject repeatField = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/etRepeat") ); repeatField.setText("42"); Espresso.closeSoftKeyboard(); w(1000); UiObject timeGapField = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/etTimeBeforeEachClick") ); timeGapField.setText("123"); Espresso.closeSoftKeyboard(); w(1000); UiObject endless = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/scEndlessRepeat") ); endless.click(); w(1000); // Swipe to display items UiObject swipeableLayout = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH+":id/clickerActivityMainLayout") ); swipeableLayout.swipeUp(100); UiObject vibrateOnStart = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/scVibrateOnStart") ); vibrateOnStart.click(); w(1000); UiObject vibrateOnClick = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/scVibrateOnClick") ); vibrateOnClick.click(); w(1000); UiObject notifications = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/scNotifOnClick") ); notifications.click(); w(1000); fillSpinnerAsUser(); // Click on the menu item mi.click(); // Check if the values have been made empty assertEquals(Config.DEFAULT_DELAY, delayField.getText()); assertEquals(Config.DEFAULT_TIME_GAP, timeGapField.getText()); assertEquals(Config.DEFAULT_REPEAT, repeatField.getText()); assertEquals(Config.DEFAULT_REPEAT_ENDLESS, endless.isChecked()); assertEquals(Config.DEFAULT_VIBRATE_ON_START, vibrateOnStart.isChecked()); assertEquals(Config.DEFAULT_VIBRATE_ON_CLICK, vibrateOnClick.isChecked()); assertEquals(Config.DEFAULT_NOTIF_ON_CLICK, notifications.isChecked()); // onView(withId(R.id.clickerActivityMainLayout)).perform(swipeUp()); // onView(withId(R.id.sPointsToClick)).perform(click()); // onView(withId(R.id.sPointsToClick)).check(ViewAssertions.matches(withListSize(1))); // Test again to check if the default values remain mi.click(); assertEquals(Config.DEFAULT_DELAY, delayField.getText()); assertEquals(Config.DEFAULT_TIME_GAP, timeGapField.getText()); assertEquals(Config.DEFAULT_REPEAT, repeatField.getText()); assertEquals(Config.DEFAULT_REPEAT_ENDLESS, endless.isChecked()); assertEquals(Config.DEFAULT_VIBRATE_ON_START, vibrateOnStart.isChecked()); assertEquals(Config.DEFAULT_VIBRATE_ON_CLICK, vibrateOnClick.isChecked()); assertEquals(Config.DEFAULT_NOTIF_ON_CLICK, notifications.isChecked()); // onView(withId(R.id.sPointsToClick)).check(ViewAssertions.matches(withListSize(1))); } catch ( Exception e ){ e.printStackTrace(); fail( e.getMessage() ); } }
Example 20
Source File: ItStandaloneModeDialog.java From SmoothClicker with MIT License | 4 votes |
/** * Test the click on the cursor's left arrow * * <i>If the user clicks on then cursor's left arrow, the previous item in the swipe cursor must be displayed</i> */ @Test public void clickLeftArrow(){ l(this, "@Test clickLeftArrow"); try { // Click on the arrow to be at the end of the cursor for ( int i = 1; i <= steps; i++ ){ // Get the selector UiObject rightArrow = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/swipeselector_layout_rightButton") ); rightArrow.click(); } // Go backward for ( int i = 1; i <= steps; i++ ){ // Get the selector UiObject leftArrow = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/swipeselector_layout_leftButton") ); leftArrow.click(); } // Check the first item String[] titles = InstrumentationRegistry.getTargetContext().getResources().getStringArray(R.array.standalone_mode_titles); String[] descs = InstrumentationRegistry.getTargetContext().getResources().getStringArray(R.array.standalone_mode_descriptions); String expectedFirstItemTitle = titles[0]; String expectedFirstItemDesc = descs[0]; UiObject lastItemTitle = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/swipeselector_content_title") ); assertEquals(expectedFirstItemTitle, lastItemTitle.getText()); UiObject lastItemDesc = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/swipeselector_content_description") ); assertEquals(expectedFirstItemDesc, lastItemDesc.getText()); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail( uonfe.getMessage() ); } }