简体   繁体   中英

Cypress verify options in a select box

I have a select tag with no value attribute but only text. How can I validate the option list shown in the select box (text in this case).

<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. 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])
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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