app多语言自动化测试对比
一、安装反编译软件
链接:https://pan.baidu.com/s/1ARYcQYnbYfZ3fjDquCregQ
提取码:o29h
1、打开文件,导入我们要测试的apk软件,找到res目录-下面便是多个国家多语言目录,取出string.xml文件(后面自动化对比测试会用到)
二、程序代码
环境问题(拿到代码+需要安装python3+openpyxl模块)
1、将我们的第一步反编译获取到的string.xml文件放到代码,对应的res下的目录,需要比对多个国家语言,代码目录就需要手动创建多个
2、程序执行文件
1)代码示例
1 from xml.dom.minidom import parse 2 import xml.dom.minidom 3 import openpyxl 4 5 # 不同国家对应的目录文件 6 CountryConfig = { 7 "zh":"values-zh", 8 "en":"values", 9 "af":"values-af", 10 "am":"values-am", 11 "ar":"values-ar", 12 "as":"values-as", 13 "az":"values-az", 14 "be":"values-be", 15 "bg":"values-bg", 16 "bn":"values-bn", 17 "bs":"values-bs", 18 "ca":"values-ca", 19 "cs":"values-cs", 20 "da":"values-da", 21 "de":"values-de", 22 "el":"values-el", 23 "rAU":"values-en-rAU", 24 "rCA":"values-en-rCA", 25 "rGB":"values-en-rGB", 26 "rIN":"values-en-rIN", 27 "rXC":"values-en-rXC", 28 "es": "values-es", 29 "rUS": "values-es-rUS", 30 "et": "values-et", 31 "eu": "values-eu", 32 "fa": "values-fa", 33 "fi":"values-fi", 34 "fr": "values-fr", 35 "fr-rCA": "values-fr-rCA", 36 "gl": "values-gl", 37 "gu": "values-gu", 38 "hi":"values-hi", 39 "hr":"values-hr", 40 "hu":"values-hu", 41 "hy":"values-hy", 42 "in":"values-in", 43 "is":"values-is", 44 "it":"values-it", 45 "iw":"values-iw", 46 "ja":"values-ja", 47 "ka":"values-ka", 48 "kk":"values-kk", 49 "km":"values-km", 50 "kn":"values-kn", 51 "ko":"values-ko", 52 "ky":"values-ky", 53 "lo":"values-lo", 54 "lt":"values-lt", 55 "lv":"values-lv", 56 "mk":"values-mk", 57 "ml":"values-ml", 58 "mn":"values-mn", 59 "mr":"values-mr", 60 "ms":"values-ms", 61 "my":"values-my", 62 "nb":"values-nb", 63 "ne":"values-ne", 64 "night":"values-night", 65 "nl":"values-nl", 66 "or":"values-or", 67 "pa":"values-pa", 68 "pl":"values-pl", 69 "pt":"values-pt", 70 "pt-rBR":"values-pt-rBR", 71 "pt-rPT":"values-pt-rPT", 72 "ro":"values-ro", 73 "ru":"values-ru", 74 "si":"values-si", 75 "sk":"values-sk", 76 "sl":"values-sl", 77 "sq":"values-sq", 78 "sr":"values-sr", 79 "sv":"values-sv", 80 "sw":"values-sw", 81 "ta":"values-ta", 82 "te":"values-te", 83 "th":"values-th", 84 "tl":"values-tl", 85 "tr":"values-tr", 86 "uk":"values-uk", 87 "ur":"values-ur", 88 "uz":"values-uz", 89 "vi":"values-vi", 90 91 "zh-rCN":"values-zh-rCN", 92 "zh-rHK":"values-zh-rHK", 93 "zh-rTW":"values-zh-rTW", 94 "zu":"values-zu" 95 96 } 97 # 多语言的xlsx中的国家排序,需要对比几个就列表里面就填写几个国家 98 CountryList = ["zh-rCN","en","af","am", #这里的国家和res目录下的文件夹一一对应,如果只对比3个国家语言,res只需要建3个文件夹 99 "ar","as","az","be","bg","bn"] 100 # "ca","cs","da","de","el","rAU","rCA","rGB","rIN","rXC","es","rUS", 101 # "et","eu","fa","fi","fr","fr-rCA","gl","gu","hi","hr","hu","hy","in","is","it","iw","ja","ka","kk","km","kn","ko","ky","lo" 102 # "lt","lv", "mk","ml", "mn","mr","ms","my","nb","ne","night","nl","or","pa", 103 # "pl","pt","pt-rBR","pt-rPT","ro","ru","si","sk","sl","sq","sr","sv","sw","ta", 104 # "te","th","tl","tr","uk","ur","uz","vi","zh","zh-rCN","zh-rHK","zh-rTW","zu"] 105 106 107 def getXML(Country): 108 # 使用minidom解析器打开 XML 文档 109 file = "res/%s/strings.xml" % CountryConfig.get(Country) 110 DOMTree = xml.dom.minidom.parse(file) 111 collection = DOMTree.documentElement 112 113 # 在集合中key 114 content = collection.getElementsByTagName("string") 115 return content 116 117 def getXMLValue(content, name): 118 for item in content: 119 if name == item.getAttribute("name"): 120 return item.childNodes[0].data #获取第一个子节点数据 121 122 123 def getAllXMLValue(keyName, testPrint=None): 124 valueList = [] 125 for country in CountryList: 126 content = getXML(country) 127 value = getXMLValue(content,keyName,) 128 if testPrint: 129 print(country, ": ", value) 130 valueList.append(value) 131 # print(valueList) 132 return valueList 133 134 # 获取单个key的 所有国家值 135 def excel01(): 136 #result_test02.xlsx 需要对比的源文件 137 book = openpyxl.load_workbook('data/result_test02.xlsx') 138 # print(book) 139 # 打开工作区 140 sh1 = book.get_sheet_by_name(name="Sheet1") 141 list1 = [] 142 for i in range(2,sh1.max_row): 143 get_name=sh1.cell(i,1).value 144 # print(get_name) 145 list1.append(get_name) 146 # print(list1) 147 return list1 148 149 def excel02(): 150 result=[] 151 for y in range(len(excel01())): 152 x=getAllXMLValue(excel01()[y], 2) 153 result.append(x) 154 # print(x) 155 return result 156 157 def excel03(): 158 #打开excel文件result_test01.xlsx 159 #解析后保存多语言结果文件 160 book = openpyxl.load_workbook('data/result_test01.xlsx') 161 print(book) 162 # 打开工作区 163 sh2 = book.get_sheet_by_name(name ="Sheet1") 164 data = excel02() 165 166 i = 1 167 r = 1 168 for line in data: 169 for col in range(1, len(line) + 1): 170 sh2.cell(row=r+1, column=col).value = line[col - 1] 171 i += 1 172 r += 1 173 book.save('data/result_test01.xlsx') 174 175 if __name__ == "__main__": 176 excel03()
data目录说明:
1、result_test02.xlsx(需求方或者提供的标准多语言示例)
2、result_test01.xlsx 文件 执行自动化测试代码后,会向result_test01.xlsx表写入数据
三、通过excel对比小工具,对比两份result_test01.xlsx和result_test02.xlsx文件
对比后对的文件内容显示
注意事项:
result_test01.xlsx 文件需要手动加入key值这一列,且两份excel数据表格式保持一致才能进行对比,对比失败日志文件会有提示
程序代码地址:
链接:https://pan.baidu.com/s/1hDdlawelqZXxFZIUyBERZw
提取码:nn2u
本文来自博客园,作者:橘子偏爱橙子,转载请注明原文链接:https://www.cnblogs.com/xfbk/p/15079033.html