allure report 修改默认语言为中文

在生成好的测试报告中的index.html 文件内容在"script src=“app.js”></script" 上一行插入以下js代码:

        <script>
        // 尝试从 localStorage 中获取 ALLURE_REPORT_SETTINGS 的值
        let allureSettings = JSON.parse(localStorage.getItem('ALLURE_REPORT_SETTINGS'));
    
        if (allureSettings) {
            // 如果能获取到值,则修改 language 属性为 "zh"
            allureSettings.language = "zh";
        } else {
            // 如果获取不到值,则创建一个新对象并设置默认值
            allureSettings = {
                "language": "zh",
                "sidebarCollapsed": false,
                "sideBySidePosition": [46.83064516129034, 53.16935483870967]
            };
        }
    
        // 将修改后的对象或新创建的对象存储回 localStorage
        localStorage.setItem('ALLURE_REPORT_SETTINGS', JSON.stringify(allureSettings));
    
        console.log("当前设置", JSON.stringify(allureSettings));
    </script>

为了方便测试完成后不用每次手动修改index.html,则用python自动生成js设置文件到测试报告中,再用python修改index.html文件内容把js设置文件引入到index.html文件代码中, 其中directory=‘…/allure-report/’ 是自己项目生成的allure report 根路径,运行测试报告后,调用对应函数修改内容


def create_settings_js_file(directory='../allure-report/', filename='settings.js'):
    # 创建或确认目录存在
    Path(directory).mkdir(parents=True, exist_ok=True)

    # 定义JS内容
    js_content = """  
    // 尝试从 localStorage 中获取 ALLURE_REPORT_SETTINGS 的值  
    let allureSettings = JSON.parse(localStorage.getItem('ALLURE_REPORT_SETTINGS'));  

    if (allureSettings) {  
        // 如果能获取到值,则修改 language 属性为 "zh"  
        allureSettings.language = "zh";  
    } else {  
        // 如果获取不到值,则创建一个新对象并设置默认值  
        allureSettings = {  
            "language": "zh",  
            "sidebarCollapsed": false,  
            "sideBySidePosition": [46.83064516129034, 53.16935483870967]  
        };  
    }  

    // 将修改后的对象或新创建的对象存储回 localStorage  
    localStorage.setItem('ALLURE_REPORT_SETTINGS', JSON.stringify(allureSettings));  
    console.log("当前设置", JSON.stringify(allureSettings));  
    """

    # 写入文件
    with open(os.path.join(directory, filename), 'w') as file:
        file.write(js_content)
    print(f"{filename} 文件已创建并写入内容。")

# 修改index.html文件并引入settings.js
def insert_script_tag(new_script_tag):
    """ 在 Allure 报告的 index.html 中的指定位置插入一个新的 <script> 标签,如果该标签已存在则不插入
    @param new_script_tag: 需要插入的新 <script> 标签内容, 例如: '<script src="settings.js"></script>'
    @return: 没有返回内容
    """
    report_filepath = r"../allure-report/index.html"
    tag_to_find = '<script src="app.js"></script>'

    # 首先确保 new_script_tag 格式正确
    assert new_script_tag.startswith('<script') and new_script_tag.endswith('</script>')

    with open(report_filepath, 'r+', encoding="utf-8") as f:
        lines = f.readlines()
        f.seek(0)
        f.truncate()

        should_insert = True  # 初始设置为需要插入
        for line in lines:
            if new_script_tag in line:
                should_insert = False  # 如果新标签已存在,则不需要插入
                break
            if tag_to_find in line and should_insert:
                f.write(new_script_tag + '\n')  # 在找到的位置插入新标签,并且仅当需要插入时
            f.write(line)
        f.close()
 # 生成设置成默认为中文的js文件
 create_settings_js_file()
 # 在修改index.html代码,引入设置文件
 insert_script_tag('<script src="settings.js"></script>')
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据引用\[1\]中提供的信息,allure的安装是通过下载zip包并将其解压到指定文件夹的方式进行的。而要将allure默认改成中文,可以参考引用\[2\]中提到的将content的值转成unicode的方法。具体步骤如下: 1. 打开allure的安装文件夹,找到allure的配置文件(通常是allure.properties或allure.yml)。 2. 打开配置文件,找到语言设置的选项。 3. 将语言设置的值改为中文对应的unicode编码。可以使用在线工具将中文转换为unicode编码。 4. 保存配置文件并重新启动allure。 这样,allure默认语言就会变成中文了。请注意,这个方法是基于allure的配置文件进行修改的,所以在使用之前请备份好配置文件以防止意外情况发生。 #### 引用[.reference_title] - *1* *3* [Jenkins集成Allure报告(在python中执行pytest)](https://blog.csdn.net/TengYu456/article/details/117676776)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [详细讲解修改allure报告自定义的logo和名称中文](https://blog.csdn.net/qq_42412061/article/details/126047877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值