|
| 1 | +import acacia_atspi |
| 2 | +import json |
| 3 | +from .protocol import (PlatformAccessibilityProtocolPart) |
| 4 | + |
| 5 | +# When running against chrome family browser: |
| 6 | +# self.parent is WebDriverProtocol |
| 7 | +# self.parent.webdriver is webdriver |
| 8 | + |
| 9 | +def findActiveTab(root): |
| 10 | + stack = [root] |
| 11 | + while stack: |
| 12 | + node = stack.pop() |
| 13 | + |
| 14 | + if node.getRoleName() == 'frame': |
| 15 | + relations = node.getRelations() |
| 16 | + if 'ATSPI_RELATION_EMBEDS' in relations: |
| 17 | + index = relations.index('ATSPI_RELATION_EMBEDS') |
| 18 | + target = node.getTargetForRelationAtIndex(index) |
| 19 | + print(target.getRoleName()) |
| 20 | + print(target.getName()) |
| 21 | + return target |
| 22 | + continue |
| 23 | + |
| 24 | + for i in range(node.getChildCount()): |
| 25 | + child = node.getChildAtIndex(i) |
| 26 | + stack.append(child) |
| 27 | + |
| 28 | + return None |
| 29 | + |
| 30 | +def serialize_node(node): |
| 31 | + node_dictionary = {} |
| 32 | + node_dictionary['role'] = node.getRoleName() |
| 33 | + node_dictionary['name'] = node.getName() |
| 34 | + node_dictionary['description'] = node.getDescription() |
| 35 | + node_dictionary['states'] = sorted(node.getStates()) |
| 36 | + node_dictionary['interfaces'] = sorted(node.getInterfaces()) |
| 37 | + node_dictionary['attributes'] = sorted(node.getAttributes()) |
| 38 | + |
| 39 | + # TODO: serialize other attributes |
| 40 | + |
| 41 | + return node_dictionary |
| 42 | + |
| 43 | +def find_node(root, dom_id): |
| 44 | + stack = [root] |
| 45 | + while stack: |
| 46 | + node = stack.pop() |
| 47 | + |
| 48 | + attributes = node.getAttributes() |
| 49 | + for attribute_pair in attributes: |
| 50 | + [attribute, value] = attribute_pair.split(':', 1) |
| 51 | + if attribute == 'id': |
| 52 | + if value == dom_id: |
| 53 | + return node |
| 54 | + |
| 55 | + for i in range(node.getChildCount()): |
| 56 | + child = node.getChildAtIndex(i) |
| 57 | + stack.append(child) |
| 58 | + |
| 59 | + return None |
| 60 | + |
| 61 | +class AcaciaPlatformAccessibilityProtocolPart(PlatformAccessibilityProtocolPart): |
| 62 | + def setup(self): |
| 63 | + self.product_name = self.parent.product_name |
| 64 | + self.root = None |
| 65 | + self.errormsg = None |
| 66 | + |
| 67 | + self.root = acacia_atspi.findRootAtspiNodeForName(self.product_name); |
| 68 | + if self.root.isNull(): |
| 69 | + error = f"Cannot find root accessibility node for {self.product_name} - did you turn on accessibility?" |
| 70 | + print(error) |
| 71 | + self.errormsg = error |
| 72 | + |
| 73 | + |
| 74 | + def get_accessibility_api_node(self, dom_id): |
| 75 | + if self.root.isNull(): |
| 76 | + return json.dumps({"role": self.errormsg}) |
| 77 | + |
| 78 | + active_tab = findActiveTab(self.root) |
| 79 | + |
| 80 | + # This will fail sometimes when accessibilty is off. |
| 81 | + if not active_tab or active_tab.isNull(): |
| 82 | + return json.dumps({"role": "couldn't find active tab"}) |
| 83 | + |
| 84 | + # This fails sometimes for unknown reasons. |
| 85 | + node = find_node(active_tab, dom_id) |
| 86 | + if not node or node.isNull(): |
| 87 | + return json.dumps({"role": "couldn't find the node with that ID"}) |
| 88 | + |
| 89 | + return json.dumps(serialize_node(node)) |
| 90 | + |
| 91 | + |
0 commit comments