linux下allure.attach()添加的图片中文乱码

问题

本地执行截图正常,未出现中文乱码情况。

截图方法:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    test_rep = outcome.get_result()
    if test_rep.when == "call" and test_rep.failed:
        """
        用例失败了在failures文件里面添加一行 用例的nodeId + {参数}
        """
        file_name = "failures"
        file_mode = "a" if os.path.exists(file_name) else "w"
        with open(file_name, file_mode) as f:
            if "tmpdir" in item.fixturenames:
                flag_name = item.funcargs['tmpdir']
            else:
                flag_name = ""
            f.write(test_rep.nodeid + flag_name + '\n')

        """ allure中添加截图 """
        with allure.step("测试用例后,对当前页面进行截图"):
            allure.attach(base64.b64decode(CHROME_DRIVER.driver.get_screenshot_as_base64().encode('utf-8')),
                          '用例失败当前页面截图', allure.attachment_type.PNG)

报错信息

image

环境

在相同的配置下

  • Liunx 出现问题
  • Mac 未出现问题

可能系统编码的问题, 试一下下面的办法。

linux的默认编码是“UTF-8”;可以利用“locale”命令查看linux的默认编码,显示结果为“LANG=en_US.UTF-8”,语言是“en_US”(美式英语),编码是“UTF-8”。

修改/etc/sysconfig/i18n,如改成中文编码:

vim /etc/sysconfig/i18n

将LANG=“en_US.UTF-8” 改为 LANG=“zh_CN.GBK”。。保存并退出。

重启。

[root@localhost ~]# locale
LANG=zh_CN.GBK

修改配置,并且添加options.add_argument(’–lang=zh-CN’) 问题还是存在

给Linux安装中文字体就能解决Selenium截图出现乱码问题,百度搜吧