如何从 cypress 项目中读取 JSON 文件?

How to read JSON file from cypress project?

我已经开始使用 cypress 自动化,但我正在努力处理 JSON 个文件。

任何人都知道我如何读取 JSON 文件,比方说,位于 ../example/vehicle.json?

我知道 cypress 是 JavaScript,但我在 cypress 项目中导入 JQuery 时也遇到了问题。

我从未与 Cypress 合作过,在查看文档时我认为这可以帮助您

cy.fixture(filePath)
cy.fixture(filePath, encoding)
cy.fixture(filePath, options)
cy.fixture(filePath, encoding, options)

请查看https://docs.cypress.io/api/commands/fixture.html#Syntax

万一这可以帮助任何人,有一个很好的方法可以使用以下行来做到这一点:

cy.readFile('path/to/file.json/txt/yaml')

https://docs.cypress.io/api/commands/readfile.html

据我所知,这将是 "Cypress" 访问存储在 json 文件中的数据的方式。

参考:https://docs.cypress.io/api/commands/fixture.html#Accessing-Fixture-Data

cy.fixture('user').then((user)  => {
    var name = user.name
    var pass = user.password
})

其中 'user' 是 /fixtures/user。json

{
    "name": "username",
    "password": "password1234"
}

嗨赛普拉斯极客我成功使用它

cy.fixture(profile.json).then((user) => {
        // Delay each keypress by 0.1 sec
        cy.get('#username').type(user.email, { delay: 100 }).should('have.value', user.email)
        cy.get('#password').type(user.password)
      })

在 fixtures 文件夹中创建一个 UserData.json 文件并像这样定义您的值:

{
  "Name": "Ghulam Nabi",
  "Password":"Admin123"
}

现在,在您的测试文件中

cy.fixture(‘UserData.json’).then((user) => { 

        cy.get(‘#txtUsername’).type(user.Name)
        cy.get(‘#txtPassword’).type(user.Password)

      });

这只是一个例子。你可以通过这个方法做很多事情。

礼貌: https://softans.com/get-user-data-from-json-file-in-cypress/