Use of Content description(Android)

5,269 views
Skip to first unread message

Anshu

unread,
Jun 20, 2013, 2:56:18 PM6/20/13
to appium-...@googlegroups.com
In order to identify elements on screen for automation can Accessibility identifier i.e. Content Description be used.
Can someone provide a Java example of how to use this . Much appreciated.Thanks.

satyajit malugu

unread,
Jun 20, 2013, 4:44:04 PM6/20/13
to Anshu, appium-...@googlegroups.com
Lets say you need to add content description to password field

which is an edittext

<EditText
                android:id="@+id/password"

                android:inputType="textPassword"
        
                android:contentDescription="@string/cd_password" />

You add it like this to your screen.xml. It is also recommended that you add cd_password to your strings.xml file like this

<resources>
    <string name="cd_password">password</string>
</resources>

Depending on the wrapper your are using, you can find elements based on text and id's too(ruby).
June 20, 2013 11:56 AM
In order to identify elements on screen for automation can Accessibility identifier i.e. Content Description be used.
Can someone provide a Java example of how to use this . Much appreciated.Thanks.

--
http://appium.io
---
You received this message because you are subscribed to the Google Groups "Appium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appium-discus...@googlegroups.com.
Visit this group at http://groups.google.com/group/appium-discuss.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Anshu

unread,
Jun 20, 2013, 8:37:46 PM6/20/13
to appium-...@googlegroups.com, Anshu
Thanks for response. Being new to this,  I am also looking for how this password field can be accessed in automation script written in Java once content description is set.

bootstrap online

unread,
Jun 20, 2013, 11:25:51 PM6/20/13
to Anshu, appium-...@googlegroups.com
On Thu, Jun 20, 2013 at 8:37 PM, Anshu <anik...@gmail.com> wrote:
how this password field can be accessed in automation script written in Java once content description is set.

Find by name. 

Jeffrey Blaze

unread,
Aug 19, 2013, 5:02:06 PM8/19/13
to appium-...@googlegroups.com
Unfortunately, the android:contentDescription works for very few Android UI elements.  I am trying to figure out a way around that, but it seems to me that we will need a way to access those elements by the text value.  But that capability does not exist yet in Appium (for Java).

Jonathan Lipps

unread,
Aug 19, 2013, 5:06:52 PM8/19/13
to Jeffrey Blaze, appium-...@googlegroups.com
You can access Android elements by text value using Xpath, I believe. Something like //text[@text='foo']

Jeffrey Blaze

unread,
Aug 19, 2013, 7:27:00 PM8/19/13
to Jonathan Lipps, appium-...@googlegroups.com
Thanks Jonathan, but that doesn't work.  In fact, I can't access the text of those elements at all.

//These don't work

driver.findElement(By.xpath("//text[text()='Kabam Rewards']")).click();

driver.findElement(By.xpath("//text[@text='Kabam Rewards']")).click()

//but this does

driver.findElement(By.xpath("//list[1]/linear[1]/relative[1]/text[1]")).click();


Someone (perhaps bootstrap online?) mentioned in an earlier post that accessing Android elements by the text property is not implemented in the Java bindings.

 

bootstrap online

unread,
Aug 19, 2013, 8:15:04 PM8/19/13
to Jeffrey Blaze, Jonathan Lipps, appium-...@googlegroups.com
It's all working for me in Ruby so in theory any language can use the
same API. We probably need an Appium Java library to make this easier
for people.

On Mon, Aug 19, 2013 at 7:27 PM, Jeffrey Blaze

Jeffrey Blaze

unread,
Aug 19, 2013, 9:55:49 PM8/19/13
to bootstrap online, Jonathan Lipps, appium-...@googlegroups.com
Yeah, we discussed this topic on a related thread a couple weeks ago. The upshot is that there is no way to access an Android text element in Appium using Java, other than by way of a vanilla (no references to the text in a predicate) xpath.

Bootstrap mentioned in another post that UIAutomator will include support for the contentDescription property.  Until then, I will try making findElements calls like this:

driver.findElements(By.xpath("//list[1]/linear[1]/relative[1]/text")) 

and looping through the results looking for the text I want.  Although, given the lack of support for @text and text() in paths. I have my doubts as to whether I'll be able to interrogate the text in the array of Element objects.  We'll see...

bootstrap online

unread,
Aug 20, 2013, 9:39:55 AM8/20/13
to Jeffrey Blaze, Jonathan Lipps, appium-...@googlegroups.com
uiautomator already fully supports contentDescription. Any
contentDescription visible with uiautomatorviewer is accessible via
Appium. What's missing is real id support, which was added in API 18
and isn't supported yet in Appium.

If someone figures out how to use mobile: find in Java, then searching
all text on screen in simple.

On Mon, Aug 19, 2013 at 9:55 PM, Jeffrey Blaze

Jeffrey Blaze

