简体   繁体   English

赛普拉斯验证 select 盒中的选项

[英]Cypress verify options in a select box

I have a select tag with no value attribute but only text.我有一个没有值属性但只有文本的select标记。 How can I validate the option list shown in the select box (text in this case).如何验证 select 框中显示的选项列表(本例中为文本)。

<select id="market-select" onchange="updatePageDataForMarket()">
  <option>Dallas-Fort Worth</option>
  <option>El Paso</option>
  <option>San Antonio</option>
</select>

You can try to manually trigger the input event/change event since those are the only ones it supports您可以尝试手动触发输入事件/更改事件,因为它们是唯一支持的

I am assuming you mean to validate each of the option text in the select.我假设您的意思是验证 select 中的每个选项文本。 If so, you could do this:如果是这样,你可以这样做:

const selectListOptions = ['Dallas-Fort Worth', 'El Paso', 'San Antonio'];
selectListOptions.forEach(item => cy.contains('#market-select', item));

If you want to ensure the items are in the correct order and have an exact match on the text string you could do:如果您想确保项目的顺序正确并且与文本字符串完全匹配,您可以执行以下操作:

const selectListOptions = ['Dallas-Fort Worth', 'El Paso', 'San Antonio'];
cy.get('#market-select option').each(($el, index) =>
  cy.wrap($el).should('have.text', selectListOptions[index])
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM