Protractor、Jasmine 和在第一次失败时停止测试

Protractor, Jasmine, and stopping test on first fail

在尝试弄清楚如何使某些 jasmine expect 语句依赖于先前的 expect 语句时,我发现在 Jasmine 2.3.0 之前,没有办法。 (参见 Stop jasmine test after first expect fails)但是,Jasmine 2.3.0 添加了一个选项 stopSpecOnExpectationFailure,当设置为 true 时将在第一次失败时停止测试。

对这个前景感到兴奋,我修改了我的 conf.js 以包括选项:

/*
 * conf.js
 */
exports.config = {
    framework: 'jasmine',
    specs: ['search-spec.js'],
    useAllAngular2AppRoots: true,
    jasmineNodeOpts: {
        stopSpecOnExpectationFailure: true
    }
};

但这对我不起作用。

在我的测试中,我有:

/**
 * Test option from select element is selected
 */
function verifyOptionSelected(option) {
   var myEl = $('select[value="' + option + '"]';

   expect(myEl.isPresent()).toBe(true, 'Option, ' + option + ', is not a value in the dropdown list. (It might be the text.)');

   expect(myEl.isSelected()).toBe(true, 'Option, ' + option + ', is not selected as expected.');
}

在上面的代码中,将尝试两个 expect 语句,但如果第一个失败,我不需要尝试第二个。

你们中有人用茉莉花解决了这个问题吗?

(是的,我知道 jasmine-bail-fast and protractor-fail-fast。但是,在我看来,使用内置功能是更好的解决方案。)

根据我在 protractorjasmine-npm (the jasmine runner that protractor uses) source code, it is not as simple as adding the stopSpecOnExpectationFailure or stopOnFailure jasmine node option to the configuration. There has to be changes applied to protractor to support the new stopSpecOnExpectationFailure jasmine node option. Please create an issue/feature request in Protractor issue tracker 中看到的内容。


我也试过调用 throwOnExpectationFailure 函数,在 onPrepare() 中设置 stopSpecOnExpectationFailure 值 throw jasmine.getEnv() 但 none 有效。也许,我遗漏了一些东西,有一种方法可以启用该设置。

执行npm install protractor-fail-fast

然后在protractor.conf.js

const failFast = require('protractor-fail-fast');

...
exports.config = {
  plugins: [
    failFast.init(),
  ],
  afterLaunch: function () {
    failFast.clean(); // Removes the fail file once all test runners have completed.
  },
}