![](/img/trans.png)
[英]What is the difference between cy.readFile and cy.fixture in Cypress.io?
[英]What is the difference between import and cy.fixture in Cypress tests?
我正在學習柏樹,我不明白import file from '../fixtures/filepath/file.json'
文件和調用cy.fixture(file)
有什么不同,以及我應該何時使用每個文件。
基本上,當您說import file from '../fixtures/filepath/file.json'
時,您可以在特定 javascript 文件中的任何方法中使用導入的文件。 而如果您說cy.fixture(file.json)
,那么夾具上下文將保留在該 cy.fixture 塊中,並且您無法訪問該 cy.fixture 塊之外的任何位置/外部。 請通過下面的代碼 go 你就會明白它的意義。
我建議使用import file from '../fixtures/filepath/file.json'
例如。 運行以下代碼即可理解。
import fixtureFile from './../fixtures/userData.json';
describe('$ suite', () => {
it('Filedata prints only in cy.fixture block', () => {
cy.fixture('userData.json').then(fileData => {
cy.log(JSON.stringify(fileData)); // You can access fileData only in this block.
})
cy.log(JSON.stringify(fileData)); //This says error because you are accessing out of cypress fixture context
})
it('This will print file data with import', () => {
cy.log(JSON.stringify(fixtureFile));
})
it('This will also print file data with import', () => {
cy.log(JSON.stringify(fixtureFile));
})
});
查看cy.fixture
的文檔 - http://on.cypress.io/api/fixture
簡而言之,使用cy.fixture
與賽普拉斯的異步特性一起工作,與許多編碼類型一起工作,並且可以在規范中的整個測試中使用別名( https://docs.cypress.io/guides/core-concepts/variables-和-aliases.html#Aliases )
此外,fixtures 也可以用作對cy.route
- https://docs.cypress.io/api/commands/route.html#Fixtures的響應
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.