如何在柏树中迭代下拉列表的每个元素


<div class="version-select">
<label>Version</label>
<select id="version-select" name="version">
<option selected="selected" value="62e131f4c940e48cb85f56aa">10</option>
<option value="62e13221c940e48cb85f6f12">09</option>
<option value="62e1102946548a0eaecda47c">1.0.6</option>
</select>
</div>

我会一个接一个地选择每个元素,然后应用断言在Url中找到所选的选项文本。每次从下拉菜单中选择一个选项时,所选版本的门户页面都会加载到网站中,版本在URL中。

cy.get('#version-select').find('option').each(($opn)=>{
cy.log($opn)
let text = $opn.text()
text= text.replace('.','_')
cy.url().should("include",text)
})

在第一次迭代中,断言正在通过,但在第二次迭代中t失败,因为它得到了相同的上一个URl。

我认为您错过了触发页面导航的选择步骤。

试试这个:

cy.get('#version-select')
.find('option').each(($opn)=>{
const optionText = $opn.text()
const urlText = optionText.replace('.','_')
cy.get('#version-select')
.select(optionText)                 // select the option
cy.url().should("include", urlText)
})

IMO最好不要依赖.each()

这就是我想要尝试的

const versions = ['10', '09', '1.0.6'] 
versions.forEach(version => {
cy.get('#version-select').select(version)
cy.url().should("include", version.replace('.','_')) 
})

此外,由于页面正在更改,如果转换缓慢,您可能需要增加URL检查的超时时间。

在这种情况下,使用cy.location()而不是

const versions = ['10', '09', '1.0.6'] 
versions.forEach(version => {
cy.get('#version-select').select(version)
cy.location('href', {timeout:10000})
.should("include", version.replace('.','_')) 
})

最新更新