[Android] Click the menu items


#1

Hi there,

I am using Appium to test Android apps. I am using Java language. I want to click menu items automatically. By searching online (https://groups.google.com/forum/#!topic/appium-discuss/fxzn5kfBt4A), I found the way to click the menu icon, which is like this:

driver.sendKeyEvent(AndroidKeyCode.MENU);

in which driver is an instance of AppiumDriver.

However, I did not find the way to click on a specific menu. For example, in my test app, I have two menu items with text “About” and “Rescan”. How do I automatically click on the two menu items?

Thanks,
Jeff


#2

You can find the menu item by name. which on Android also finds by text, then click the returned element. Something like

menu_element = appium_driver.driver.findElement(By.name(menu_text));
menu_element.click();

#3

How they “About” and “Rescan” items are displayed in your application. Can you able to identify with UIAutomatorViewer or Inspector?

Hopefully the below may work for you:

driver.findElementByAndroidUIAutomator("text(\"About\")").click();
driver.findElementByAndroidUIAutomator("text(\"Rescan\")").click();

If the above doesnt work for you use xpath or resourceId etc…


#4

Sorry, I was not clear that I want the code to work for all apps, but not specific to the two menu items with text “About” and “Rescan”. Therefore, I cannot check the text to locate the menu item. I tested with following code, which seems work. Let me know if you have any other suggestions.

	System.out.println("click on menu");
	driver.sendKeyEvent(AndroidKeyCode.MENU); 
	Thread.sleep(3000);
	
	System.out.println("click on menu items");
	List<WebElement> menu_items = driver.findElementsByClassName("android.widget.TextView");
	System.out.println(""+menu_items.size()+" menu items found");
	for (WebElement menu_item:menu_items) {
		//click on a menu item
		Thread.sleep(3000);
	}

#5

You could make this a method which takes the desire text as a parameter.

Using UD’s code above, it would become something like

driver.findElementByAndroidUIAutomator("text(\"" + text_param + "\")").click();

Alternatively, you could pass in an index. This has the benefit of being language independent, but you have to map out each menu screen to verify the order of the entries in the menu list.


#6

I think what jeffchenurbana was looking for is a way to find menu item elements that does not depend on the text shown in the UI, to make the appium test resilient to changes to the text strings.

I’m trying to do this with a menu built with NavigationView. With this class, each menu item is a CheckedTextView with resource-id=<package-name>:id/design_menu_item_text and no content-desc set. The only way to distinguish different menu items from each other is via the text attribute, which is less than ideal.

I’m wondering if anyone has found an approach that works.


#7

Have you found the solution?


#8

I found this solution.

There was no resource id or content description available even after been setting up for menu items. Still not available in uiAutomatorViewer.

So i get the menu item by its xpath. like this:

        driver.findElementByXPath("//android.widget.CheckedTextView[@text='Home']").click();

the class for menu item was ‘android.widget.CheckedTextView’ so i add the Label of menu item which is ‘Home’. So it works.
Hope it helps