前言
近期在学习用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
来源:http://www.safe6.cn/
本文由 safe6 创作,著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
本站的所有程序和文章,仅限用于学习和研究目的;不得用于商业或者非法用途,否则,一切后果请用户自负!! 最后编辑时间为: 2019-10-25
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论