I am getting inconsistent results with the getAttribute() method when trying to retrieve the “value” attribute.
Environment
Appium server version: Version 1.5.3 (Ara)
Desktop OS/version used to run Appium if necessary: OS X 10.11.5
Node.js version: v6.3.0
Mobile platform/version under test: iOS 9.3
Real device or emulator/simulator: Simulator
Details
I am working on automated tests for an app that has buttons that use a change of imagery to show when the button is selected/deselected. As such, I can not use the isSelected() method to check whether the buttons are selected. I worked with the app developer to get “selected” and “deselected” values added to the element as appropriate. When I locate the element and use .getAttribute(“value”), I get an empty String. However, I use getAttribute(“value”) elsewhere in the code and it works as expected. Examples:
This works:
return driver.findElement(NOTES_TEXT_FIELD).getAttribute(“value”).toString();
// NOTES_TEXT_FIELD element type: UIATextView
// element is located by xpath
// didn’t work until I added ".toString()
This does not work:
return driver.findElement(TABLE_CELL_A).getAttribute(“value”).toString();
// TABLE_CELL_A element type: UIAButton
// element is located by AccessibilityId; changed locator from MobileBy.AccessibilityId() to By.xpath() but it didn’t fix the problem
// tried with and without .toString(); doesn’t work either way
// if I look at the element with either the Appium inspector or the Accessibility Inspector, the “selected” and “deselected” traits added by the developer are present under “value”
Any assistance would be greatly appreciated. I searched for this issue and it seems that some others have experienced it, but the issues were either not addressed or the proposed solutions did not work for me.
If the value is set then only it will return assigned value. If value is not set then getAttribute("—") will return null. Perhaps this could be the reason in your case.
Thank you for your responses. I’ve attached two screenshots: One from the Appium Inspector, and the other from the Accessibility Inspector. I apologize for not including the screenshot of the app, but this is for work and I’m not at liberty to share it.
I should also mention that return driver.findElement(TABLE_CELL_A).getAttribute("label") does return A as expected, which makes this issue even more puzzling.
@Christopher_Graham I double checked, and it’s just poor variable naming on my part. I actually am targeting the button. Definitely going to change that though. Thanks!
I would output the full page_source prior to your call to get the value attribute. That should help you troubleshoot. Possibly the value is empty at the time you’re calling it (race condition?)
Programmatically printing the page_source right before your call for the attribute should show you exactly what Appium sees. I do this all of the time to troubleshoot issues clicking / finding / polling / etc. elements.
Then you can manually review the page_source and you’ll see if your element is even in the DOM and if it is exactly what the current state of its attributes are. Since you can see it while manually driving around in the Inspector there’s likely not an Appium issue but more likely an issue in your locator / test code / app / etc.
Thanks again for the tip, @Christopher_Graham. I debugged the test and went through the logs, and sure enough - the value for the “value” attribute was null! Here’s what I don’t get: If I start the app via the Appium UI and fire up the Inspector, I see the “Selected/Deselected” value attribute in the Accessibility Inspector. If I run the automated test and stop at a breakpoint, I don’t see the “Selected/Deselected” value attribute in the Accessibility Inspector. I am definitely using the same build in both situations. It’s also odd that getAttribute(“label”) works in both cases, but not getAttribute(“value”).
Here’s a snippet of what I pulled from the log: {"UIAButton":{"@":{"name":"A","label":"A","value":null,"dom":null,"enabled":true,"valid":true,"visible":true,"hint":null,"path":"/0/0/20/0/0/2/1","x":20,"y":233.361328125,"width":40,"height":40}
I personally cannot offer any additional assistance. I’d suggest looking at some of the other iOS accessibility discussions to see if any give you some paths to explore.
I think this is a bug specific to the UIAButton element - possibly specific to buttons in UIATableView - because “value” can be retrieved from other elements (i.e. UIAStaticText) without issue. I also saw another thread where someone else experienced the same issue, and it doesn’t appear that a resolution was found.
If you do not see anything similar please open a new issue. You’ll get direct help from the development team, here you’re only getting help from other users.