unread,
Aug 20, 2013, 2:32:15 PM8/20/13
to bootstrap online, Jonathan Lipps, appium-...@googlegroups.com
Is uiautomatorviewer the same thing as uiautomator?  I am using uiautomatorviewer to interrogate my UI, and in my experience it does not display the value of the contentDescription property for:
TextView
<item>
<menu>
It does display it for:
ImageButton
ImageView

My concern is the TextView (and, to a lesser extent, the <item>) element.  Since we can't grab it using expressions like:
driver.findElement(By.xpath("//text[text()='Kabam Rewards']")).click();
driver.findElement(By.xpath("//text[@text='Kabam Rewards']")).click();
I will need to try something like the findElements call I described in an earlier post to this thread

bootstrap online

unread,
Aug 20, 2013, 3:08:16 PM8/20/13
to Jeffrey Blaze, Jonathan Lipps, appium-...@googlegroups.com
contentDescription works properly on everything. See the API demos for
an example of working TextView with content descriptions.
uiautomatorviewer is a helper app and is different from uiautomator.

On Tue, Aug 20, 2013 at 2:32 PM, Jeffrey Blaze

Jeffrey Blaze

unread,
Aug 20, 2013, 3:24:15 PM8/20/13
to bootstrap online, Jonathan Lipps, appium-...@googlegroups.com
I looked at the API demos previously, but they are in Ruby - I'm working in Java.  Thanks for the tip about uiautomator.  I'll check it out.
   

bootstrap online

unread,
Aug 20, 2013, 3:27:14 PM8/20/13
to Jeffrey Blaze, Jonathan Lipps, appium-...@googlegroups.com
API demos is an apk that's part of Appium's unit tests.
uiautomatorviewer can be used to inspect it. The API demos code is in
Java and demonstrates proper usage of contentDescriptions.

On Tue, Aug 20, 2013 at 3:24 PM, Jeffrey Blaze

Jeffrey Blaze

unread,
Aug 20, 2013, 4:34:08 PM8/20/13
to bootstrap online, Jonathan Lipps, appium-...@googlegroups.com
My bad on the API demos.  I looked again, and found it in appiumandroid-apidemos.  I'll take a look...

Marius Bob

unread,
Nov 14, 2013, 5:20:38 AM11/14/13
to appium-...@googlegroups.com
How do you write the code for finding an element by content description?

Dan Cuellar

unread,
Nov 14, 2013, 10:38:37 AM11/14/13
to appium-...@googlegroups.com
use by name I believe

LOKESH L R

unread,
Jan 25, 2014, 2:03:16 PM1/25/14
to appium-...@googlegroups.com, Jeffrey Blaze, Jonathan Lipps
Hi ,
Can you please tell me how to inspect the Elements in Android App because i am new to Appium.

Thanks in Advance
Message has been deleted

Brent LaVelle

unread,
Feb 13, 2014, 6:58:17 PM2/13/14
to appium-...@googlegroups.com, Jeffrey Blaze, Jonathan Lipps
Use the uiautomatorviewer viewer to see all of the content descriptions in your app http://developer.android.com/tools/testing/testing_ui.html

STK

unread,
Feb 25, 2014, 3:32:54 AM2/25/14
to appium-...@googlegroups.com, Jeffrey Blaze, Jonathan Lipps
I want to get the contentDescriptor of an element. I have tried the following code for that and not succeeded. 

String contentDesc = element.getAttribute("content-desc");

It would be great if any one can help me in this.

Jonathan Lipps

unread,
Feb 25, 2014, 1:05:43 PM2/25/14
to STK, appium-...@googlegroups.com, Jeffrey Blaze
do this instead:

element.getAttribute("name");

Andrew Dinh

unread,
Feb 25, 2014, 1:33:10 PM2/25/14
to appium-...@googlegroups.com
Hi,

I can confirm that you can use By.name for both the 'content-desc' and 'text' fields.  I use them regularly.

Andrew Dinh

unread,
Feb 25, 2014, 1:52:15 PM2/25/14
to appium-...@googlegroups.com
driver.findElement(By.name("Text in content-desc field")).sendKeys("my text");
        
As a new user, you might find these tips to be useful:
1.  Use By.name for text and content-desc fields
2.  Use By.id and the full content of the resource-id field in "" to locate by resource-id
3.  Use tagName, but only the last part of the class to locate using the content of the 'class' field.  (e.g., use "ImageView" for the 'class' content android.widget.ImageView)


wait.until(ExpectedConditions.presenceOfElementLocated(By.name("Something")));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("Something else")));

If you want to check if an element exists and not throw an exception when the item doesn't exist, use:
if (!driver.findElements(By.name("Camera settings")).isEmpty()) {
}

or something similiar

I have not used xpath as locator, but I believe you can only use it with selendroid?  I am not sure.

Just some tips from one Appium Java user to another :)  

Ramdas Krishna Baliga

unread,
Mar 12, 2014, 3:50:51 AM3/12/14
to appium-...@googlegroups.com
Very nicely listed.. Basics, but u can almost automate 80% of your UI automation work using these :)

Thanks a lot !

Regards,
Ramdas
Reply all
Reply to author
Forward
0 new messages