Вы должны написать рекурсивную функцию обещания и самостоятельно проверить цветной текст, попробуйте следующее
function checkColor(selector, color, maxWait, alreadyWaited = 0) {
const waitTime = 500;
// cy.get returns a thenebale
return cy.get(selector).each((item)=> {
// it checks the text right now, without unnecessary waitings
if(!item.text() === color) {
return false;
}
return true;
}).then(result => {
if(result) {
// only when the condition passes it returns a resolving promise
return Promise.resolve(true);
}
// waits for a fixed milliseconds amount
cy.wait(waitTime);
alreadyWaited++;
// the promise will be resolved with false if the wait last too much
if(waitTime * alreadyWaited > maxWait) {
return Promise.reject(new Error('Awaiting timeout'))
}
// returns the same function recursively, the next `.then()` will be the checkColor function itself
return checkColor(selector, color, maxWait, alreadyWaited);
});
}
// then, in your test...
cy.addParameter('blue'); //Will send graphQL query to fetch the correct items.
checkColor('data li', 'blue', 10000).then(result => {
cy.get('data li').each((item)=> {
cy.wrap(item).should('have.text', ' blue ');
});
// or
expect(result).to.be(true);
});
Я попытался прокомментировать как можно больше кода, рефакторинг кода, который я разработал для принятого аналогичный вопрос .
Дайте мне знать, если вам нужна дополнительная помощь ?
ОБНОВЛЕНИЕ
Мы ( меня и Томмазо ) написал плагин, чтобы помочь вам с такого рода проверками, его имя cypress-wait-before .
Пожалуйста, спасибо Сообщество Open Source Saturday для этого, мы разработали его во время одной из них в субботу