【问题标题】:How to clean up after failed Intern functional tests?实习生功能测试失败后如何清理?
【发布时间】:2017-12-19 21:20:44
【问题描述】:

我有一些使用 Intern (3) 运行的功能测试,测试的最后一步是进行一些清理,包括清除远程浏览器上的 localStorage,并通过立即存储窗口句柄切换回原始窗口并在测试结束之前切换回它(因此任何后续测试都不会失败,因为如果前一个测试在另一个测试结束时它们试图在关闭的窗口上运行)。但是,如果某些 chaijs 断言在 .then() 中失败,则最后的清理代码将被跳过。有没有更好的方法来清理即使在某些断言失败时仍会运行的功能测试?

this.leadfoot.remote
    .get(require.toUrl(url))
    .execute(function() { return document.querySelector('#welcome').textContent})
    .then(welcome => {
        assert.equal(welcome, 'hello world', 'properly greeted');
    })
    .execute(function() {
        localStorage.clear();
    });

如果断言失败,它永远不会在最后清除 localStorage,如果下一个运行的测试期望 localStorage 为空,它也会失败。功能测试后有没有更好的清理方法?

【问题讨论】:

    标签: javascript functional-testing intern


    【解决方案1】:

    使用afterEach 方法。

    afterEach() {
        return this.remote
            .execute(function () {
                localStorage.clear();
            });
    },
    
    'my test'() {
        return this.remote
            .get(...)
            .execute(...)
            .then(welcome => {
                assert.equal(welcome, ...);
            });
    }
    

    【讨论】:

    • Leadfoot 有clearLocalStorage() 功能,但我不能让它工作。这就是你使用这种方法的原因吗?
    • 是的,clearLocalStorage 依赖于 webdriver 的实现,并不是所有的 webdriver 都实现了该命令。如果驱动程序没有实现本地存储命令,Leadfoot 应该 已经知道并运行类似于上面的代码的东西,但它目前没有。 (github.com/theintern/leadfoot/issues/120)
    【解决方案2】:

    在我们的项目中,我们这样做:

    afterEach: function() {
            var command = this.remote;
            if(intern.variables.currentCase && !intern.variables.currentCase.hasPassed) {
                command = command.execute(function () {
                   localStorage.clear();
                });
            }
            return command;
        },
    

    只有在测试失败时才会执行本地 Storage.clear():

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 1970-01-01
      • 2014-05-21
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 2010-11-11
      相关资源
      最近更新 更多