Selecting elements using if, else

These were added to a custom command file and were used to determine whether or not the first rate card is disabled. If so, select the second rate card. Otherwise, select the first rate card

Cypress.Commands.add('selectStallRate', () => {
 // CHECK RATE BUTTON STATUS. IF FIRST ONE DISABLED, SELECT THE SECOND
 // OTHERWISE, SELECT THE FIRST RATE CARD
 cy.wait(1500)
 cy.get('[data-testid="product-row-0-stalls-button"]').then(($Stalls)
=> {
 if ($Stalls.hasClass('Mui-disabled')) {
 cy.get('[data-testid="product-row-1-stalls-button"]').click()
 } else {
 cy.get('[data-testid="product-row-0-stalls-button"]').click()
 }
 })
 // END SELECT FOR STALLS
})
Cypress.Commands.add('selectRVRate', () => {
 // CHECK RATE BUTTON STATUS. IF FIRST ONE DISABLED, SELECT THE SECOND
 // OTHERWISE, SELECT THE FIRST RATE CARD
 cy.wait(1500)
 cy.get('[data-testid="product-row-0-rvs-button"]').then(($RVs) => {
 if ($RVs.hasClass('Mui-disabled')) {
 cy.get('[data-testid="product-row-1-rvs-button"]').click()
 } else {
 cy.get('[data-testid="product-row-0-rvs-button"]').click()
 }
 })
 // END SELECT FOR RVS
})

Usage example:

 it('1-Logs in as venue admin, books RV spots only, cancels', () => {
 cy.venueadmin()
 cy.contains('CREATE NEW').click()
 cy.get('[name="renterInformation.email"]')
 .click()
 .type('[REDACTED]')
 .wait(1000)
 cy.contains('[REDACTED]@[REDACTED].com').click()
 //Search for and select event
 cy.get('[id="mui-component-select-EVENT"]').click()
 cy.contains('[REDACTED]').click() //Event Name
 //Enable Stalls Toggle
 const stallsToggle = cy.get('[class="MuiSwitch-root"]')
 stallsToggle.eq(0).click()
 //Stalls Section
 cy.get('[name="stalls.quantity"]').type(2)
 // ****************** SETS THE DATES ************************
 cy.get('[name="start"]').type(myCurrentDate, { force: true })
 cy.get('[name="end"]').type(myEndDate, { force: true })
 // ************************************************************
 cy.selectStallRate()
Previous
Previous

Exact text matches

Next
Next

Checking CSS of images with dynamic environments