简体   繁体   English

如何在量角器中对测试用例失败进行截图

[英]How to take screenshot in protractor on failure of test cases

I am new to the protractor and would like to take screenshots of my failed test cases in browsers.我是量角器的新手,想在浏览器中截取我失败的测试用例的屏幕截图。

Can you please help me out by advising how should I go about it?你能帮我建议我应该怎么做吗?

Thank you :)谢谢:)

You can use protractor-jasmine2-screenshot-reporter module for this, it has some good features which would serve your purpose.您可以protractor-jasmine2-screenshot-reporter使用protractor-jasmine2-screenshot-reporter模块,它有一些很好的功能protractor-jasmine2-screenshot-reporter您的目的。

 var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');

 var reporter = new HtmlScreenshotReporter({
 dest: 'target/screenshots',
 filename: 'my-report.html',
 captureOnlyFailedSpecs: true
});

This will capture screenshots when your specs have failed, you have many more options, you can checkout this link : https://www.npmjs.com/package/protractor-jasmine2-screenshot-reporter这将在您的规范失败时捕获屏幕截图,您有更多选择,您可以查看此链接: https : //www.npmjs.com/package/protractor-jasmine2-screenshot-reporter

The code below can be placed within the exports.config block of protractor.conf.js :下面的代码可以放在protractor.conf.js的exports.config 块中:

const fs = require('fs');
. . . 
exports.config = {
. . . 
onPrepare: function() {
jasmine.getEnv().addReporter({
  specDone: function(result) {
    browser.takeScreenshot().then(function(screenShot) {

      //    Saving File.
      //    Param filePath : where you want to store screenShot
      //    Param screenShot : Screen shot file which you want to store. 

      fs.writeFile(filePath, screenShot, 'base64', function (err) {
      if (err) throw err;
      console.log('File saved.');
      });

    });
  }
});
}

I hope it helps!我希望它有帮助! :) :)

Reference link 参考链接

Similarly, if you want it to only take screenshots on failures, you can place the call to takeScreenshot within a conditional matching a result.failedExpectations.length greater than 0:类似地,如果您希望它仅在失败时截取屏幕截图,您可以将调用 takeScreenshot 置于匹配 result.failedExpectations.length 大于 0 的条件中:

jasmine.getEnv().addReporter({
  specDone: (result) => {
    if(result.failedExpectations.length > 0) {
      browser.takeScreenshot().then((screenShot) => {
        fs.writeFile('./protractorFailure.png', screenShot, 'base64', (err) => {
          if (err) throw err;
        });
      });
    }
  }
});

protractor-beautiful-reporter is capable of building nice html reports including the screenshots. protractor-beautiful-reporter能够构建漂亮的 html 报告,包括屏幕截图。

Installation:安装:

npm install protractor-beautiful-reporter --save-dev

Configuration in protractor.conf.js : protractor.conf.js配置:

const HtmlReporter = require('protractor-beautiful-reporter');

exports.config = {
   // your config here ...

   onPrepare: function() {
      jasmine.getEnv().addReporter(new HtmlReporter({
         baseDirectory: 'target/screenshots',
         takeScreenShotsOnlyForFailedSpecs: true
      }).getJasmine2Reporter());
   }
}

If you're using framework: 'custom' in protractor.conf.js then using Jasmine or a Jasmine reporter doesn't seem to work.如果您在 protractor.conf.js 中使用framework: 'custom'那么使用 Jasmine 或 Jasmine 报告器似乎不起作用。

You can do something like the following in your post-test hook (eg cucumber.After):您可以在测试后挂钩中执行以下操作(例如黄瓜.After):


  protractor.browser.takeScreenshot().then(function(screenshot) {
    const screenshots = path.join(process.cwd(), 'e2e/reports/screenshots');

    fs.writeFile(screenshots + '/test.png', screenshot, 'base64', function (err) {
      if (err) {
          throw err;
      }
      console.log('File saved.');
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM