Angularjs 是否有人找到了一种方法,将列表编号添加到量角器描述块中?

Angularjs 是否有人找到了一种方法,将列表编号添加到量角器描述块中?,angularjs,protractor,jasmine-spec-reporter,Angularjs,Protractor,Jasmine Spec Reporter,在量角器测试中,我有大量的描述块。我得到了一页又一页正确缩进的测试输出,但是很难看到哪个测试是哪个测试以及测试进展了多远 是否有人试图在描述中添加列表编号。大概是这样的: 1. Main Page test 1.1 Test xxx 1.2 Test yyy 1.2.1 Describe in describe in describe test 2. XXX Page test 2.1 Test abc 请注意,这里点后的第一个数字和第二个数字可能是

在量角器测试中,我有大量的描述块。我得到了一页又一页正确缩进的测试输出,但是很难看到哪个测试是哪个测试以及测试进展了多远

是否有人试图在描述中添加列表编号。大概是这样的:

1.   Main Page test
   1.1  Test xxx
   1.2  Test yyy
      1.2.1 Describe in describe in describe test
2.   XXX Page test
   2.1  Test abc
请注意,这里点后的第一个数字和第二个数字可能是descripes与descripes的结果

您可以编写一个(并非如此)简单的“插件”来添加此功能。 我会避免替换原来的
descripe
it
功能。我的方法唯一的缺点是,您必须分别从descripe和it搜索并替换为lp.descripe和lp.it

(是的,如果您确信它不会影响其他任何事情,您可以直接覆盖原始的
描述
——不应该,但为了安全起见,请不要:)

我的方法经过更新,考虑到您可以在另一个
描述
中包含
描述

list-plugin.js

规格js

conf.js


也许最优雅的解决方案是更改量角器的代码。但如果需要升级库,则可能会出现问题

我想出了一个可行的解决方案,改为装饰量角器的描述。唯一需要注意的是,它要求规范代码正确缩进。实际上,这一限制可能被视为一个特性,因为可以肯定的是,正确缩进代码是一个很好的实践,对于当前的IDE来说,这只是一个2秒的任务。您可以通过调用
require('./量角器装饰器').resetCounter()重置计数器(例如,在每个规范的开头)

更新

如果您想装饰
it
只需调用
it=require('./grandor decorator.js')。decorateUsingErrorStack(it)或将其重构为单个方法

progrator-decorator.js模块:

您可以使用(>=1.1.0)和displaySuiteNumber选项来显示量角器测试的输出

输出示例:

1 first suite
  ✗ should failed
    - Expected true to be false.
  ✓ should be ok

2 second suite
  ✗ should failed
    - Expected true to be false.
  ✓ should be ok

  2.1 first child suite

    2.1.1 first grandchild suite
      ✗ should failed
        - Expected true to be false.
        - Expected true to be false.
      ✗ should failed
        - Expected true to be false.
      ✓ should be ok

