-
Notifications
You must be signed in to change notification settings - Fork 801
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 all commits
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]) | ||
|
|
||
| foundElement = False | ||
|
|
||
| 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.evaluateObjectExpression('[[UIApplication sharedApplication] keyWindow]') | ||
| foundElement = False | ||
| 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.evaluateExpressionValue('(id)[[UIDevice currentDevice] model]').GetObjectDescription().lower().find('simulator') >= 0: | ||
|
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. 👍 |
||
| 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.evaluateObjectExpression('[[[UIApplication sharedApplication] keyWindow] _accessibilityElementsInContainer:0 topLevel:%s includeKB:0]' % view) | ||
| 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.evaluateObjectExpression('[%s objectAtIndex:%i]' % (accessibilityElements, index)) | ||
| printAccessibilityHierarchy(subview, indent + 1) | ||
| else: | ||
| print indentString + ('({} {}) {}'.format(classDesc, view, a11yLabel.GetObjectDescription())) | ||
|
|
||
| def accessibilityGrepHierarchy(view, needle): | ||
| 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.evaluateObjectExpression('[[[UIApplication sharedApplication] keyWindow] _accessibilityElementsInContainer:0 topLevel:%s includeKB:0]' % view) | ||
| accessibilityElementsCount = fb.evaluateIntegerExpression('[%s count]' % accessibilityElements) | ||
| for index in range(0, accessibilityElementsCount): | ||
| subview = fb.evaluateObjectExpression('[%s objectAtIndex:%i]' % (accessibilityElements, index)) | ||
| 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 foundElement: | ||
| foundElement = True | ||
| cmd = 'echo %s | tr -d "\n" | pbcopy' % view | ||
| os.system(cmd) | ||
|
|
||
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.