如何使用实习生js导入外部图书馆?

问题描述:

我正在使用internjs使用internjs来处理Web应用程序的UI自动化,我试图直接将功能测试的测试报告转储到Excel文件中。我想要做的是:如何使用实习生js导入外部图书馆?

  1. 在Excel中创建功能测试的测试报告文件
  2. 采取截图如果测试失败或跳过。
  3. 将excel报告和屏幕截图压缩(zip)成一个zip文件。

为了做到这一点,我需要安装第三方库archiver

现在我陷入了一个非常基本的问题,即我无法将库导入到自定义记者中。那么请问一些机构可以帮助我介绍如何导入外部库实习项目。

我使用下面的命令安装归档: npm install archiver --save

我的代码去如下:

define([ 
    'intern/dojo/node!fs', 
    '../TestUtils/ExcelUtil', 
    '../TestUtils/SnapShotUtil', 
    'archiver' //<= import archiver 
], function (fs, ExcelUtil, SnapShotUtil, archiver) { 
    var Excel = ExcelUtil.instance(); 
    var screenshotDir = 'test-reports/screenshot/'; 
    function TestReporter(config) { 
    config = config || {}; 
} 
TestReporter.prototype = { 

    suiteError: function (suite) { 
     SnapShotUtil.takeSnapShot(suite, suite.name + '-' + suite.error.name) 
    }, 
    testPass: function (test) { 
     Excel.addRow([test.parent.name, test.name, 'N/A', 'Pass', 'N/A']) 
    }, 
    testSkip: function (test) { 
     var picName = screenshotDir + test.parent.name.replace(/ /g, '') + '-' + 
      test.name.replace(/ /g, '') + '.png' 
     Excel.addRow([test.parent.name, test.name, 'N/A', 'Skipped', '=HYPERLINK(' + picName + ',ScreenShot)']) 
     SnapShotUtil.takeSnapShot(test, picName) 

    }, 

    testFail: function (test) { 
     var picName = screenshotDir + test.parent.name.replace(/ /g, '') + '-' + 
      test.name.replace(/ /g, '') + '-' + test.error.name + '.png' 
     Excel.addRow([test.parent.name, test.name, test.error.name, 'Failed', '=HYPERLINK(' + picName + ',ScreenShot)']) 
     SnapShotUtil.takeSnapShot(test, picName) 

    } 
}; 
return TestReporter; 

});

但我发现了以下错误:

Error: Failed to load module archiver from 
    D:/Users/sshrestha/Documents/APps/newAuto/advtestautomation/archiver.js 
(parent: Automation/ConfigFiles/TestReporter.js) 
    at ReadFileContext.callback <node_modules\intern\node_modules\dojo\loader.ts:831:119> 
    at FSReqWrap.readFileAfterOpen [as oncomplete] <fs.js:303:13> 

我的文件夹结构去如下

enter image description here

能有人帮。

归档分布为CommonJS的模块,所以你需要使用Dojo /节点插件(就像你使用“FS”做)将其导入:

intern/dojo/node!archiver 
+0

你的意思是我们不” ñ需要单独安装存档器? – CodeBlooded

+0

您仍然需要安装归档程序。与归档程序包中JS模块的格式有关的问题。 'define(['archiver'],...)'命令将尝试加载归档器作为AMD模块。但是,归档模块使用CommonJS格式,所以它们必须使用加载器插件加载,如'define(['intern/dojo/node!archiver'],...)''。 – jason0x43