Javascript withData和before在WebDrivero中同时调用。如何先调用before,然后调用with data?

Javascript withData和before在WebDrivero中同时调用。如何先调用before,然后调用with data?,javascript,mocha.js,webdriver-io,Javascript,Mocha.js,Webdriver Io,我有一个从UI获取数据的数据提供者。为了从UI获取数据,我使用beforehook打开url并执行所需的操作。但withData和before同时被调用;因此,dataprovider具有导致故障的“未定义”值 describe('abcd', function(){ before(function(){ //get data }); withData(data, function(value){ it('abccd', func

我有一个从UI获取数据的数据提供者。为了从UI获取数据,我使用beforehook打开url并执行所需的操作。但withData和before同时被调用;因此,dataprovider具有导致故障的“未定义”值

describe('abcd', function(){
     before(function(){
         //get data
     });
     withData(data, function(value){
         it('abccd', function(){
           },)
     });
});
如何首先从用户界面获取数据,然后将其传递给数据提供者?

3件需要检查的事情

首先,确保以同步方式获取数据,或者在处理异步代码之前先执行
。请在此处阅读:

第二,我不知道
with data
是如何工作的,但是你可以用一种方式嵌套你的测试,在调用
之前的
之后,让摩卡调用
with 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
             });
         });
     });
});