Search code examples
javascriptcypresse2e-testing

How do I check random numbers in cypress?


I have numbers that can be in a certain range in red square and somehow I need to check if the number in the square is in this range using cypress

enter image description here

cy.get('.details > .justui_scroll-area').contains('0.36')

cy.get('.details > .justui_scroll-area').contains('0.17')

cy.get('.details > .justui_scroll-area').contains('0.02')

cy.get('.details > .justui_scroll-area').contains('0')

cy.get('.details > .justui_scroll-area').contains('0')   

now there are specific numbers and I check them using contains


Solution

  • There is a chaijs range check within(low,high).

    Assume an individual element for each number, for example <li>

    cy.get('.details > .justui_scroll-area li')  // select all elements for numbers 
      .each($el => {
        const num = $el.text()
        expect(+num).to.be.within(0, 0.4)   // "+num" convert text to number
      })
    

    or

    cy.get('.details > .justui_scroll-area li')  
      .each($el => {
        const num = $el.text()
        cy.wrap(+num).should('be.within', 0, 0.4)
      })