实习生功能测试和清除 cookie 之前和之后

Intern functional testing and clearing cookies with before and after

本文关键字:之后 清除 功能测试 实习生 cookie      更新时间:2023-09-26
使用

intern 编写一系列功能测试,我尝试使用 Before 和 After 方法来清除所有 cookie 和本地存储的数据,以便每个测试都干净开始。

这是我尝试使用的cleanEnvironment函数,位于名为utils的模块中:

cleanEnvironment: function(name) {
    name = name || 'NONE';
    // window params
    var defaultHeight = 768;
    var defaultWidth  = 1024;
    if (this.remote.environmentType.webStorageEnabled === true) {
        this.remote
            .clearSessionStorage()
            .clearLocalStorage();
    }
    return this.remote
        .setWindowSize(defaultWidth, defaultHeight)
        .getCookies()
        .then(
            function(cookies) {
                console.log('in ', name);
                console.log('Existing cookies that will be cleared are  ', cookies);
            }
        )
        .clearCookies()
        .then(
            function(cookies) {
                console.log('in ', name);
                console.log('Existing cookies are  ', cookies);
            }
        );
},

这是我尝试在之前/之后的方法中调用它的方式:

after: function() {
    console.log('timestamp for login after start is ', Date.now());
    utils.cleanEnvironment.call(this, 'login before');
    console.log('timestamp for login after finish is ', Date.now());
},

终于意识到(并重读了这样说的文档)我无法保证多个套件之前和之后的顺序,因为我不会在这些模块中返回 Promise。但是我正在努力编写一个承诺,允许我使用此外部模块utils所以我不会在每个套件中重复代码。

成功通过并返回对this.remote的正确引用的 before/after 方法中的 promise 会是什么样子?我在这些方法中没有找到任何承诺的例子,到目前为止,我要么在未定义this.remotecleanEnvironment 函数中出现错误,要么浏览器从未加载测试 URL,我认为这意味着我永远不会解决承诺。

这是我的尝试之一:

        after: function() {
            var self = this;
            return new Promise(function(resolve, reject) {
                console.log('timestamp for login after start is ', Date.now());
                utils.cleanEnvironment.call(self, 'login before');
                console.log('timestamp for login after finish is ', Date.now());
                resolve();
            });
        },

确定我完全错过了关于 Promise 的一些明显的东西,但是在盯着这段代码 7 个小时后,我对它是什么视而不见。

命令类似于 promise,可用于异步操作,因此您可以在 after 方法中返回 cleanEnvironment 函数的结果。

after: function () {
    return utils.cleanEnvironment(...)
}

您还应该注意维护cleanEnvironment中的单个链。在原始cleanEnvironment中,可以启动两个独立的链,并且只返回第二个链。如果由于某种原因,第一个比第二个运行的时间更长,实习生不会等待它完成。要保持链的连续性,请执行以下操作:

var chain = this.remote;
if (this.remote.environmentType.webStorageEnabled === true) {
    chain = this.remote
        .clearSessionStorage()
        .clearLocalStorage();
}
return chain
    .setWindowSize(defaultWidth, defaultHeight)
    .getCookies()
    // ...