-1

In Cypress, I am trying to write function to type in the editable field. If multiple elements found for the same locator, then enter the string in last editable field, else type the text in the detected editable field.

My below code works very well when multiple elements found for the same locator. But failed If there is single webelement located for locator.

module.exports. enterValue = (valueToType,ediatbleFieldLocator)=>{
    cy.log("Entering the  "+valueToType+"  in the editable field " +  ediatbleFieldLocator)
      cy.get(ediatbleFieldLocator).its(`length`). then($size=>{
        cy.log($size)
        if ($size === 1){
            cy.log("  editable field is found ")
            cy.get(ediatbleFieldLocator) .clear().should('be.empty')
            .should('be.enabled')
            .type(valueToType,{ log: false })
             }
        if ($size > 2) {
            cy.log(" multiple element of same locator is found ")
            cy.get(ediatbleFieldLocator)  .last() .clear().should('be.empty')
            .should('be.enabled')
            .type(valueToType,{ log: false })
        } else {
            throw new Error('Element is not found !');
        }
    })
}

The above code throws an error: enter image description here

Below code works good when single webelement is detected.

cy.get(ediatbleFieldLocator).clear().should('be.empty').should('be.enabled').type(valueToType,{ log: false })

I want to write function to handle all the possibilities like either single webelement detected or multiple. It should works. kindly please suggest..

Rama
  • 815
  • 2
  • 17
  • 37

1 Answers1

0

If you always want the last element when multiple elements are found, you can simply use .last() and not have to worry about if one or more elements are found. The behavior will be the same.

cy.get(ediatbleFieldLocator)
  .last()
  .clear()
  .should('be.empty')
  .and('be.enabled');
agoff
  • 5,818
  • 1
  • 7
  • 20
  • No, read the question again - the if statements have `$size === 1` and `$size > 2` but `$size === 2` is not covered, so **Element is not found** is thrown. – Achtung Apr 15 '23 at 21:48
  • `cy.get()` can yield multiple elements. In the case that multiple elements are yielded, `.first()`, `.last()`, and `.eq(n)` all behave the same way, relative to the size of the list yielded. In this case, `$size > 2` and `$size === 2` behave the same way. – agoff Apr 17 '23 at 13:16
  • How does `$size > 2` and `$size === 2` behave the same way? – Fay Fulbright Apr 18 '23 at 09:33
  • When Cypress yields only a singular element, the subsequent commands act on that singular element. When Cypress yields more than one element, the subsequent commands either a) act on the list of elements (such as `.each()`) or b) act on the first element. In this case, `.last()` operates on the list, so yielding two, three, four, etc. elements will still behave the same - Cypress grabs the last yielded element. OP asked for a method of acting on the last element when more than one was found. Using `.last()` in this way achieves that. – agoff Apr 18 '23 at 13:27