在我的protractor
项目中,我试图截取屏幕截图,并将其附加到我的html
报告中。截屏的过程发生在After
钩子中,如下所示:
import { Status,After, HookScenarioResult} from 'cucumber';
import {browser} from 'protractor';
import { async } from 'q';
After(async (scenario:HookScenarioResult)=> {
if(scenario.result.status===Status.FAILED){
const screenshot = await browser.takeScreenshot();
this.attach(screenshot,"image/png");
}
});
但在this.attach(screenshot,"image/png");
线上,它抱怨:
TypeError: this.attach is not a function
问题是什么?
我的配置是:
"cucumber": "^5.1.0",
"cucumber-html-reporter": "^4.0.4",
"protractor": "^5.4.2",
"protractor-cucumber-framework": "^6.1.1",
通过将脂肪功能改为正常功能,问题得以解决。我仍然,不明白为什么它会影响我的代码,但现在运行良好,我的html报告上有截图。
After(async function(scenario) {
if (scenario.result.status === Status.FAILED) {
// screenShot is a base-64 encoded PNG
const screenShot = await browser.takeScreenshot();
this.attach(screenShot, "image/png");
}
});
尝试以下代码为我工作:
After(function(scenarioResult) {
let self = this;
if (scenarioResult.result.status === Status.FAILED) {
return browser.takeScreenshot()
.then(function (screenshot) {
const decodedImage = new Buffer(screenshot.replace(/^data:image\/png;base64,/, ''), 'base64');
self.attach(decodedImage, 'image/png');
});
}
});