黄瓜等待承诺完成

elc*_*rua 2 cucumber node.js nightwatch.js

I\xc2\xb4m 在使用 Cucumber 和 Nightwatch.js 时遇到问题。在我的一个功能中,第一步我发出 POST 请求来获取 ID,然后将该 ID 使用到我需要导航的 URL 中。我遇到的问题是,当我在另一步骤中得到响应时,我\xc2\xb4m ,因此测试失败,因为项目 i\xc2\xb4m 试图查看它不在\xc2\xb4t 那里。这是我正在采取的步骤的一个片段。

\n\n
    Given(/^I make a post request and retreive the Id i got the page + id$/, () => {\n  //I make the post request \n  var transactionIdPromise = utils.getTransactionId(1);\n  var url = "";\n\n  transactionIdPromise.getBody(\'utf8\').then(JSON.parse).done(function (result) {\n      var id = result.transaction_id;\n      url = "https://mywebpage.com:8443/" + id;\n      return client.url(url)\n  });\n});\n\n//request that returns id\nvar getTransactionId = function (user) {\n        return request(\'POST\', \'http://localhost:8888/internal/retreive_transaction/?\', {json: {user_id: user, type: \'UNIQUENESS\', info: \'{"person_id":"xxxxxxxxxxxxxxxx"}\'}});\n    };\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的问题是如何让它等待请求完成后再进入下一步?

\n

muc*_*i96 5

要使 Cucumber.js 等待承诺得到解决,您必须将其作为步骤定义的结果返回。return所以你需要在前面加上一个transactionIdPromise.getBody...