.check() 勾选复选框或单选
语法:
.check()
.check(value)
.check(values)
.check(options)
.check(value, options)
.check(values, options)
使用方法:
cy.get('[type="checkbox"]').check() // 选中checkbox元素
cy.get('[type="radio"]').first().check() // 选中第一个radio元素
选中所有复选框
cy.get('[type="checkbox"]').check()
选中所有单选框
cy.get('[type="radio"]').check()
选中id为saveUserName的元素
cy.get('#saveUserName').check()
选中对应值
<form>
<input type="radio" id="ca-country" value="CA">
<label for="ca-country">Canada</label>
<input type="radio" id="us-country" value="US">
<label for="us-country">United States</label>
</form>
cy.get('[type="radio"]').check('US')
勾选值为’ subscribe ‘和’ accept '的复选框
<form>
<input type="checkbox" id="subscribe" value="subscribe">
<label for="subscribe">Subscribe to newsletter?</label>
<input type="checkbox" id="acceptTerms" value="accept">
<label for="acceptTerms">Accept terms and conditions.</label>
</form>
cy.get('form input').check(['subscribe', 'accept'])
选中一个不可见的复选框
通过在选项中设置force为true,可以忽略Cypress检查元素是否可见、可单击和未禁用的默认行为。
cy.get('.action-checkboxes').should('not.be.visible') // Passes
.check({ force: true }).should('be.checked') // Passes
选中name属性值为emailUser的元素
cy.get('form').find('[name="emailUser"]').check()