cy.document().its('body')
will give you a subject that is outside the .within()
, and it seems to go back to inner scope after (still within callback).
cy.get('body').find('div.without'); // checking this query first (outer scope)
cy.get('div.myform').within(() => {
cy.contains('text within'); // inner scope
cy.document().its('body').find('div.without'); // outer scope
cy.contains('text within'); // inner scope
})
Tested with this html
<body>
<div class="myform">
<div>text within</div>
</div>
<div class="without">text without</div>
</body>
Nested withins
You can nest .within()
statements using the same breakout pattern,
cy.get('div.scope1')
.within(() => {
cy.contains('text within scope1'); // testing in scope1
cy.document().its('body').find('div.scope2')
.within(() => {
cy.contains('text within scope2'); // switch to scope2
})
cy.contains('text within scope1'); // back to scope1
})
Tested with this html
<body>
<div class="scope1">
<div>text within scope1</div>
</div>
<div class="scope2">
<div>text within scope2</div>
</div>
</body>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…