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

时间:2017-06-23 13:08:09

标签: protractor hook cucumberjs

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

我正在设置一个新的最小项目: - 执行一个小方案 - 生成精彩的HTML报告

几乎不错。有一点我无法解决:在HTML报告中添加screnshots。 我用this.After实现了hooks.js,但它没有被执行(根本没有 - 尝试使用console.log,但没有被调用。)。

节点版本:6.9.5 npm版本:3.10.10

从IntelliJ终端窗口执行:

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

1 个答案:

答案 0 :(得分:0)

使用您的设置,以下内容适用于我:

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');
            });
        }
    });
});