gpt4 book ai didi

javascript - 等待 Cypress each() 函数完成

转载 作者:行者123 更新时间:2023-12-04 00:14:15 25 4
gpt4 key购买 nike

我正在调用函数 populateArray,它使用 children() 函数在页面上选取元素。我想将属性值存储到使用 each() 函数中。这是我正在做的

static populateArray(){
let arr = []

cy.xpath(NODE_PREVIEW_PANEL).children(NODE_TYPE)
.each((element, index, list) => arr.push(cy.wrap(element).invoke('attr', 'data-testid')))
}

问题是当我通过将它分配给一个变量来调用这个函数时

actualArray = ArticlePage.populateArray()

它不等待底层的 each() 函数完全完成。它只是选择部分值(value)和 yield 。我希望 actualArray 仅在 populateArray 完全解析后才具有值。

最佳答案

使用 Cypress.Promise 怎么样?来自文档

Cypress is promise aware so if you return a promise from inside of commands like .then(), Cypress will not continue until those promises resolve

const populateArray = () => {
return new Cypress.Promise((resolve, reject) => {
let arr = []
cy.xpath('//ul')
.children('li')
.each(element => arr.push(element.attr('data-testid'))) // NOTE different push
.then(() => {
return resolve(arr)
})
})
}

调用等待(测试必须是异步的),

it('gets the array', async () => {

const actualArray = await ArticlePage.populateArray();
expect(actualArray).to.deep.equal(['1', '2', '3']); // whatever is in 'data-testid'

})

关于javascript - 等待 Cypress each() 函数完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65030658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com