1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import Logger from '../utils/Logger' 17 18export default class AccessibilityUtils { 19 constructor() { 20 } 21 22 static async getAttributeValue(accessibilityElement, key) { 23 Logger.info("AccessibilityUtils getAttributeValue") 24 if (accessibilityElement == null) { 25 Logger.error("AccessibilityUtils accessibilityElement is null") 26 return null 27 } 28 29 let keys = await accessibilityElement.attributeNames() 30 let value = undefined 31 let isExit = false 32 for (let keyString of keys) { 33 if (key == keyString) { 34 isExit = true 35 } 36 } 37 if (isExit) { 38 try { 39 value = await accessibilityElement.attributeValue(key) 40 } catch (error) { 41 Logger.error("AccessibilityUtils Failed to get attributeValue of " + key + ". Cause: " + JSON.stringify(error)) 42 } 43 } 44 return value 45 } 46 47 static async getElementWantedInfo(accessibilityElement, wantedAttribute) { 48 Logger.info("AccessibilityUtils getElementAllInfo") 49 if (accessibilityElement == null) { 50 Logger.error("AccessibilityUtils accessibilityElement is null") 51 return null 52 } 53 54 let info = "" 55 let value = undefined 56 for (let name of wantedAttribute) { 57 value = await AccessibilityUtils.getAttributeValue(accessibilityElement, name); 58 info = info.concat(name + ": " + value + " ") 59 } 60 return info 61 } 62 63 static async createElementArray(accessibilityElement) { 64 Logger.info("AccessibilityUtils createElementArray") 65 let resultArray = [] 66 if (accessibilityElement == null) { 67 Logger.error("AccessibilityUtils accessibilityElement is null.") 68 return resultArray 69 } 70 71 let stack = [] 72 stack.push(accessibilityElement) 73 let tmpElement 74 while (stack.length > 0) { 75 tmpElement = stack.pop() 76 resultArray.push(tmpElement) 77 let childElements = await AccessibilityUtils.getAttributeValue(tmpElement, "children") 78 if (childElements) { 79 for (let childElement of childElements) { 80 if (childElement != null) { 81 stack.push(childElement) 82 } 83 } 84 } else { 85 Logger.error("AccessibilityUtils this node does not have childElement") 86 } 87 } 88 return resultArray 89 } 90 91 static async findElementByKey(elementArray, targetElement) { 92 Logger.info("AccessibilityUtils findElementByKey") 93 if (elementArray == null || elementArray.length == 0 || targetElement == null) { 94 return null 95 } 96 97 let value = undefined 98 for (let element of elementArray) { 99 value = await AccessibilityUtils.getAttributeValue(element, targetElement.KEY) 100 if (value == targetElement.VALUE) { 101 return element 102 } 103 } 104 Logger.error("AccessibilityUtils Failed to find target element.") 105 return null 106 } 107}