前言
近期在学习用airtest进行自动化测试,遇到的坑来记录一下。
问题
用pycharm学习airtest的时候发现任何poco的函数都会直接报错raise JSONDecodeError("Expecting value", s, err.value) from None异常,进行debug后发现问题出在minicap.py文件的get_display_info方法里面。
get_display_info代码
def get_display_info(self):
display_info = self.adb.shell("%s -i" % self.CMD)
display_info = json.loads(display_info)
display_info["orientation"] = display_info["rotation"] / 90
# 针对调整过手机分辨率的情况
actual = self.adb.shell("dumpsys window displays")
arr = re.findall(r'cur=(\d+)x(\d+)', actual)
if len(arr) > 0:
display_info['physical_width'] = display_info['width']
display_info['physical_height'] = display_info['height']
# 通过 adb shell dumpsys window displays | find "cur="
# 获取到的分辨率是实际分辨率,但是需要的是非实际的
if display_info["orientation"] in [1, 3]:
display_info['width'] = int(arr[0][1])
display_info['height'] = int(arr[0][0])
else:
display_info['width'] = int(arr[0][0])
display_info['height'] = int(arr[0][1])
return display_info
这个方法会被传一条命令进来
LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/minicap -i ; echo ---$?---
执行之后会返回这些
WARNING: linker: /data/local/tmp/minicap has text relocations. This is wasting memory and prevents security hardening. Please fix.
{
"id": 0,
"width": 540,
"height": 960,
"xdpi": 240.00,
"ydpi": 240.00,
"size": 4.59,
"density": 1.50,
"fps": 60.00,
"secure": true,
"rotation": 0
}
很明显里面多了一句警告,导致了**json.loads(display_info)**时候报错,下面我们改造一下这段代码。
添加如下两行
index = display_info.index("{")
display_info = display_info[index:len(display_info)]
最后代码如下
def get_display_info(self):
display_info = self.adb.shell("%s -i" % self.CMD)
index = display_info.index("{")
display_info = display_info[index:len(display_info)]
display_info = json.loads(display_info)
display_info["orientation"] = display_info["rotation"] / 90
# 针对调整过手机分辨率的情况
actual = self.adb.shell("dumpsys window displays")
arr = re.findall(r'cur=(\d+)x(\d+)', actual)
if len(arr) > 0:
display_info['physical_width'] = display_info['width']
display_info['physical_height'] = display_info['height']
# 通过 adb shell dumpsys window displays | find "cur="
# 获取到的分辨率是实际分辨率,但是需要的是非实际的
if display_info["orientation"] in [1, 3]:
display_info['width'] = int(arr[0][1])
display_info['height'] = int(arr[0][0])
else:
display_info['width'] = int(arr[0][0])
display_info['height'] = int(arr[0][1])
return display_info