赛普拉斯:每个都有内部承诺 - 打破循环
Cypress: each with internal promise - break loop
我正在使用 Cypress,我想退出包含 promise 的 each 循环。
cy.get('div').find('div.myclass').each(el => {
cy.get(el).find('span#myclass').then(span => {
const myText = Cypress.$(span).clone().children().remove().end().text().trim();
if (myText === 'somethings') {
cy.get(el).as('mySection');
// ### HERE I WANT TO EXIT ###
}
});
});
有人可以帮助我吗?
在最后添加return false
即可退出该功能
cy.get('div').find('div.myclass').each(el => {
cy.get(el).find('span#myclass').then(span => {
const myText = Cypress.$(span).clone().children().remove().end().text().trim();
if (myText === 'somethings') {
cy.get(el).as('mySection')
return false
}
})
})
你可以return false
早点休息,见docs。
Return early
You can stop the .each() loop early by returning false in the callback function.
const selectMySection = {
html: `
<div class="myclass">
<span id="myid">
<p>child of span1</p>
<p>child of span2</p>
span text - find this
</span>
</div>
<div class="myclass">
<span id="myid">
<p>child of span3</p>
<p>child of span4</p>
span text - ignore this
</span>
</div>
`,
test: `
cy.get('div.myclass span#myid')
.each(($span, i) => {
console.log('processing span #', i); // only logs 'processing span # 0'
const text = Cypress.$($span).text()
if (text.includes('span text - find this')) {
cy.wrap($span)
.parent('div.myclass') // move to parent
.as('mySection')
return false;
}
})
cy.get('@mySection')
.then(x => console.log(x))
`
}
it('test selectMySection', () => {
cy.runExample(selectMySection)
})
循环的替代方法是使用 .contains('my text')
定位您想要的文本。
请注意 .contains()
进行部分匹配,因此您可以忽略子文本。
cy.get('div.myclass span#myid')
.contains('span text - find this')
.as('mySection')
我正在使用 Cypress,我想退出包含 promise 的 each 循环。
cy.get('div').find('div.myclass').each(el => {
cy.get(el).find('span#myclass').then(span => {
const myText = Cypress.$(span).clone().children().remove().end().text().trim();
if (myText === 'somethings') {
cy.get(el).as('mySection');
// ### HERE I WANT TO EXIT ###
}
});
});
有人可以帮助我吗?
在最后添加return false
即可退出该功能
cy.get('div').find('div.myclass').each(el => {
cy.get(el).find('span#myclass').then(span => {
const myText = Cypress.$(span).clone().children().remove().end().text().trim();
if (myText === 'somethings') {
cy.get(el).as('mySection')
return false
}
})
})
你可以return false
早点休息,见docs。
Return early
You can stop the .each() loop early by returning false in the callback function.
const selectMySection = {
html: `
<div class="myclass">
<span id="myid">
<p>child of span1</p>
<p>child of span2</p>
span text - find this
</span>
</div>
<div class="myclass">
<span id="myid">
<p>child of span3</p>
<p>child of span4</p>
span text - ignore this
</span>
</div>
`,
test: `
cy.get('div.myclass span#myid')
.each(($span, i) => {
console.log('processing span #', i); // only logs 'processing span # 0'
const text = Cypress.$($span).text()
if (text.includes('span text - find this')) {
cy.wrap($span)
.parent('div.myclass') // move to parent
.as('mySection')
return false;
}
})
cy.get('@mySection')
.then(x => console.log(x))
`
}
it('test selectMySection', () => {
cy.runExample(selectMySection)
})
循环的替代方法是使用 .contains('my text')
定位您想要的文本。
请注意 .contains()
进行部分匹配,因此您可以忽略子文本。
cy.get('div.myclass span#myid')
.contains('span text - find this')
.as('mySection')