感谢@RichardMatsen和Arnon Axelrod,以下是对我有用的递归函数。
/* Recursive function */
function getEnvironment() {
function getEnvironmentInternal(retires) {
if (retires == 0) {
throw "text didn't appear after the specified retires";
}
return cy.get('header > p').invoke('text').then(text => {
if(text) {
return cy.wrap(text);
}
cy.wait(10000);
cy.reload();
return getEnvironmentInternal(retires-1);
});
}
return getEnvironmentInternal(12);
}
/* Usage */
getEnvironment().then(text => {
cy.log("User Id: " + text);
});