如何在异步方法中使用一些方法?

时间:2019-01-10 13:59:52

标签: javascript html automated-tests cypress

在赛普拉斯中,我想创建一个方法,如果条件合适的话,该方法将运行其他方法。

代码:

sizeOfTableShouldBeChangedAfterResizingIfThereAreManyData() {
    cy.get('.footerWrapper').then(div => {
        if(div.find('.pagination').length) {
            cy.get('.pagination > .page').its('length').then(numberOfPages => {
                if(numberOfPages > 3) {
                    this.numberOfRowsShouldBeEqualAtLeast(10)
                    this.changeNumberOfDisplayedRowsByIndex(1)
                    this.numberOfRowsShouldBeEqualAtLeast(20)
                    this.changeNumberOfDisplayedRowsByIndex(0)
                    this.numberOfRowsShouldBeEqualAtLeast(10)

                    return this
                }
            })
        } 
    })
    return this
}

我从赛普拉斯收到一条消息:

CypressError: cy.then() failed because you are mixing up async and sync code.

In your callback function you invoked 1 or more cy commands but then returned a synchronous value.

Cypress commands are asynchronous and it doesn't make sense to queue cy commands and yet return a synchronous value.

You likely forgot to properly chain the cy commands using another cy.then().

The value you synchronously returned was: '{}'

我从以下位置阅读了文档:

https://docs.cypress.io/guides/overview/why-cypress.html#In-a-nutshell

但是我不确定应该如何正确地编写它。使用这种方法后,我想同步调用其他方法。

有人知道我该怎么做吗?

1 个答案:

答案 0 :(得分:0)

我自己解决了一个问题,实际上并不需要第一次返回,这会引起问题:-)