我认为这是一个好的开始。我如何扩展它,使其提供多级编号,如在我的问题示例中,第二级和第三级是“描述”中“描述”的结果。您的答案有两个问题:
1。
需要使用自定义
lp。descripe
2。
它似乎工作不正常——这个简单的规范:产生这样的输出:(自己检查)<代码>1。
可能很容易克服,但在我看来,将当前方法更改为有效的解决方案并不是一项简单的任务。实际上,修改量角器源代码对任何人来说都应该是一个破坏者。如果您正在测试代码,您很可能还使用某种依赖关系管理器,它会自动使所有库保持最新。第二,正如我清楚地指出的,任何对JavaScript了解最少的人都可以实现,覆盖原始函数本身将是微不足道的。您可以看到更改是一个额外的函数调用,这是我立即发现的一个遗漏。@SergiuParaschiv确实——它似乎得到了解决。说实话,我昨天认为它不会工作,因此想发明一些黑客。当然,您的方法比使用
Error.stack
要好得多+1.1. 您没有处理“it”描述。2.如果我在另一个函数中包装“descripe”调用,缩进将减少一个,并且我总是“使用strict”并包装在匿名函数中。3.错误()。堆栈在Firefox中有不同的实现。这可能会引起问题。4.我将“Error.stackTraceLimit”设置为1,以减少这种方法对性能的明显影响(获取堆栈跟踪需要停止代码执行)。OP要求
描述
2。这是一个解决方案的种子,开始压痕不是问题,因为它可以很容易地改变???在Firefox中???你能详细说明一下(这些规格无论如何都不会在FF中运行)4。在这个阶段,我宁愿不关心性能(因为好处是非常有限的)。不,OP要求提供您的解决方案无法产生的特定输出。我的解决方案也是一个种子,但是看到您对一个完整的解决方案感兴趣,我扩展了它以涵盖其他场景。OP没有指定在什么环境下运行测试,但是如果她像我一样定期进行e2e测试,那么她很可能在很多不同的浏览器中进行测试。当您运行那么多的测试,很难跟踪输出时,您肯定不会在整个应用程序运行AJAX调用和所有其他e2e操作时阻止它。@SergiuParaschiv引用OP:有人试图在描述中添加列表编号吗。规范确实在服务器端运行,并通过webdriver与浏览器通信,因此完全没有提到FF。好的,我收回第(3)点,你是对的,
Error()。堆栈调用不会在测试的浏览器中执行。我仍然认为依赖代码格式是错误的,这是您的解决方案最重要的一个缺点。我们的两个解决方案都是不错的(在外界的帮助下,它总是变得更好),但有一些小缺点,我不是来质疑你的解决方案的。(+1用于提出错误堆栈想法)感谢您的建议。出于兴趣,您认为这是一个可以直接添加到Jasmine Spec Reporter的功能吗?我喜欢定制处理器的想法,但如果这些处理器可以作为配置功能使用的话,可能会更容易实现。Thanks我试图限制配置选项的数量,因此引入了定制处理器。但是你是对的,这个选项可能对其他用户有用。我将把它添加为配置功能。
var lp = require('./list-plugin.js');

lp.describe('Main Page test', function() {
    lp.it('Test xxx', function() {
        expect('a').toEqual('a');
    });

    lp.describe('Test yyy', function() {
        lp.it('Describe in describe test', function() {
            expect('a').toEqual('a');
        });
    });
});

lp.describe('XXX Page test', function() {
    lp.it('Test abc', function() {
        expect('a').toEqual('a');
    });
});
exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  jasmineNodeOpts: {
    isVerbose: true
  },
  specs: [
    'spec.js'
  ]
};
var stack = [];
var lastIndentColumn = 1;

function decorateUsingErrorStack(origDescribe){

    function describe(){
        var callerIndent, args;

        if(stack.length === 0){
            stack.push(0);
        }

        // from current stack we get the information about indentation of the code
        callerIndent = new Error().stack.split('\n')[2].split(':');
        callerIndent = parseInt(callerIndent[callerIndent.length-1]);

        if(callerIndent == lastIndentColumn){
            stack[stack.length-1] += 1;
        }
        else {
            if(callerIndent < lastIndentColumn){
                stack.pop();
                stack[stack.length-1] += 1;
            }
            else {
                stack.push(1);
            }
        }
        lastIndentColumn = callerIndent;

        args = Array.prototype.slice.call(arguments, 0);

        origDescribe.call(null, stack.join('.') + '.   ' + args[0], args[1]);
    }

    return describe;
}


module.exports = {
    decorateUsingErrorStack : decorateUsingErrorStack,
    resetCounter : function(){
        // this function should be called to start counting from 1.
        stack = [];
        lastIndentColumn = 1;
    }
} 
describe = require('./protractor-decorator.js').decorateUsingErrorStack(describe);

describe(' should be 1.', function(){

    describe('should be 1.1.', function(){
        it('xxx', function(){

        });

        describe('should be 1.1.1.', function(){
            it('xxx', function(){

            });

            describe('should be 1.1.1.1', function(){
                it('xxx', function(){

                });
            });

            describe('should be 1.1.1.2', function(){
                it('xxx', function(){

                });
            });

        });

        describe('should be 1.1.2.', function(){
            it('xxx', function(){

            });
        });

    });

    describe('should be 1.2.', function(){
        it('xxx', function(){

        });
    });

    describe('should be 1.3.', function(){
        it('xxx', function(){

        });
    });

});

// same as above but all starts with 2.
describe(' should be 2.', function(){...});
1 first suite
  ✗ should failed
    - Expected true to be false.
  ✓ should be ok

2 second suite
  ✗ should failed
    - Expected true to be false.
  ✓ should be ok

  2.1 first child suite

    2.1.1 first grandchild suite
      ✗ should failed
        - Expected true to be false.
        - Expected true to be false.
      ✗ should failed
        - Expected true to be false.
      ✓ should be ok