withData 和 before 在 webdriverio 中同时被调用。如何先调用before再调用withData?

withData and before are invoked at same time in webdriverio. How to invoke before first and then withData?

我有一个从 UI 获取数据的数据提供者。为了从 UI 获取数据,我使用 before hook 打开 url 并执行所需的操作。但是 withData 和 before 是同时调用的;因此 dataprovider 具有 'undefined' 导致失败的值。

describe('abcd', function(){
     before(function(){
         //get data
     });
     withData(data, function(value){
         it('abccd', function(){
           },)
     });
});

如何实现先从UI获取数据,再传给dataprovider?

3 件事要检查...

首先, 确保您以同步方式获取数据或使 before 处理异步代码。在这里阅读:Mocha Asynchronous Code

其次,我不知道 withData 是如何工作的,但是你可以嵌套你的测试,使 Mocha 在之后调用 withData呼叫 before.

第三, 确保你在正确的范围内使用 data 而不是不小心得到了不同的。

根据这些建议,您的代码可能类似于:

describe('abcd', function() {
     var data = null; //declare data in a scope usable by `before` and `withData` functions

     before(function() {

         // get data synchronously
         data = 'some data';

         // or...

         //return a promise so the tests don't start before the promise resolves
         return getData().then(function (someData) {
           data = someData;
         })
     });

     // nested tests that will start only after `before` function finished executing
     describe('with data', function () {
         withData(data, function(value) {
             it('abccd', function() {
                //test
             });
         });
     });
});