|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +"""Python environment for ATK a11y browser tests. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | + |
| 12 | +import psutil |
| 13 | + |
| 14 | +# pyatspi can't be installed using pip. Rely on the system installation. |
| 15 | +# Get the path to the system installation of pyatspi. |
| 16 | +pyatspiFile = subprocess.check_output( |
| 17 | + ( |
| 18 | + os.path.join(sys.base_prefix, "bin", "python3"), |
| 19 | + "-c", |
| 20 | + "import pyatspi; print(pyatspi.__file__)", |
| 21 | + ), |
| 22 | + encoding="utf-8", |
| 23 | +).rstrip() |
| 24 | +sys.path.append(os.path.dirname(os.path.dirname(pyatspiFile))) |
| 25 | +import pyatspi |
| 26 | + |
| 27 | +sys.path.pop() |
| 28 | +del pyatspiFile |
| 29 | + |
| 30 | + |
| 31 | +def getDoc(): |
| 32 | + """Get the Accessible for the document being tested.""" |
| 33 | + # We can compare the parent process ids to find the Firefox started by the |
| 34 | + # test harness. |
| 35 | + commonPid = psutil.Process().ppid() |
| 36 | + for app in pyatspi.Registry.getDesktop(0): |
| 37 | + if ( |
| 38 | + app.name == "Firefox" |
| 39 | + and psutil.Process(app.get_process_id()).ppid() == commonPid |
| 40 | + ): |
| 41 | + break |
| 42 | + else: |
| 43 | + raise LookupError("Couldn't find Firefox application Accessible") |
| 44 | + root = app[0] |
| 45 | + for embeds in root.getRelationSet(): |
| 46 | + if embeds.getRelationType() == pyatspi.RELATION_EMBEDS: |
| 47 | + break |
| 48 | + else: |
| 49 | + raise LookupError("Firefox root doesn't have RELATION_EMBEDS") |
| 50 | + doc = embeds.getTarget(0) |
| 51 | + child = doc[0] |
| 52 | + if child.get_attributes().get("id") == "default-iframe-id": |
| 53 | + # This is an iframe or remoteIframe test. |
| 54 | + doc = child[0] |
| 55 | + return doc |
| 56 | + |
| 57 | + |
| 58 | +def findByDomId(root, id): |
| 59 | + for child in root: |
| 60 | + if child.get_attributes().get("id") == id: |
| 61 | + return child |
| 62 | + descendant = findByDomId(child, id) |
| 63 | + if descendant: |
| 64 | + return descendant |
0 commit comments