files.spec.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /// <reference types="cypress" />
  2. /// JSON fixture file can be loaded directly using
  3. // the built-in JavaScript bundler
  4. // @ts-ignore
  5. const requiredExample = require('../../fixtures/example')
  6. context('Files', () => {
  7. beforeEach(() => {
  8. cy.visit('https://example.cypress.io/commands/files')
  9. })
  10. beforeEach(() => {
  11. // load example.json fixture file and store
  12. // in the test context object
  13. cy.fixture('example.json').as('example')
  14. })
  15. it('cy.fixture() - load a fixture', () => {
  16. // https://on.cypress.io/fixture
  17. // Instead of writing a response inline you can
  18. // use a fixture file's content.
  19. // when application makes an Ajax request matching "GET **/comments/*"
  20. // Cypress will intercept it and reply with the object in `example.json` fixture
  21. cy.intercept('GET', '**/comments/*', { fixture: 'example.json' }).as('getComment')
  22. // we have code that gets a comment when
  23. // the button is clicked in scripts.js
  24. cy.get('.fixture-btn').click()
  25. cy.wait('@getComment').its('response.body')
  26. .should('have.property', 'name')
  27. .and('include', 'Using fixtures to represent data')
  28. })
  29. it('cy.fixture() or require - load a fixture', function () {
  30. // we are inside the "function () { ... }"
  31. // callback and can use test context object "this"
  32. // "this.example" was loaded in "beforeEach" function callback
  33. expect(this.example, 'fixture in the test context')
  34. .to.deep.equal(requiredExample)
  35. // or use "cy.wrap" and "should('deep.equal', ...)" assertion
  36. cy.wrap(this.example)
  37. .should('deep.equal', requiredExample)
  38. })
  39. it('cy.readFile() - read file contents', () => {
  40. // https://on.cypress.io/readfile
  41. // You can read a file and yield its contents
  42. // The filePath is relative to your project's root.
  43. cy.readFile('cypress.json').then((json) => {
  44. expect(json).to.be.an('object')
  45. })
  46. })
  47. it('cy.writeFile() - write to a file', () => {
  48. // https://on.cypress.io/writefile
  49. // You can write to a file
  50. // Use a response from a request to automatically
  51. // generate a fixture file for use later
  52. cy.request('https://jsonplaceholder.cypress.io/users')
  53. .then((response) => {
  54. cy.writeFile('cypress/fixtures/users.json', response.body)
  55. })
  56. cy.fixture('users').should((users) => {
  57. expect(users[0].name).to.exist
  58. })
  59. // JavaScript arrays and objects are stringified
  60. // and formatted into text.
  61. cy.writeFile('cypress/fixtures/profile.json', {
  62. id: 8739,
  63. name: 'Jane',
  64. email: 'jane@example.com',
  65. })
  66. cy.fixture('profile').should((profile) => {
  67. expect(profile.name).to.eq('Jane')
  68. })
  69. })
  70. })