6.0 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| fixture | true |
Load a fixed set of data located in a file.
{% note info New to Cypress? %} Read about Creating Fixtures first. {% endnote %}
Syntax
.fixture(filePath)
.fixture(filePath, encoding)
.fixture(filePath, options)
.fixture(filePath, encoding, options)
Usage
.fixture() cannot be chained off any other cy commands, so should be chained off of cy for clarity.
{% fa fa-check-circle green %} Valid Usage
cy.fixture('users').as('usersJson') // load data from users.json
cy.fixture('logo.png').then(function(logo){
// load data from logo.png
})
Arguments
{% fa fa-angle-right %} filePath (String)
A path to a file within the fixturesFolder (which defaults to cypress/fixtures).
You can nest fixtures within folders and reference them by defining the path from the fixturesFolder:
cy.fixture('users/admin.json') // Get data from {fixturesFolder}/users/admin.json
{% fa fa-angle-right %} encoding (String)
The encoding to be used when reading the file. The following encodings are supported:
asciibase64binaryhexlatin1utf8utf-8ucs2ucs-2utf16leutf-16le
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of .fixture().
| Option | Default | Notes |
|---|---|---|
timeout |
responseTimeout |
Total time to wait for the command to be processed |
Yields
.fixture() yields the contents of the file. Formatting is determined by it's file extension.
Timeout
.fixture will wait up for the duration of responseTimeout for the server to process the command.
Examples
JSON
Load a users.json fixture
cy.fixture('users.json').as('usersData')
Omit the fixture file's extension
When no extension is passed to .fixture(), Cypress will search for files with the specified name within the fixturesFolder (which defaults to cypress/fixtures) and resolve the first one.
cy.fixture('admin').as('adminJSON')
The example above 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
Images
Image fixtures are sent 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...
})
Accessing Fixture Data
Using cy.then to access fixture data
cy
.fixture('users').then(function(json){
cy.route('GET', '/users/**', json)
})
{% note info %} Check out our example recipe using cy.fixture to bootstrap data for our application. {% endnote %}
Using an alias to access a fixture
You can make use of aliasing, .as(), instead of working directly with the yielded data.
Using an alias provides the benefit of terseness and readability. It also makes it easier to access the data later in your tests.
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 modify fixture data directly before passing it along to a route.
cy.fixture('user').then(function(user) {
user.firstName = 'Jane'
cy.route('GET', '/users/1', user).as('getUser')
})
cy.visit('/users')
cy.wait('@getUser').then(function(xhr) {
expect(xhr.requestBody.firstName).to.eq('Jane')
})
Fixture Shortcuts
Using fixture or fx shortcuts
Fixtures can also be referenced directly without using the .fixture() command by using the special keywords: fixture: or fx: within .route().
cy.route('GET', '/users/**', 'fixture:users') // this works
cy.route('GET', '/users/**', 'fx:users') // this also works
Notes
Validation and Formatting
Cypress automatically validates and formats your fixtures. If your .json, .js, or .coffee files contain syntax errors, they will be shown in the Command Log.
Default 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, unless specified in the second argument of .fixture()
Command Log
fixture does not log in the command log