Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Compare json file and dropdown values displayed in an inorderly way

I have my JSON file with the following text[there are around 100 values I have just put 4 here]

{
  label: ['a', 'b', 'c', 'd'],
}

I have my dropdown in my website which has these values but in a different order.

How can I compare that the text from the JSON and dropdown values are equal?

Also, I have a text "ALL" which is displayed in the dropdown by default and is not included in the JSON file. It's like this: enter image description here

In the dropdown, the rest of the values are in the JSON file.

I have done the below but it is not working.

cy.fixture('label.json')
    .then(function (category) {
           this.cat = category
      })
})
cy.get('#labels').each(($ele, i) => {
      expect($ele).to.have.text(this.category.label[i])
      })
})

Any help will be highly appreciated Thank you

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do something like this:

cy.fixture("labels.json").then((labels) => {
  cy.get("select#labels option").each(($ele, i) => {
    if ($ele.text().trim() == "ALL") {
      expect(labels.label).to.not.include($ele.text().trim())
    } else {
      expect(labels.label).to.include($ele.text().trim())
    }
  })
})

Test runner:

test runner screenshot

about 4 years ago · Juan Pablo Isaza Report

0

One way is to map the option elements to an array of label texts

HTML

<select name="labels" id="labels">
  <option value="" selected="">ALL</option>
  <option value="a">a</option> 
  <option value="c">c</option> 
  <option value="b">b</option> 
  <option value="d">d</option>
</select>

Test

cy.get('option')
  .then($options => Cypress.$.map($options, (option => option.innerText)))
  .then(optionLabels => {  

    // optionLabels is ['ALL', 'a', 'c', 'b', 'd']

    cy.fixture('my-labels.json'),then(fixture => {

      const expected = fixture.label.concat(['ALL'])  // add the ALL
      // expected is ['a', 'c', 'b', 'd', 'ALL']

      expect(optionLabels).to.have.members(expected)  // .to.have.members checks with any order
    })
  })

Result

enter image description here

When missing an option

enter image description here


Truncated option text

When your option text is truncated but you want to match to text in the fixture,

"clarif..." -> "clarification"

you can transform the fixture data before comparing.

Also, since there are 124 entries, it is better to just log any failures rather than the whole list.

<select>
  <option>ALL</option>
  <option>clarif...</option>
  <option>a</option>
  <option>d</option>
  <option>b</option>
</select>


cy.get('option')
  .then($options => Cypress.$.map($options, (option => option.innerText)))
  .then(console.log)
  .then(optionLabels => {

    cy.fixture('my-labels.json').then(fixture => {

      const truncate = (text) => text.length > 6 ? text.slice(0,6) + '...' : text;

      const expected = fixture.label.concat(['ALL'])      // add ALL
        .map(truncate)                 // convert fixture to short strings 

      const notInDropdown = expected.filter(exp => !optionLabels.includes(exp))
      const extraInDropdown = optionLabels.filter(label => !expected.includes(label))

      if (notInDropdown.length) {
        throw `Not in dropdown: ${notInDropdown.join(' ')}`
      }
      if (extraInDropdown.length) {
        throw `Extra in dropdown: ${extraInDropdown.join(' ')}`
      }
      cy.log('Dropdown passes')
    })
  })
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!