访问 `it` 中的夹具数据 "a test case"

Accessing fixture data inside `it` "a test case"

如何访问 it 块中的以下固定装置:

users.json

{
"users": [
    {
        "first_name": "Fadi",
        "last_name": "Salam", 
        "work_email": "fadi.salam@bayzat.com"
    },
    {
        "first_name": "Maha",
        "last_name": "Black", 
        "work_email": "maha.black@bazyat.com"
    }
  ] 
}

我的柏树相关函数代码:

descibe('test', () => {
    beforeEach(function(){
    cy.restoreToken()
    cy.fixture('users.json').as('users')
    })

    it('Add an Employee', function() {
           cy.get('@users').then((users) => {
            const user_1 = users[0]
            cy.add_employee(user_1.first_name, user_1.last_name, user_1.work_email)
        }
    )}
})

我无法访问 first_name、...等等

我该怎么做?

我更改了您代码中的一些拼写错误并使其正常运行。

在您的 users.json 文件中,'first_name' 嵌套在 'users' 下。

你可以使用 users.users[0].first_name 访问 first_name。下面的示例代码,

cy.get('@users').then((users) => {
            console.log(users.users[0].first_name);
        })

在您的情况下,控制台会打印 'Fadi'。

如果您想在所有上下文中访问此固定程序并且它会阻塞,您可以执行以下操作:

const USERS;

before (() => {
  cy.fixture('users.json').then(($users) => USERS = $users);
}

然后你访问常量。

cy.getUserName().should('have.text', USERS[0].first_name);