我想检查是否在字段 1 中选择了“法国”选项,然后在字段 2 中选择了“欧元”选项。
如果在字段 1 中选择了“美国”选项,那么在字段 2 中我选择了“美元”选项。
cy.get('State').then(($btn) => {
// assert on the text
if ($btn.text().to.equal('France')) {
cy.get('#vs3__combobox > .vs__actions').click() //Expands
cy.get('#dialog-2').contains('Euro').click() //Selects
} else {
cy.get('#vs3__combobox > .vs__actions').click() //Expands
cy.get('#dialog-2').contains('Dollars').click() //Selects
}
})
Run Code Online (Sandbox Code Playgroud)
但我无法正确理解,而且我是 Cypress / Javascript 的新手。
谢谢
我的方法要简单得多,用于invoke('val')获取 State 元素的当前值<select>。
value="FR"根据选项的属性,它可以是“FR”或“US”,也可以什么都不是。
cy.get('select#State')
.invoke('val') // what is selected
.then(selected => {
if (selected === 'FR') {
// do French stuff
}
if (selected === 'FR') {
// do USA stuff
}
if (selected === '') {
// default selected is the first option
cy.log('Nothing is selected')
}
})
Run Code Online (Sandbox Code Playgroud)
获取text()并<select>不能告诉您当前选择的值是什么。
供参考:Cypress 示例 (v13.3.3) - 选定值
| 归档时间: |
|
| 查看次数: |
76 次 |
| 最近记录: |