Tutorials Hut

  • Cypress tutorials




  • How to use cypress fixtures to access data, upload and download file

    Cypress is an end-to-end testing framework that provides a lot of features to automate the testing of web applications. One of the features that make Cypress easy to use is the support for fixtures. Fixtures are used to provide data to your tests. You can use fixtures to provide static data that your tests can use or you can use fixtures to provide dynamic data that is generated at runtime.

     

    In this article, we will discuss the use of fixtures in Cypress and provide an example of how to use fixtures in your tests.

    • What are fixtures in Cypress?
    • Using fixtures in Cypress tests
    • Using fixtures with aliases
    • Access data from fixtures
    • Upload and download files in Cypress using fixtures.

    What are fixtures in Cypress?

    In Cypress, fixtures are files that contain data that you can use in your tests. Fixtures can be in various formats such as JSON, CSV, or YAML. Fixtures are loaded asynchronously and are available to all tests in the spec file.

    Fixtures are stored in the fixtures directory at the root of your project. Cypress automatically looks for fixtures in this directory, so you don’t need to configure anything to use fixtures.

    Using fixtures in Cypress tests

    Using fixtures in your tests is easy. All you need to do is to call the cy.fixture() command and pass the name of the fixture file. The cy.fixture() command returns a promise that resolves to the contents of the fixture file.

    Here’s an example of how to use a fixture file in a Cypress test:

    describe('Using fixtures in Cypress', () => {
    it('should use a fixture', () => {
    cy.fixture('example.json').then((data) => {
    cy.log(data);
    });
    });
    });
    
    

    In this example, we have a fixture file named example.json in the fixtures directory. We use the cy.fixture() command to load the fixture file. The then() callback receives the contents of the fixture file and logs the data to the Cypress console.

    You can use the contents of the fixture file in your tests as you would any other data.

    Using fixtures with aliases

    You can also use fixtures with Cypress aliases. Aliases allow you to reference data in your tests by name. Here’s an example of how to use a fixture with an alias:

    describe('Using fixtures with aliases in Cypress', () => {
    beforeEach(() => {
    cy.fixture('example.json').as('data');
    });
    
    it('should use an alias', () => {
    cy.get('@data').then((data) => {
    cy.log(data);
    });
    });
    });
    
    

    In this example, we use the cy.fixture().as() command to create an alias for the fixture file. We then use the get() command to retrieve the alias and use the contents of the fixture file in our test.

    Access Data from Fixtures

    In Cypress, you can use fixtures to store and access test data. Fixtures are static files that contain data in various formats, such as JSON, CSV, or text files.

    Here’s how you can access data from fixtures in your tests:

    Create a fixture file: To create a fixture file, create a new file in the fixtures/ folder of your Cypress project and give it a descriptive name, such as example.json. In the fixture file, add the data you want to use in your tests in the appropriate format. For example, if you want to store test data in JSON format, your fixture file might look like this:

    {
    “username”: “testuser”,
    “password”: “testpassword”
    }
    Load fixture data in your test: To load fixture data in your test, use the cy.fixture() command, passing in the name of the fixture file (without the file extension). For example, if you created a fixture file named example.json, you can load it in your test like this:

    describe('My test suite', () => {
    it('My test case', () => {
    cy.fixture('example').then((data) => {
    // use the fixture data in your test
    cy.visit('/login')
    cy.get('#username').type(data.username)
    cy.get('#password').type(data.password)
    cy.get('#login-btn').click()
    })
    })
    })
    
    

    In this example, the cy.fixture() command loads the example.json fixture file and returns the data as an object, which is passed as an argument to the then() function. You can then use the fixture data in your test by accessing the properties of the object.

    Use fixture data in your test: Once you’ve loaded the fixture data in your test, you can use it to interact with your application. In the example above, the fixture data is used to fill in a login form with a username and password, and click on the login button.

    By using fixtures in your tests, you can store test data separately from your test code, making it easier to update and maintain your test data as your application changes.

    Upload and download files in cypress

    To upload and download files in Cypress, you can use the cy.fixture() and cy.readFile() commands. Here’s an example of how to use them:

    Uploading a file:

    cy.fixture('example.csv').then(fileContent => {
    cy.get('#file-input').upload({ fileContent, fileName: 'example.csv', mimeType: 'application/vnd.ms-excel' })
    })
    
    

    In this example, we first load the file example.csv from the fixtures folder using the cy.fixture() command. We then use the cy.get() command to select the file input element on the page, and use the upload() command to upload the file content.

    Downloading a file:

    cy.downloadFile('https://example.com/file.pdf', 'downloads', 'file.pdf')
    
    

    In this example, we use the cy.downloadFile() command to download a file from the specified URL (https://example.com/file.pdf) and save it to the downloads folder with the name file.pdf.

    Fixtures are a powerful feature of Cypress that allow you to provide data to your tests. Fixtures are easy to use and can be used to provide static or dynamic data to your tests. By using fixtures, you can make your tests more flexible and easier to maintain.