.check() 勾选复选框或单选
语法:
1 2 3 4 5 6 | .check() .check(value) .check(values) .check(options) .check(value, options) .check(values, options) |
使用方法:
1 2 | cy.get('[type="checkbox"]').check() // 选中checkbox元素 cy.get('[type="radio"]').first().check() // 选中第一个radio元素 |
选中所有复选框
1 | cy.get('[type="checkbox"]').check() |
选中所有单选框
1 | cy.get('[type="radio"]').check() |
选中id为saveUserName的元素
1 | cy.get('#saveUserName').check() |
选中对应值
1 2 3 4 5 6 | <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> |
1 | cy.get('[type="radio"]').check('US') |
勾选值为’ subscribe ‘和’ accept '的复选框
1 2 3 4 5 6 | <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> |
1 | cy.get('form input').check(['subscribe', 'accept']) |
选中一个不可见的复选框
通过在选项中设置force为true,可以忽略Cypress检查元素是否可见、可单击和未禁用的默认行为。
1 2 | cy.get('.action-checkboxes').should('not.be.visible') // Passes .check({ force: true }).should('be.checked') // Passes |
选中name属性值为emailUser的元素
1 | cy.get('form').find('[name="emailUser"]').check() |