UIAutomator - How to click the menu item - android

I have automated go to setting in android phone using UI Automator and i clicked the menu using "getUiDevice().pressMenu();" and its opened menu with 3 sub menu item and i want to click the second menu by using name or index or id , please help how to click the sub menu in Android UIAutomator ?

This overflow menu icon ImageView with the three dots has no id ...
but one can get it by it's description:
UiObject2 menuButton = this.mDevice.findObject(By.desc("More options"));
/* open the menu */
if(menuButton != null && menuButton.isClickable()) {
menuButton.click();
menuButton.recycle();
} else {
Assert.fail();
}
Then one can click menu items by their index:
ArrayList<UiObject2> menu = (ArrayList<UiObject2>) this.mDevice.findObject(By.clazz("android.widget.ListView")).getChildren();
/* click the menu item at index 0 */
UiObject2 menu_action = menu.get(0);
if (menu_action != null && menu_action.isClickable()) {
menu_action.click();
menu_action.recycle();
} else {
Assert.fail();
}

Use clazz method is working
UiObject2 item = mDevice.findObject(By.clazz("android.widget.ListView"));

You can use 'uiautomatorviewer' tool and view the text/description/resource id/index of your menu items displayed there.
To use uiautomatorviewer tool :
[1] Connect your device.
[2] Open command prompt and type 'uiautomatorviewer'.
[3] Open the particular screen of whose elements you want to view (in this case - your menu items).
[4] Press the green 'Device Screenshot' button next to file open in top left.
Hover on the screenshot to view the text/description/resource id/index of the elements of the screen and you can use whatever is being displayed there.

Related

Android UI testing: how to click to Android dialog?

Problem:
I cannot run test, because when Activity using fullscreen mode, Android dialog (see picture) is showed.
Goal:
I want to click to GOT IT and close the dialog.
Code (Kotlin):
//device is instance of UiDevice UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val gotIt = device.findObject(UiSelector()
//.clickable(true)
//.checkable(false)
//.className("android.widget.Button")
//.packageName("com.android.launcher3")
//.resourceId("android:id/ok")
//.index(3) //by index for language-independent
.text("GOT IT")
)
if (gotIt.waitForExists(3000)) {
try {
return gotIt.click()
} catch (e: UiObjectNotFoundException) {
e.printStackTrace()
}
} else Log.i(TAG, "clickViewingFullscreenDialog: btn not exists")
I tried many combination of selectors, but i still cannot click to GOT IT. Selectors are correct, but dialog is as "invisible" or unfocusable. Cannot click to dialog.
Additional info:
Trying on Emulator Nexus 5 Android 7.0.
I tried uiautomatorviewer and layoutinspector to get information about View hierarchy to find this Android Dialog, but i can get information only about android home screen (see second picture), not about own Activity (first picture).
is "GOT IT" a positive or neutral button?
if it's positive, click on ID android.R.id.button2.
And in case it's neutral button, click on ID android.R.id.button3

How to open ActionMode menu with help of Espresso?

I'm trying to test the scenario like below.
Here is my code:
#Test
public void testRenameList() {
addNewList();
// Long click on the list
onView(recyclerViewItemWithText(mNewListName)).perform(longClick());
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
}
The following error occures on the last line:
android.support.test.espresso.AmbiguousViewMatcherException: '((is displayed on the screen to the user and with content description: is "More options") or (is displayed on the screen to the user and with class name: a string ending with "OverflowMenuButton"))' matches multiple views in the hierarchy.
I think, Espresso finds two menu: from ActionMode and from ActionBar.
The question is how to open menu exactly from ActionMode?
I've solved this issue by using UIAutomator.
I've replaced the line
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
by
UiObject btnMenu = mDevice.findObject(new UiSelector().description(mActivity.getString(R.string.menu_button_identifier)));
btnMenu.click();
The value of R.string.menu_button_identifier in English is "More options".

Espresso: calling openActionBarOverflowOrOptionsMenu() opens the first item in the menu

I have the below espresso test:
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
// if I Thread.sleep() here, I can see that the MenuItem has been clicked already
onView(withText("Sign in")) //<= click on the MenuItem
.perform(click());
onView(withId(R.id.signupButton)) //<= click the signup button in my UI
.perform(click());
The first line up there opens the overflow menu and clicks the first item at the same time (which happens to be the signin item). So the test fails because it cannot find the MenuItem view. Is there anything I am doing wrong ? I am using an emulator API 22, compiling agains targetSdk 24 and using espresso 2.2.1.
Try that:
public class EspressoMatchers {
public static Matcher<View> withOverflowMenuButton() {
return anyOf(allOf(isDisplayed(), withContentDescription("More options")),
allOf(isDisplayed(), withClassName(endsWith("OverflowMenuButton"))));
}
}
To open overflow menu:
onView(allOf(EspressoMatchers.withOverflowMenuButton(),
isDescendantOfA(withId(R.id.toolbar)))).perform(click());
Then it should work fine. Just use right ID for your Toolbar. I know this is just a copy from Espresso class, but I also ran into this issue and this helped me.
Please remember to always click the menu items "by name", not by their ID, as ID won't work. So your "click item" should be fine:
onView(withText("Sign in")) //<= click on the MenuItem
.perform(click());

How to display title text(in the nav bar) above TabGroup in Android in Titanium?

Does any one know how to display title text(in the nav bar) above TabGroup in Android in Titanium?
I know I can set title to display at each tab, but how about the one on top of the tab group?
It only has an application icon at left and rather strange there if it's android. I tried to set navBarHidden : true so that nav bar won't show, but the icon inside tab also goes hidden...
Please kindly advise me here...thanks!
You can set title/icon and handle icon click events of the action bar with the following code
// Set activity title and back functionality
if( Ti.Platform.Android.API_LEVEL >= 11 ) {
var activity = tabGroup.getActivity();
activity.actionBar.icon = "/images/icon.png";
activity.actionBar.title = "Title";
activity.actionBar.onHomeIconItemSelected = function() {
doSomething();
};
}
This example assumes that your tab group is called tabGroup. You could also do this with a window activity var activity = win.getActivity();
You can find more on this topic in the Titanium docs.

How to make cursor invisible on clicking "GO" button from soft keyboard in android?

I am preparing app using phonegap.In app i have displayed list of data and one search bar.User can search by particular name.Here i have used jquery ajax.It is working fine.My problem is when click on search bar soft keyboard will open automatically.After enter the text just click on "GO" button in soft keyboard i call search method.It is also working fine.But cursor is still visible on search bar.I want to make the cursor invisible on clicking "GO" button from soft keyboard.My code is,
function submitOnEnter(inputElement, event)
{
console.log("KEY OCDE:: "+event.keyCode);
if (event.keyCode == 13)
{
caSearch();
}
}
Please guide me to over come this.
Use these attributes, which will make it impossible to select your edittext and will therefore deselect if for you.
yourEditText.setFocusable( false );
yourEditText.setFocusableInTouchMode( false );
Immediately afterwards, reverse these two prices of code
yourEditText.setFocusable( true );
yourEditText.setFocusableInTouchMode( true );
That should remove the cursor from search bar for you

Categories

Resources