-
Notifications
You must be signed in to change notification settings - Fork 800
Added print accessibility tree command. Find accessibility element command reworked #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #!/usr/bin/python | ||
|
|
||
| # Copyright (c) 2015, Facebook, Inc. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. An additional grant | ||
| # of patent rights can be found in the PATENTS file in the same directory. | ||
|
|
||
| import re | ||
| import os | ||
|
|
||
| import lldb | ||
| import fblldbbase as fb | ||
| import fblldbobjecthelpers as objHelpers | ||
|
|
||
| # This is the key corresponding to accessibility label in _accessibilityElementsInContainer: | ||
| ACCESSIBILITY_LABEL_KEY = 2001 | ||
|
|
||
| def lldbcommands(): | ||
| return [ | ||
| FBPrintAccessibilityLabels(), | ||
| FBFindViewByAccessibilityLabelCommand(), | ||
| ] | ||
|
|
||
| class FBPrintAccessibilityLabels(fb.FBCommand): | ||
| def name(self): | ||
| return 'pa11y' | ||
|
|
||
| def description(self): | ||
| return 'Print accessibility labels of all views in hierarchy of <aView>' | ||
|
|
||
| def args(self): | ||
| return [ fb.FBCommandArgument(arg='aView', type='UIView*', help='The view to print the hierarchy of.', default='(id)[[UIApplication sharedApplication] keyWindow]') ] | ||
|
|
||
| def run(self, arguments, options): | ||
| forceStartAccessibilityServer(); | ||
| printAccessibilityHierarchy(arguments[0]) | ||
|
|
||
| class FBFindViewByAccessibilityLabelCommand(fb.FBCommand): | ||
| def name(self): | ||
| return 'fa11y' | ||
|
|
||
| def description(self): | ||
| return 'Find the views whose accessibility labels match labelRegex and puts the address of the first result on the clipboard.' | ||
|
|
||
| def args(self): | ||
| return [ fb.FBCommandArgument(arg='labelRegex', type='string', help='The accessibility label regex to search the view hierarchy for.') ] | ||
|
|
||
| def run(self, arguments, options): | ||
| forceStartAccessibilityServer() | ||
| rootView = fb.evaluateExpression('(id)[[UIApplication sharedApplication] keyWindow]') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| accessibilityGrepHierarchy(rootView, arguments[0]) | ||
|
|
||
| def forceStartAccessibilityServer(): | ||
| #We try to start accessibility server only if we don't have needed method active | ||
| if not fb.evaluateBooleanExpression('[UIView instancesRespondToSelector:@selector(_accessibilityElementsInContainer:)]'): | ||
| #Starting accessibility server is different for simulator and device | ||
| if fb.evaluateBooleanExpression('((NSRange)[(NSString *)[(id)[(id)[UIDevice currentDevice] model] lowercaseString] rangeOfString:@"simulator"]).location != __LONG_MAX__'): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of getting the model as a string, then do the string search in python? Might be clearer that way. Alternatively, is there any benefit choosing which selector to call based on availability, such as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, despite the fact that accessibility behaves very differently in simulator and device - methods used are pretty much the same, so I didn't find any selector that can be used for this. |
||
| lldb.debugger.HandleCommand('expr (void)[[UIApplication sharedApplication] accessibilityActivate]') | ||
| else: | ||
| lldb.debugger.HandleCommand('expr (void)[[[UIApplication sharedApplication] _accessibilityBundlePrincipalClass] _accessibilityStartServer]') | ||
|
|
||
| def accessibilityLabel(view): | ||
| #using Apple private API to get real value of accessibility string for element. | ||
| return fb.evaluateExpressionValue('(id)[%s accessibilityAttributeValue:%i]' % (view, ACCESSIBILITY_LABEL_KEY), False) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how to do use it with a string. If I try evaluateObjectExpression(...) I get string address, instead of actual string in case or evaluateExpressionValue(...).getObjectDescription(). Any suggestion on how to do it right?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. False positive on my part. This can be left as is, apologies for the misdirection. |
||
|
|
||
| def printAccessibilityHierarchy(view, indent = 0): | ||
| a11yLabel = accessibilityLabel(view) | ||
| classDesc = objHelpers.className(view) | ||
| indentString = ' | ' * indent | ||
|
|
||
| #if we don't have any accessibility string - we should have some children | ||
| if int(a11yLabel.GetValue(), 16) == 0: | ||
| print indentString + ('{} {}'.format(classDesc, view)) | ||
| #We call private method that gives back all visible accessibility children for view | ||
| accessibilityElements = fb.evaluateExpression('(id)[[[UIApplication sharedApplication] keyWindow] _accessibilityElementsInContainer:0 topLevel:%s includeKB:0]' % view) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| accessibilityElementsCount = int(fb.evaluateExpression('(int)[%s count]' % accessibilityElements)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| for index in range(0, accessibilityElementsCount): | ||
| subview = fb.evaluateExpression('(id)[%s objectAtIndex:%i]' % (accessibilityElements, index)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| printAccessibilityHierarchy(subview, indent + 1) | ||
| else: | ||
| print indentString + ('({} {}) {}'.format(classDesc, view, a11yLabel.GetObjectDescription())) | ||
|
|
||
| def accessibilityGrepHierarchy(view, needle): | ||
| foundLocally = False | ||
| a11yLabel = accessibilityLabel(view) | ||
|
|
||
| #if we don't have any accessibility string - we should have some children | ||
| if int(a11yLabel.GetValue(), 16) == 0: | ||
| #We call private method that gives back all visible accessibility children for view | ||
| accessibilityElements = fb.evaluateExpression('(id)[[[UIApplication sharedApplication] keyWindow] _accessibilityElementsInContainer:0 topLevel:%s includeKB:0]' % view) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| accessibilityElementsCount = int(fb.evaluateExpression('(int)[%s count]' % accessibilityElements)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| for index in range(0, accessibilityElementsCount): | ||
| subview = fb.evaluateExpression('(id)[%s objectAtIndex:%i]' % (accessibilityElements, index)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| foundLocally |= accessibilityGrepHierarchy(subview, needle) | ||
| elif re.match(r'.*' + needle + '.*', a11yLabel.GetObjectDescription(), re.IGNORECASE): | ||
| classDesc = objHelpers.className(view) | ||
| print('({} {}) {}'.format(classDesc, view, a11yLabel.GetObjectDescription())) | ||
|
|
||
| #First element that is found is copied to clipboard | ||
| if not foundLocally: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this will always be true, since
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. Did it correct way initially, but refactored wrong way
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Were you intending to rectify this in 55cca66?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this should work properly now |
||
| foundLocally = True | ||
| cmd = 'echo %s | tr -d "\n" | pbcopy' % view | ||
| os.system(cmd) | ||
|
|
||
| return foundLocally | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful to add the ability to print the accessibility value and traits if possible. If you had the values for them, adding an optional second argument to pa11y would be pretty easy.