1

For the following html,

             <select class="cell dropdown" data-test="Visibility-warn">
                <option value="">Select...</option>
                <!---->
                <option value="0"><!---->Good<!----></option>
                <!---->
                <option value="1"><!---->Moderate/Good<!----></option>
                <!---->
                <option value="2"><!---->Moderate<!----></option>
                <!---->
                <option value="3" selected=""><!---->Moderate/Poor<!----></option>
                <!---->
                <option value="4"><!---->Poor<!----></option>
                <!---->
            </select>

I have written the cypress code to select by value and not by text. As per docs, both select by text and select by value can be done in the same way.

        cy.get('[data-test="Visibility-warn"]')
                .should('be.enabled')
                .select('3')

which works fine.

But on the other hand, I am trying to create a custom command that does not work.

 cy.selectAndVerify('[data-test="Visibility-warn"]', weatherLimits.Visibility.warn, 'Moderate/Poor');
 //weatherLimits.Visibility.warn = gives the value 3

And in Commands:

Cypress.Commands.add('selectAndVerify', (selector, textToSelect, valueToVerify) => {
  cy.log(textToSelect)
  cy.get(selector)
    .should('be.enabled')
    .select(textToSelect)
  //.should('have.text', valueToVerify);
})

How can I possibly make the custom command work?

enter image description here

2
  • 1
    At which line of code that test is failing ? Is it this .select(textToSelect) ? Commented Sep 15, 2021 at 10:33
  • Yes, that's correct Commented Sep 15, 2021 at 10:57

2 Answers 2

1

What I think is happening is weatherLimits.Visibility.warn is passing a number that is 3 but cypress is expecting an string. so it should be .select(textToSelect + "")

Cypress.Commands.add(
  "selectAndVerify",
  (selector, textToSelect, valueToVerify) => {
    cy.log(textToSelect)
    cy.get(selector)
      .should("be.enabled")
      .select(textToSelect + "")
      .should("have.text", valueToVerify)
  }
)
Sign up to request clarification or add additional context in comments.

2 Comments

Ahh, I got it. But it works with .select(textToSelect+"") and not with .select("textToSelect") as this one will not convert to text from number but considers textToSelect as a value itself.
Yes true. The idea is to change the number to string. I will update my answer.
0

Though @Alapan Das answer is already accepted, adding an answer that rightly does the "verify" part.

Cypress.Commands.add('selectAndVerify', (selector, textToSelect, valueToVerify) => {
  cy.get(selector)
    .should('be.enabled')
    .select(textToSelect + "")
  cy.get(selector).find('option:selected').should('have.text', valueToVerify)
})

and in test

cy.selectAndVerify('[data-test="Visibility-warn"]', weatherLimits.Visibility.warn, 'Moderate/Poor');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.