Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cypress – Condition depending on json list

I have a json list containing some several hundreds of entries. I want to check whether the list contains a specific entry.

I tried using .contains as a chainer, but this is not accepted by Cypress:

const permission_list = require('../../fixtures/permissions/permissions_admin.json')

describe('Test plan creator for Administrator', () => {
    it('create a test plan', () => {
        cy.get(permission_list[0].granted_permissions).then((list) => {
            if (list.contains('dyna'))
            {cy.log('Yes')}
            else
            {cy.log('No')}

        })
    })
})

This leads to the error message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError
list.contains is not a function

{edit} I also tried .includes instead of .contains with the same result

Just for information, this is what the json list looks like (the list I am interested is the list "granted permissions")

[
  {
    "name": "administrator",
    "title": "Administrator",
    "description": "",
    "is_external": "0",
    "granted_permissions": [
      "dyna",
      "jobcards",
      ...
      ...
    ],
    "denied_permissions": [],
    "inherited_roles": []
  }
]

>Solution :

You can use the Jquery inArray method.

describe('Test plan creator for Administrator', () => {

  it('create a test plan', () => {
    cy.get(permission_list[0].granted_permissions).then((list) => {
      if (Cypress.$.inArray('dyna', list) != -1) {
        cy.log('Yes') //dyna found
      } else {
        cy.log('No')
      }
    })
  })
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading