【问题标题】:get file name from Protractor test从量角器测试中获取文件名
【发布时间】:2019-09-18 15:27:27
【问题描述】:

我的套件中有许多测试,我想在运行测试时打印出文件名,或者在所有测试失败中包含文件名。

我可以在browser.getProcessedConfig() 中使用console.log(config.specs) 打印出规格,但它只显示glob 模式,而我想要完整的文件名。我试过config.suites,但它会打印出所有套件,而不仅仅是测试中运行的那个。

conf.shared.ts

await browser.getProcessedConfig().then(async function(config: any) {
        });
        console.log(config.specs)
        console.log(config.suites)
        return config.specs;

config.specs 的输出

 '../../../../lib/tests/home/**/*.myparam*.js'

config.suites 的输出

 mysuite:
    '../../../../lib/tests/home/**/market/**/*.*.myfilename.js',
  mysuite2:
        '../../../../lib/tests/home/**/market2/**/*.*.myfilename.js',
  mysuite3:
        '../../../../lib/tests/home/**/market3/**/*.*.myfilename.js',
  mysuite4:
        '../../../../lib/tests/home/**/market4/**/*.*.myfilename.js'

【问题讨论】:

    标签: typescript protractor mocha.js


    【解决方案1】:

    在你的conf.js 中添加这个到onPrepare

    onPrepare() {
    
            /**
             *  Set global environment configuration
             */
            Object.defineProperty(global, '__stack', {
                get: function () {
                    let orig = Error.prepareStackTrace;
                    Error.prepareStackTrace = function (_, stack) {
                        return stack;
                    };
                    let err = new Error;
                    Error.captureStackTrace(err, arguments.callee);
                    let stack = err.stack;
                    Error.prepareStackTrace = orig;
                    return stack;
                }
            });
    
            // returns name of the file from which is called
            Object.defineProperty(global, '__file', {
                get: function () {
                    let path = __stack[1].getFileName();
                    try { //*nix OSes
                        return path.match(/[^\/]+\/[^\/]+$/)[0];
                    } catch (error) { //Windows based OSes
                        return path.match(/[^\\]+\\[^\\]+$/)[0];    
                    }
                }
            });
            // returns function name from which is called
            Object.defineProperty(global, '__function', {
                get: function () {
                    return __stack[1].getFunctionName();
                }
            });
            // returns line number of the position in code when called
            Object.defineProperty(global, '__line', {
                get: function () {
                    return __stack[1].getLineNumber();
                }
            });
    
        },
    

    然后如果您在框架中的任何位置(规范或页面对象)运行它

    console.log(__file);
    console.log(__function);
    console.log(__line);
    

    你会得到类似的东西

    // test.spec.js
    // funcName()
    // 56
    

    显然您可以根据需要调整代码

    【讨论】:

    • 上述解决方案给我错误:Cannot find name '__stack'。您还可以解释一下上述解决方案与仅使用console.log(__filename) 有何不同?
    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多