量角器黄瓜框架挂钩未执行

protractor cucumber framework hooks not executed

我是节点的新手,javascript,量角器和黄瓜也是。

我正在建立一个新的最小项目: - 执行一个小场景 - 生成精美的 html 报告

差不多好了。有一件事我无法解决:在 html 报告中添加屏幕截图。 我用 this.After 实现了 hooks.js,但它没有执行(根本没有 - 用 console.log 尝试过,但没有调用。)。

节点版本:6.9.5 npm 版本:3.10.10

正在从 IntelliJ 终端执行 window:

protractor e2e-tests/protractor.conf.js

我仔细阅读了所有相关的帖子,花了好几个小时,但完全卡住了。

请帮帮我!

代码如下:

package.json

 {
  "name": "autotest",
  "private": true,
  "version": "1.0.0",
  "description": "Automated test project",
  "repository": "",
  "license": "MIT",
  "devDependencies": {
    "chai": "3.5.0",
    "chai-as-promised": "6.0.0",
    "protractor": "^4.0.9",
    "protractor-cucumber-framework": "^3.1.2",
    "cucumber": "^2.3.1",
    "cucumber-html-report": "^0.6.0"
  },
  "scripts": {
    "test": "protractor e2e-tests/protractor.conf.js"
  }
}

sample.feature:

Narrative: any

Scenario: Protractor and Cucumber Test sample
Given I go to "https://angularjs.org"
When I add "Be Awesome" in the task field
And I click the add button
Then I should see my new task in the list

sample.spec.js

chai = require('chai');
chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);
expect = chai.expect;

const {defineSupportCode} = require('cucumber');

defineSupportCode(function ({Given, When, Then}) {

    Given(/^I go to "([^"]*)"$/, {timeout: 10 * 1000}, function (url) {
        return browser.get(url);
    });


    When(/^I add "([^"]*)" in the task field$/, function (textToType) {
        return element(by.model('todoList.todoText')).sendKeys(textToType);
    });


    When(/^I click the add button$/, function () {
        const el = element(by.css('[value="add"]'));
        return el.click();
    });

    Then(/^I should see my new task in the list$/, function () {
        const todoList = element.all(by.repeater('todo in todoList.todos'));
        expect(todoList.count()).to.eventually.equal(3);
        return expect(todoList.get(2).getText()).to.eventually.equal('Be Awesome');
    });
});

protractor.conf.js

var cucumberReportDirectory = 'reports';
var jsonReportFile = cucumberReportDirectory + '/results.json';

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    baseURL: 'http://localhost:8080/',
    framework: 'custom',
    frameworkPath: require.resolve('protractor-cucumber-framework'),
    capabilities: {
        'browserName': 'chrome'
    },

    // Spec patterns are relative to this directory.
    specs: [
        'features/*.feature'
    ],

    cucumberOpts: {
        require: [
            'steps/*.spec.js',
            'support/*js'
        ],
        tags: false,
        profile: false,
        format: [
            'json:' + jsonReportFile,
            'pretty']
    },

    onCleanUp: function () {
        var CucumberHtmlReport = require('cucumber-html-report');

        return CucumberHtmlReport.create({
            source: jsonReportFile,
            dest: cucumberReportDirectory,
            title: 'OptiRoute - Protractor Test Run',
            component: new Date().toString()
        }).then(console.log).catch(console.log);
    },
    ignoreUncaughtExceptions: true,
    untrackOutstandingTimeouts: true
};

hooks.js

module.exports = function () {

    this.Before(function (scenario, callback) {
        console.log("!!!!!!!EXECUTING  3  !!!!!!!!");
    });

    this.After(function (scenario, done) {
        console.log("!!!!!!!EXECUTING   2  !!!!!!!!");
        return browser.driver.manage().window().setSize(1280, 1000).then(function () { //reset size after each scenario
            return browser.driver.manage().window().setPosition(0, 0).then(function () { //reset position after each scenario
                //Attach any step screenshots to the scenario metadata
                for (var key in eachStepScreensArr) {
                    scenario.attach(eachStepScreensArr[key], 'image/png');
                }
            });
        });
        done();
    });

    this.StepResult((stepResult) => {
        console.log("!!!!!!!EXECUTING  1  !!!!!!!!");
        var step = stepResult.getStep();
        return browser.executeScript('return {' +
            'height: document.body.scrollHeight,' +
            'width: document.body.scrollWidth' +
            '}'
        ).then(function (result) {
            return browser.driver.manage().window().setSize(/*result.width*/1280, result.height + 50).then(function () {
                return browser.takeScreenshot().then(function (png) {
                    eachStepScreensArr.push(new Buffer(png.replace(/^data:image\/(png|gif|jpeg);base64,/, ''), 'base64'));
                });
            });
        });
    });
};

这是我的包结构:

e2e-tests
 - features
    - sample.feature
 - steps
    - sample.spec.js
 - support
    - hooks.js
 - protractor.conf.js
reports
package.json

使用您的设置,以下对我有效:

hooks.js

const {defineSupportCode} = require('cucumber');

defineSupportCode(function ({After}) {
    After(function (scenario, callback) {
        console.log('scenario', scenario);
        console.log("!!!!!!!EXECUTING  3  !!!!!!!!");
    });
});

这是附加屏幕截图所需的代码:

const {defineSupportCode} = require('cucumber');

defineSupportCode(function ({After}) {

    After(function (scenario) {
        var world = this;
        if (scenario.isFailed()) {
            return browser.takeScreenshot().then(function (screenShot) {
                // screenShot is a base-64 encoded PNG
                world.attach(screenShot, 'image/png');
            });
        }
    });
});