将外部javascript注入html脚本标记以获取cobertura代码覆盖率结果

问题描述 投票:1回答:1

this类似,我想要注入html外部的js文件。

我的问题的背景 - 我使用cobertura为我的Angular6应用程序生成代码覆盖。在我的vsts构建中,我发布了代码覆盖率结果,但是当需要触发.js文件时单步执行文件链接时,我在浏览器中出现错误:

Blocked script execution in '<URL>' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

我认为最好的解决方案是执行一个脚本将javascript代码直接注入html而不是外部链接到它。

所以例如说html是这样的:

<html>

   <script src="code.js"></script>
...
</html>

然后运行像injector.js这样的脚本:node ./injector.js

html将成为:

<html>

   <script>
      function hello() {
         console.log('hello');
      }
   </script>
</html>

你怎么能这样做?

还是有更好的解决方案来解决这个问题?

javascript angular code-coverage azure-pipelines cobertura
1个回答
0
投票

由于您的开发完全基于NodeJs。

您只需编写nodejs脚本即可

  1. 使用Glob模块使用coverage文件夹中的模式获取所有HTML文件
  2. 循环遍历每个生成的报告HTML并内联CSS和脚本。
  3. 保存
  4. 然后将代码覆盖率报告发布为工件。

以下NPM模块确实完成了内联工作,几乎没有优化https://www.npmjs.com/package/vsts-coverage-styles

免责声明:我写了上面提到的简单可重用的npm包。

© www.soinside.com 2019 - 2024. All rights reserved.