[英]What is the difference between fixtures and support files in Cypress? Which one is better to use?
Both fixtures and support files store static data, right? fixtures和 support 文件都存储了 static 数据,对吧? What is the best recommended to use?
最好推荐使用什么?
A use case for fixtures
could be to put static data that is used during a test, such as a JSON body. fixtures
的一个用例可能是放置测试期间使用的 static 数据,例如 JSON 主体。
Say you need to store some request body JSON that you're going to be using a lot.假设您需要存储一些您将经常使用的请求主体 JSON。 It won't change much and doesn't need to be changed by tests.
它不会有太大变化,也不需要通过测试来改变。
requestBody.json
could be stored in fixtures
and then in the test spec the .json
could be referenced and then used in a test. requestBody.json
可以存储在fixtures
,然后在测试规范中.json
然后在测试中使用。 Files in cypress/fixtures/
are not meant to change or be dynamic in the tests: cypress/fixtures/
中的文件并不意味着在测试中改变或动态:
const requestBody = require("../fixtures/requestBody.json");
describe('Test', () => {
it('should do something', () => {
cy.request({
method: 'GET',
url: myUrl,
body: requestBody
})....
A use case for support
could be to define a Custom Command in cypress/support/commands.js
. support
的一个用例可能是在cypress/support/commands.js
中定义自定义命令。
Say you need to send a header with some data that you don't want to expose, such as an Authorization key.假设您需要发送一个 header,其中包含一些您不想公开的数据,例如授权密钥。 You also need to hardcode the
GET
method, but you want to leave the url
and body
to be dynamic.您还需要对
GET
方法进行硬编码,但您希望url
和body
保持动态。 A Custom Command could be defined in cypress/support/commands.js
:可以在
cypress/support/commands.js
中定义自定义命令:
Cypress.Commands.add('getRequestWithSecret', (url, body) => {
cy.request({
method: 'GET',
headers: {
Authorization: mySecretKey
},
url: url,
body: body
});
});
and then this Custom Command can be used in the code.然后可以在代码中使用此自定义命令。 You can see in the example below there is a mixture of static and dynamic content.
您可以在下面的示例中看到混合了 static 和动态内容。 The fixture being imported, as well as the Custom Command.
正在导入的夹具,以及自定义命令。
getRequestWithSecret
is dynamic because it is accepting inputs. getRequestWithSecret
是动态的,因为它正在接受输入。 cypress/support/index.js
and commands.js
get loaded before the test spec starts. cypress/support/index.js
和commands.js
在测试规范开始之前加载。 This is pretty useful because it means that you can add scripts that can run before tests start.这非常有用,因为这意味着您可以添加可以在测试开始之前运行的脚本。 So these don't need to be imported into your tests at runtime.
所以这些不需要在运行时导入到您的测试中。
const requestBody = require("../fixtures/requestBody");
describe('Test', () => {
it('should do something', () => {
cy.getRequestWithSecret({
url: myUrl,
body: requestBody
})....
Both have different purposes.两者都有不同的目的。 Fixtures are used as external pieces of static data that can be used by your tests whereas the support file is a great place to put reusable behavior such as custom commands or global overrides that you want to be applied and available to all of your spec files.
夹具用作 static 数据的外部片段,可由您的测试使用,而支持文件是放置可重用行为的好地方,例如您希望应用并可用于所有规范文件的自定义命令或全局覆盖。 For more better understanding, I recommend you to read the cypress official docs https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Support-file
为了更好地理解,我建议您阅读 cypress 官方文档https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Support-file
Fixture is static data that is needed for tests - usually we put images, JSON files, and some downloadable fixtures there. Fixture是测试所需的 static 数据——通常我们将图像、JSON 文件和一些可下载的 fixture 放在那里。
In support files we add/customize commands, and import Plugins and other dependencies needed for the test.在支持文件中,我们添加/自定义命令,并导入插件和测试所需的其他依赖项。 It can also be used to make some initial setup, like before/beforeAll functions, but this part is better made in configuration files or directly in the tests.
它还可以用于进行一些初始设置,如 before/beforeAll 函数,但这部分最好在配置文件中或直接在测试中进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.