Home > Blockchain >  Cypress - Condition depending on json list
Cypress - Condition depending on json list

Time:01-15

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:

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": []
  }
]

CodePudding user response:

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')
      }
    })
  })
})
  •  Tags:  
  • Related