簡體   English   中英

賽普拉斯從 json 加載數據 - 夾具之前

[英]Cypress load data from json - fixture before

我正在嘗試通過賽普拉斯中的夾具從 json 文件中檢索一些數據,但這些數據根本無法識別。

before(() => {
cy.fixture('example').then(function (data) {
    console.log("this", data.user);
})

})

控制台輸出用戶,這是有效的。

但在那之后我有一個步驟:

Given("I check data", () => {
    console.log("this", this.data.user);
});

這里的數據是未定義的。

before也嘗試過設置:

this.data = data但沒有幫助。 我也嘗試使用beforeEach沒有成功。

不是 cucumber 用戶,但在普通賽普拉斯測試中,您只能通過將回調設為 function 而不是箭頭 function 來訪問this

Given("I check data", function() {
  console.log("this", this.data.user);
});

我認為您可能還必須為數據起別名

before(() => {
  cy.fixture('example')
    .then(function (data) {
      console.log("this", data.user)
    })
    .as('data');
}

請注意,賽普拉斯會在測試之間清除別名,因此您需要使用beforeEach()而不是before()

賽普拉斯文檔中所述

如果您使用此測試上下文 object 存儲和訪問夾具數據,請確保使用 function () {... } 回調。 否則,測試引擎將不會將 this 指向測試上下文。

將箭頭 function 更改為 function 應該可以工作:

Given("I check data", function() {
    console.log("this", this.example.user);
});

還有你的beforeEach()塊:

   beforeEach(function () {
        cy.fixture('example').then(function (example) {
            this. example = example
        })
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM