异步/等待解决不等待

时间:2018-08-16 20:40:05

标签: typescript protractor

我有以下测试用例,其中我正在获取值,添加过滤器,再次获取值并进行比较。如果它们不相等,则测试通过。这与控制流一起使用,但是由于量角器不赞成使用控制流,所以我被要求更新为异步/等待。

我遇到的问题是membersAfter在测试开始时就解决了,而没有等到应用过滤器后才解决。

listOfSimpleFilters.forEach((filter, index) => {
    it(`Test Each Filter Type With Cohorts ${index}`, async () => {
        const membersBefore = await cohortView.cohortMemberCount.getText()
        await cohortView.openFilters()
        await filterList.addSimpleFilters(filter)
        const membersAfter = await cohortView.cohortMemberCount.getText()
        await expect(membersBefore !== membersAfter).toBe(true, `The Members Before Filter: ${membersBefore} and the Members after Filter: ${membersAfter} are not different`)
    })
})

只是想知道如何使它真正等到应用过滤器以获取membersAfter金额之后。

我确实尝试过做Promise.all(),但这也不起作用。

2 个答案:

答案 0 :(得分:0)

尝试将代码放入“ it”块中。

一切都首先执行“ it”块

答案 1 :(得分:0)

问题出在.forEach()循环上。 一旦我将它们切换到这样的东西:

for (const filter of listOfSimpleFilters) {
    it(`Test Each Filter Type With Cohorts`, async () => {
        const membersBefore = await cohortView.cohortMemberCount.getText()
        await cohortView.openFilters()
        await filterList.addSimpleFilters(filter)
        const membersAfter = await cohortView.cohortMemberCount.getText()
        await expect(membersBefore !== membersAfter).toBe(true, `The Members Before Filter: ${membersBefore} and the Members after Filter: ${membersAfter} are not different`)
    })
}

我所遇到的问题消失了。