如何停止柏树中的循环 [英] How to stop loop in cypress
问题描述
我有一个循环检查40个项目. 当我找到第一个> 0的元素时,我想停止循环 这是我的代码
I have a loop checking 40 items. I want to stop my loop, when I am finding the first element, which > 0 This is my code
var genArr = Array.from({length:40},(v,k)=> k +1);
var genArr = Array.from({ length: 40 }, (v, k) => k + 1);
cy.wrap(genArr).each((index) => {
cy.get('.list-item').eq(index - 1).find('.number')
.invoke('text')
.then(text => {
const pendingCount = text;
cy.get('.list-item').eq(index - 1).click();
cy.get('.list-table').find('.list-table-list-item')
.should('have.length', pendingCount);
if (text > 0) break
});
});
});
但是我有一个语法错误.请帮助我,我该如何打破循环
But I have a syntax error. Help me pls, How can I break my loop
推荐答案
break
仅适用于本机for
/while
循环.
break
works only for native for
/while
loops.
要从.each
循环中提前退出(如注释中所建议),必须从传递给它的同一回调中返回false
,因此从嵌套的then
回调中返回false
没有效果.
To exit early from an .each
loop (as was suggested in the comments), the false
must be returned from the same callback you pass to it, so returning false
from the nested then
callback won't have an effect.
您甚至无法在then
回调中设置标志并在each
回调中进行检查,因为.each()
命令位于jQuery.fn.each
的最下方---它是同步的,并且在您使用时设置好标记后,所有迭代都将运行(并将嵌套命令排入队列).
You can't even set a flag in the then
callback and check it in the each
callback, because .each()
command is deep down just a jQuery.fn.each
--- it's synchronous and by the time you'd set up the flag, all iterations will have run (and enqueued the nested commands).
因此,唯一的选择是不使用.each()
,而使用某种递归命令.让我们来建立一个.
Thus, the only option is not to use .each()
, and use some kind of recursive command. Let's build one.
function walk ( arr, cb, index = 0 ) {
if ( !arr.length ) return;
arr = arr.slice();
const ret = cb(index, arr.shift());
((ret && ret.chainerId) ? ret : cy.wrap(ret))
.then( ret => {
if ( ret === false ) return;
return walk(arr, cb, index + 1);
});
}
/**
* Your callback should return/yield `false` if the loop is to exit early.
*/
Cypress.Commands.add('eachSeries', { prevSubject: 'optional' }, (subject, arr, cb) => {
return subject
// assume `arr` to be `cb`
? walk(subject, arr)
: walk(arr, cb);
});
用法:
describe('test', () => {
it('test', () => {
cy.document().then(doc => {
doc.body.innerHTML = `
<div class="list-item">0</div>
<div class="list-item">1</div>
<div class="list-item">2</div>
<div class="list-item">3</div>
`;
});
var genArr = Array.from({ length: 40 }, (v, k) => k + 1);
// the command can be chained on an existing subject
cy.wrap(genArr).eachSeries((index) => {
return cy.get('.list-item').eq(index)
.invoke('text')
.then(text => {
if (text > 1) return false;
});
});
// or it can start the chain (the array is passed as 1st arg)
cy.eachSeries(genArr, (index) => {
return cy.get('.list-item').eq(index)
.invoke('text')
.then(text => {
if (text > 1) return false;
});
});
// you can also return a promise
cy.eachSeries(['a', 'b', 'c'], (index, value) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(value === 'b' ? false : true);
}, 500);
});
});
// non-thenable callbacks work too
cy.eachSeries(['a', 'b', 'c'], (index, value) => {
return value === 'b' ? false : true;
});
});
});
在上面的前两个示例中,仅循环了前3个项目,然后循环较早退出.
In the first two examples above, only the first 3 items are looped through and then the loop exits early.
这篇关于如何停止柏树中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!