6.4 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| fixture | true |
{% note info New to Cypress? %} Read about Creating Fixtures first. {% endnote %}
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 for the server to process this command. |
cy.fixture( fixture )
Loads the fixture at the specified filepath within the fixturesFolder, which defaults to cypress/fixtures.
cy.fixture( fixture, encoding )
Loads the fixture at the specified filepath within the fixturesFolder, 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 )
cy.fixture( fixture, encoding, options )
| Option | Default | Notes |
|---|---|---|
timeout |
responseTimeout |
Total time to wait for the cy.fixture command to be processed |
Single Fixture Usage
Load the users.json fixture
cy.fixture("users.json")
Omit the fixture file's extension
cy.fixture("admin")
When no extension is passed to cy.fixture, Cypress will search for files with the specified name within the fixturesFolder, which defaults to cypress/fixtures, and resolve the first one. The above example would resolve in the following order:
{fixturesFolder}/admin.json{fixturesFolder}/admin.js{fixturesFolder}/admin.coffee{fixturesFolder}/admin.html{fixturesFolder}/admin.txt{fixturesFolder}/admin.csv{fixturesFolder}/admin.png{fixturesFolder}/admin.jpg{fixturesFolder}/admin.jpeg{fixturesFolder}/admin.gif{fixturesFolder}/admin.tif{fixturesFolder}/admin.tiff{fixturesFolder}/admin.zip
Image fixtures will be sent by default as base64
cy.fixture("images/logo.png").then(function(logo){
// logo will be encoded as base64
// and should look something like this:
// aIJKnwxydrB10NVWqhlmmC+ZiWs7otHotSAAAOw==...
})
Change encoding of Image fixture
cy.fixture("images/logo.png", "binary").then(function(logo){
// logo will be encoded as binary
// and should look something like this:
// 000000000000000000000000000000000000000000...
})
Notes
Nesting
You can nest fixtures within folders and reference them by defining the path to the file:
{fixturesFolder}/users/admin.json
cy.fixture("users/admin.json")
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.
cy.fixture("foo.bmp", "base64")
The following encodings are supported:
asciibase64binaryhexlatin1utf8utf-8ucs2ucs-2utf16leutf-16le
Usage with cy.route()
Using fixture or fx shortcuts
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.
cy.route("GET", /users/, "fixture:users") // this works
cy.route("GET", /users/, "fx:users") // this also works
This saves you from having to explicitly load the fixture first (like below).
Using cy.then to access fixture data
cy
.fixture("users").then(function(json){
cy.route("GET", /users/, json)
})
{% note info Using cy.fixture for response data %} Check out our example recipe using cy.fixture to bootstrap data for our application. {% endnote %}
Using an alias to access a fixture
However if you still need access to the fixture data, instead of yielding the fixture's data, we can make use of aliasing.
Using an alias provides the benefit of terseness and readability.
cy
.fixture("users").as("usersJSON")
.route("GET", /users/, "@usersJSON")
// ...later on...
.then(function(){
// we have access to this.usersJSON since it was aliased
this.usersJSON
})
Modifying fixture data before using it
You can also modify fixture data directly before passing it along to the route.
cy
.fixture("user").then(function(user){
user.firstName = "Jane"
cy.route("GET", "/users/1", user)
})
.visit("/users")
.get(".user").should("include", "Jane")
})