{"__v":0,"_id":"569029b418c3920d00be8b43","category":{"__v":60,"_id":"569002f19ebef90d0087289d","pages":["56900352769f210d00132595","5690047121fcf0190071d943","569004924719c119002ce654","569004ceb700ce0d002f4b94","569023e518c3920d00be8b37","569023f748df220d004ca215","5690240af7eb9a0d00f4465d","5690241b48df220d004ca217","5690243e48df220d004ca219","56902453741e9c0d00af2fb5","56902463efcc380d0043a5a1","5690247b18c3920d00be8b39","56902492f7eb9a0d00f4465f","569024a16c49d70d00f18075","569024b2efcc380d0043a5a3","569024cc48df220d004ca21b","569024ea18c3920d00be8b3b","5690258618c3920d00be8b3f","5690259bf7eb9a0d00f44662","569025b06c49d70d00f18077","569029b418c3920d00be8b43","569029d4f7eb9a0d00f44667","569029e8e056c80d00fdec58","569029fb48df220d004ca221","56902a1c18c3920d00be8b46","56902a45f7eb9a0d00f44669","56902a6be056c80d00fdec5a","56902a78f7eb9a0d00f4466b","56902a8848df220d004ca224","56902aa018c3920d00be8b4b","56902ace6c49d70d00f18085","56902ae0741e9c0d00af2fc6","56902aee48df220d004ca226","56902af8f7eb9a0d00f44674","56902b26efcc380d0043a5b1","56902b8148df220d004ca22a","56902ba918c3920d00be8b55","56902bb96c49d70d00f1808b","56902bc818c3920d00be8b57","56902bd518c3920d00be8b59","56902be218c3920d00be8b5b","56902bf66c49d70d00f1808e","56902c05e056c80d00fdec5d","56902c196c49d70d00f18090","56902c2648df220d004ca22d","56902c34f7eb9a0d00f44678","56902c5518c3920d00be8b5d","56902c62741e9c0d00af2fcc","56902cde48df220d004ca230","56902cea741e9c0d00af2fcf","56902d0ae056c80d00fdec60","56902d20efcc380d0043a5b4","56902d3448df220d004ca232","5696c3fbf9203821005fe2fb","5696c3fb9e2d000d00947ab0","5696c3fbf9203821005fe2fa","5697efee8d2a770d00d2fd17","569802611c4dc823005426c7","56a65c82b3ffe00d00156eaf","56f01f88332da41700f24b74"],"project":"568fde81b700ce0d002f4b43","version":"568fde82b700ce0d002f4b46","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-08T18:41:53.562Z","from_sync":false,"order":1,"slug":"commands","title":"Commands"},"parentDoc":null,"project":"568fde81b700ce0d002f4b43","user":"568fffce769f210d0013258f","version":{"__v":23,"_id":"568fde82b700ce0d002f4b46","project":"568fde81b700ce0d002f4b43","createdAt":"2016-01-08T16:06:26.373Z","releaseDate":"2016-01-08T16:06:26.373Z","categories":["568fde82b700ce0d002f4b47","568ff0e504440a1700e4cbbd","569002f19ebef90d0087289d","569004f4769f210d00132599","5690056d9ebef90d008728a0","569005d394c5030d0028813a","5690067804440a1700e4cbe2","569137eb3c4f510d00ec9b92","56913815e56a790d008dbfe3","569138ba3c4f510d00ec9b93","5691392f3c4f510d00ec9b94","56913bbe72f2810d007e4cb0","56933b8d6ebadc0d005b71d2","56933b8d6ebadc0d005b71d3","569564facaa32519009c41e6","5696a319b6d61f0d00acfb40","5696a319a857080d0082e8e8","5697efe43503e40d0061f4d1","5697efe48d2a770d00d2fd16","569e9597ffccd10d00a05c59","56a7a1523d33bc2100793d5c","56a7a32ecf6d771700baeee8","56b8b0f7ddeb231700e69825"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":false,"codename":"bar","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-08T21:27:16.131Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":23,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"[Read about Creating Fixtures first.](https://on.cypress.io/guides/creating-fixtures)\",\n  \"title\": \"New to Cypress?\"\n}\n[/block]\n\nLoads a single fixture file. Image fixtures will be sent as `base64`.\n\nIf an extension is omitted, Cypress will attempt to resolve the fixture by order of these extensions:\n\n* `.json`\n* `.js`\n* `.coffee`\n* `.html`\n* `.txt`\n* `.csv`\n* `.png`\n* `.jpg`\n* `.jpeg`\n* `.gif`\n* `.tif`\n* `.tiff`\n* `.zip`\n\n| | |\n|--- | --- |\n| **Returns** | the contents of the file, formatted by file extension |\n| **Timeout** | `cy.fixture` will wait up for the duration of [`responseTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) for the server to process this command. |\n\n***\n\n# [cy.fixture( *fixture* )](#section-single-fixture-usage)\n\nLoads the fixture at the specified filepath within the [`fixturesFolder`](https://on.cypress.io/guides/configuration#section-folders), which defaults to `cypress/fixtures`.\n\n***\n\n# [cy.fixture( *fixture*, *encoding* )](#section-encoding)\n\nLoads the fixture at the specified filepath within the [`fixturesFolder`](https://on.cypress.io/guides/configuration#section-folders), which defaults to `cypress/fixtures`, using the encoding specified when reading the file.\n\n***\n\n# Options\n\nPass in an options object to change the default behavior of `cy.fixture`.\n\n**[cy.fixture( *fixture*, *options* )](#options-usage)**\n\n**[cy.fixture( *fixture*, *encoding*, *options* )](#options-usage)**\n\nOption | Default | Notes\n--- | --- | ---\n`timeout` | [`responseTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) | Total time to wait for the `cy.fixture` command to be processed\n\n***\n\n# Single Fixture Usage\n\n## Load the `users.json` fixture\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.fixture(\\\"users.json\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Omit the fixture file's extension\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.fixture(\\\"admin\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nWhen no extension is passed to `cy.fixture`, Cypress will search for files with the specified name within the [`fixturesFolder`](https://on.cypress.io/guides/configuration#section-folders), which defaults to `cypress/fixtures`, and resolve the first one. The above example would resolve in the following order:\n\n1. `{fixturesFolder}/admin.json`\n2. `{fixturesFolder}/admin.js`\n3. `{fixturesFolder}/admin.coffee`\n4. `{fixturesFolder}/admin.html`\n5. `{fixturesFolder}/admin.txt`\n6. `{fixturesFolder}/admin.csv`\n7. `{fixturesFolder}/admin.png`\n8. `{fixturesFolder}/admin.jpg`\n9. `{fixturesFolder}/admin.jpeg`\n10. `{fixturesFolder}/admin.gif`\n11. `{fixturesFolder}/admin.tif`\n12. `{fixturesFolder}/admin.tiff`\n13. `{fixturesFolder}/admin.zip`\n\n***\n\n## Image fixtures will be sent by default as `base64`\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.fixture(\\\"images/logo.png\\\").then(function(logo){\\n  // logo will be encoded as base64\\n  // and should look something like this:\\n  // aIJKnwxydrB10NVWqhlmmC+ZiWs7otHotSAAAOw==...\\n})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Change encoding of Image fixture\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.fixture(\\\"images/logo.png\\\", \\\"binary\\\").then(function(logo){\\n  // logo will be encoded as binary\\n  // and should look something like this:\\n  // 000000000000000000000000000000000000000000...\\n})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Notes\n\n## Nesting\n\nYou can nest fixtures within folders and reference them by defining the path to the file:\n\n`{fixturesFolder}/users/admin.json`\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.fixture(\\\"users/admin.json\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Validation\n\nCypress will automatically validate your fixtures. If your `.json`, `.js`, or `.coffee` files contain syntax errors, they will automatically be shown in the Command Log.\n\n***\n\n## Formatting\n\nCypress automatically formats your fixture files. That means you can paste in a single line of `json` and the next time Cypress serves this fixture, it will format / indent the `json`, which makes it easier to read and debug.\n\n***\n\n## Encoding\n\nCypress automatically determines the encoding for the following file types:\n\n* `.json`\n* `.js`\n* `.coffee`\n* `.html`\n* `.txt`\n* `.csv`\n* `.png`\n* `.jpg`\n* `.jpeg`\n* `.gif`\n* `.tif`\n* `.tiff`\n* `.zip`\n\nFor other types of files, they will be read as `utf8` by default. You can specify a different encoding by passing it as the [second argument](https://on.cypress.io/api/fixture#section--cy-fixture-fixture-encoding-section-encoding-).\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.fixture(\\\"foo.bmp\\\", \\\"base64\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nThe following encodings are supported:\n\n* `ascii`\n* `base64`\n* `binary`\n* `hex`\n* `latin1`\n* `utf8`\n* `utf-8`\n* `ucs2`\n* `ucs-2`\n* `utf16le`\n* `utf-16le`\n\n***\n\n# Usage with `cy.route()`\n\nFixtures can be referenced directly by the special keywords: `fixture:` or `fx:`.\n\nThis enables you to set a fixture as the response for a route without having to first use the `cy.fixture` command.\n\n## Example 1:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.route(\\\"GET\\\", /users/, \\\"fixture:users\\\") // this works\\ncy.route(\\\"GET\\\", /users/, \\\"fx:users\\\")      // this also works\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nThis saves you from having to explicitly load the fixture first (like in [Example #2](https://on.cypress.io/api/fixture#section-example-2-)).\n\n## Example 2:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .fixture(\\\"users\\\").then(function(json){\\n    cy.route(\\\"GET\\\", /users/, json)\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nHowever if you still need access to the fixture data, instead of yielding the fixture's data in [Example #2](https://on.cypress.io/api/fixture#section-example-2-), we can make use of [aliasing](https://on.cypress.io/guides/using-aliases).\n\n## Example 3:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .fixture(\\\"users\\\").as(\\\"usersJSON\\\")\\n  .route(\\\"GET\\\", /users/, \\\":::at:::usersJSON\\\")\\n\\n  // ...later on...\\n\\n  .then(function(){\\n    // we have access to this.usersJSON since it was aliased\\n    this.usersJSON\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nUsing an alias provides the benefit of terseness and readability.\n\n## Example 4:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .fixture(\\\"user\\\").then(function(user){\\n    user.firstName = \\\"Jennifer\\\"\\n\\n    cy.route(\\\"GET\\\", \\\"/users/1\\\", user)\\n  })\\n  .visit(\\\"/users\\\")\\n  .get(\\\".user\\\").should(\\\"include\\\", \\\"Jennifer\\\")\\n})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nYou can also modify fixture data directly before passing it along to the route.\n\n***\n\n# Command Log\n\n## `fixture` does *not* log in the command log\n\n***\n\n# Related\n\n- [route](https://on.cypress.io/api/route)\n- [Creating Fixtures](https://on.cypress.io/guides/creating-fixtures)","excerpt":"Load a fixture to represent data","slug":"fixture","type":"basic","title":"fixture"}

fixture

Load a fixture to represent data

[block:callout] { "type": "info", "body": "[Read about Creating Fixtures first.](https://on.cypress.io/guides/creating-fixtures)", "title": "New to Cypress?" } [/block] Loads a single fixture file. Image fixtures will be sent as `base64`. If an extension is omitted, Cypress will attempt to resolve the fixture by order of these extensions: * `.json` * `.js` * `.coffee` * `.html` * `.txt` * `.csv` * `.png` * `.jpg` * `.jpeg` * `.gif` * `.tif` * `.tiff` * `.zip` | | | |--- | --- | | **Returns** | the contents of the file, formatted by file extension | | **Timeout** | `cy.fixture` will wait up for the duration of [`responseTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) for the server to process this command. | *** # [cy.fixture( *fixture* )](#section-single-fixture-usage) Loads the fixture at the specified filepath within the [`fixturesFolder`](https://on.cypress.io/guides/configuration#section-folders), which defaults to `cypress/fixtures`. *** # [cy.fixture( *fixture*, *encoding* )](#section-encoding) Loads the fixture at the specified filepath within the [`fixturesFolder`](https://on.cypress.io/guides/configuration#section-folders), which defaults to `cypress/fixtures`, using the encoding specified when reading the file. *** # Options Pass in an options object to change the default behavior of `cy.fixture`. **[cy.fixture( *fixture*, *options* )](#options-usage)** **[cy.fixture( *fixture*, *encoding*, *options* )](#options-usage)** Option | Default | Notes --- | --- | --- `timeout` | [`responseTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) | Total time to wait for the `cy.fixture` command to be processed *** # Single Fixture Usage ## Load the `users.json` fixture [block:code] { "codes": [ { "code": "cy.fixture(\"users.json\")\n", "language": "javascript" } ] } [/block] *** ## Omit the fixture file's extension [block:code] { "codes": [ { "code": "cy.fixture(\"admin\")\n", "language": "javascript" } ] } [/block] When no extension is passed to `cy.fixture`, Cypress will search for files with the specified name within the [`fixturesFolder`](https://on.cypress.io/guides/configuration#section-folders), which defaults to `cypress/fixtures`, and resolve the first one. The above example would resolve in the following order: 1. `{fixturesFolder}/admin.json` 2. `{fixturesFolder}/admin.js` 3. `{fixturesFolder}/admin.coffee` 4. `{fixturesFolder}/admin.html` 5. `{fixturesFolder}/admin.txt` 6. `{fixturesFolder}/admin.csv` 7. `{fixturesFolder}/admin.png` 8. `{fixturesFolder}/admin.jpg` 9. `{fixturesFolder}/admin.jpeg` 10. `{fixturesFolder}/admin.gif` 11. `{fixturesFolder}/admin.tif` 12. `{fixturesFolder}/admin.tiff` 13. `{fixturesFolder}/admin.zip` *** ## Image fixtures will be sent by default as `base64` [block:code] { "codes": [ { "code": "cy.fixture(\"images/logo.png\").then(function(logo){\n // logo will be encoded as base64\n // and should look something like this:\n // aIJKnwxydrB10NVWqhlmmC+ZiWs7otHotSAAAOw==...\n})\n", "language": "javascript" } ] } [/block] *** ## Change encoding of Image fixture [block:code] { "codes": [ { "code": "cy.fixture(\"images/logo.png\", \"binary\").then(function(logo){\n // logo will be encoded as binary\n // and should look something like this:\n // 000000000000000000000000000000000000000000...\n})\n", "language": "javascript" } ] } [/block] *** # Notes ## Nesting You can nest fixtures within folders and reference them by defining the path to the file: `{fixturesFolder}/users/admin.json` [block:code] { "codes": [ { "code": "cy.fixture(\"users/admin.json\")\n", "language": "javascript" } ] } [/block] *** ## Validation Cypress will automatically validate your fixtures. If your `.json`, `.js`, or `.coffee` files contain syntax errors, they will automatically be shown in the Command Log. *** ## Formatting Cypress automatically formats your fixture files. That means you can paste in a single line of `json` and the next time Cypress serves this fixture, it will format / indent the `json`, which makes it easier to read and debug. *** ## Encoding Cypress automatically determines the encoding for the following file types: * `.json` * `.js` * `.coffee` * `.html` * `.txt` * `.csv` * `.png` * `.jpg` * `.jpeg` * `.gif` * `.tif` * `.tiff` * `.zip` For other types of files, they will be read as `utf8` by default. You can specify a different encoding by passing it as the [second argument](https://on.cypress.io/api/fixture#section--cy-fixture-fixture-encoding-section-encoding-). [block:code] { "codes": [ { "code": "cy.fixture(\"foo.bmp\", \"base64\")\n", "language": "javascript" } ] } [/block] The following encodings are supported: * `ascii` * `base64` * `binary` * `hex` * `latin1` * `utf8` * `utf-8` * `ucs2` * `ucs-2` * `utf16le` * `utf-16le` *** # Usage with `cy.route()` Fixtures can be referenced directly by the special keywords: `fixture:` or `fx:`. This enables you to set a fixture as the response for a route without having to first use the `cy.fixture` command. ## Example 1: [block:code] { "codes": [ { "code": "cy.route(\"GET\", /users/, \"fixture:users\") // this works\ncy.route(\"GET\", /users/, \"fx:users\") // this also works\n", "language": "javascript" } ] } [/block] This saves you from having to explicitly load the fixture first (like in [Example #2](https://on.cypress.io/api/fixture#section-example-2-)). ## Example 2: [block:code] { "codes": [ { "code": "cy\n .fixture(\"users\").then(function(json){\n cy.route(\"GET\", /users/, json)\n })\n", "language": "javascript" } ] } [/block] However if you still need access to the fixture data, instead of yielding the fixture's data in [Example #2](https://on.cypress.io/api/fixture#section-example-2-), we can make use of [aliasing](https://on.cypress.io/guides/using-aliases). ## Example 3: [block:code] { "codes": [ { "code": "cy\n .fixture(\"users\").as(\"usersJSON\")\n .route(\"GET\", /users/, \"@usersJSON\")\n\n // ...later on...\n\n .then(function(){\n // we have access to this.usersJSON since it was aliased\n this.usersJSON\n })\n", "language": "javascript" } ] } [/block] Using an alias provides the benefit of terseness and readability. ## Example 4: [block:code] { "codes": [ { "code": "cy\n .fixture(\"user\").then(function(user){\n user.firstName = \"Jennifer\"\n\n cy.route(\"GET\", \"/users/1\", user)\n })\n .visit(\"/users\")\n .get(\".user\").should(\"include\", \"Jennifer\")\n})\n", "language": "javascript" } ] } [/block] You can also modify fixture data directly before passing it along to the route. *** # Command Log ## `fixture` does *not* log in the command log *** # Related - [route](https://on.cypress.io/api/route) - [Creating Fixtures](https://on.cypress.io/guides/creating-fixtures)