Search code examples
cypress

Changing the pre selected value of a dropdown to value before it


enter image description here

In cypress i am trying to get the above day dropdown to - 1 day from the current pre populated day (so in example i would need to get the 16th selected) not sure how to get cypress to select the current value minus one

enter image description here

just thinking logically if it would need to grab the current day -1 and then re select the correct date but not sure how to get cypress to do this in a simple way. Fairly new to cypress (maybe obvious)TIA

cy.get('#ctl00_Main_EndDay').invoke('value').as('Day') cy.get('#ctl00_Main_EndDay').type('@Day'-1)


Solution

  • You can do it by replacing .as() with .then().

    Alias .as() is not for this situation.

    Also change

    • value is probably (maybe always) a string since it comes from the web page. Add parseInt() conversion to make it a number, then you can subtract 1 from it.

    • your picture shows <select> + <option> elements, so maybe .select() command is better than .type() command

    cy.get('#ctl00_Main_EndDay')
      .invoke('val')
      .then(dayString => {
        const day = parseInt(dayString)
        cy.get('#ctl00_Main_EndDay')
          .select(day-1)
      })