json 数组中的赛普拉斯推送值

Cypress push value in json array

我想编译一个列表并将它们推送到 Cypress 中的 json 数组中。

json 输出应该是这样的:

[
  {
    "role": "Administrator",
    "scenarios": [
      "clients_a",
      "clients_d",
      "clients_f",
      ....
    ]
  }
]

到目前为止,这是我的代码:

const permission_list = require('../../fixtures/permissions/permissions_admin.json')
const test_plan = 'cypress/fixtures/permissions/test_plan_admin.json'
const role = 'Administrator'

describe('Test plan creator for Administrator', () => {
    it('create a test plan', () => {
        //prepare the json array
        cy.writeFile(test_plan, [{role: role,scenarios: []}])

        cy.get(permission_list[0].granted_permissions).then((list) => {
            if (Cypress.$.inArray('clients', list) != -1) {
                cy.readFile(test_plan).then((scenarios) => {
                    scenarios.push('clients_a') // this is where I make a mistake I suppose
                    // write the merged list
                    cy.writeFile(test_plan, scenarios)
                })
            }
            ///added to handle case "clients" was not found in Array
            else {
            }
        })
    })
})

这是我的代码的输出:

[
  {
    "role": "Administrator",
    "scenarios": []
  },
  "clients_a" <-- should be inside the list behind "scenarios"
]

所以它很接近但没有雪茄。如何将值 "clients_a" 推入 "scenarios" 的列表?

你可以这样做。您必须在 JSON 文件中创建一个结构。使用 flag: a+ 您可以在文件末尾附加文本而不是覆盖它。

describe('Test plan creator for Administrator', () => {
  it('create a test plan', () => {

    //prepare the json array
    cy.writeFile(test_plan, '[{role: role,scenarios: [')

    cy.get(permission_list[0].granted_permissions).then((list) => {
      if (Cypress.$.inArray('clients', list) != -1) {
        cy.writeFile(test_plan, 'clients_a', {flag: 'a+'})
      }
    })

    cy.writeFile(test_plan, ']}]', {flag: 'a+'}) //complete the structure with closing brackets
  })
})

我会采取的方法是在内存中做所有事情,最后只写一次完成的列表。

这样做会稍微加快速度,但主要优点是您无需担心追加。

const permission_list = require('../../fixtures/permissions/permissions_admin.json')
const test-plan-file-path = 'cypress/fixtures/permissions/test_plan_admin.json'
const role = 'Administrator'

describe('Test plan creator for Administrator', () => {
  it('create a test plan', () => {

    //prepare the json array
    const scenarios = []

    cy.get(permission_list[0].granted_permissions).then((list) => {
      if (Cypress.$.inArray('clients', list) != -1) {
        scenarios.push('clients_a') 
      }
    })

    // write the result
    const plan = [{ role, scenarios}]
    cy.writeFile(test-plan-file-path, plan)     
  })
})

这里还有一些奇怪的地方,看起来你在循环中推送多个项目,但这里没有循环。需要查看更多应用程序以建议如何解决该问题。

如果你想推送多个客户端id,我就是这样做的

const permission_list = require('../../fixtures/permissions/permissions_admin.json')
const test_plan = 'cypress/fixtures/permissions/test_plan_admin.json'
const role = 'Administrator'

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

  before(() => {
    cy.writeFile(test_plan, [{role: role, scenarios: []}])
  })

  it('create a test plan', () => {

    cy.fixture('test_plan_admin.json').then(plan => {

      const list = permission_list[0].granted_permissions
      if (list.includes('clients')) {
        plan.scenarios.push('clients_a') 
      }

      cy.writeFile(test_plan, plan)     
    })
  })

  it('another client', () => {

    cy.fixture('test_plan_admin.json').then(plan => {

      const list = permission_list[0].granted_permissions
      if (list.includes('clients')) {
        plan.scenarios.push('clients_a') 
      }

      cy.writeFile(test_plan, plan)     
    })
  })

})