1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Cypressテストコード(Request関連)

Last updated at Posted at 2019-11-24

API

integration/sample_spec.js
describe('request test', function() {
  it('', function() {
    cy.server()
    cy.route('GET','**/getUsers').as('getUsers')
    cy.wait('@getUsers')

    cy.get('#results').should('contain', 'user1')
  })
})

Stub

integration/sample_spec.js
describe('request test', function() {
  it('', function() {
    cy.server()
    cy.route('GET', '**/getUsers',
    {data: [
      {id: '1',name: 'user1'},
      {id: '2',name: 'user2'}
    ]}).as('getUsers')

    cy.wait('@getUsers')

    cy.get('#results').should('contain', 'user1')
  })
})

Fixtures

integration/sample_spec.js
describe('request test', function() {
  it('', function() {
    cy.server()
    cy.route('GET', '**/getUsers', 'fixture:getUsers').as('getUsers')

    cy.wait('@getUsers')

    cy.get('#results').should('contain', 'user1')
  })
})
fixtures/getUsers.json
{
  "data": [{
      "id": "1",
      "name": "user1"
    },
    {
      "id": "2",
      "name": "user2"
    }
  ]
}

階層が深くてもいい

integration/sample_spec.js
describe('request test', function() {
  it('', function() {
    cy.server()
    cy.route('GET', '**/getUsers', 'fixture:test/getUsers').as('getUsers')

    cy.wait('@getUsers')

    cy.get('#results').should('contain', 'user1')
  })
})
fixtures/test/getUsers.json
{
  "data": [{
      "id": "1",
      "name": "user1"
    },
    {
      "id": "2",
      "name": "user2"
    }
  ]
}

複数Wait

integration/sample_spec.js
cy.server()
cy.route('activities/*', 'fixture:activities').as('getActivities')
cy.route('messages/*', 'fixture:messages').as('getMessages')

cy.visit('http://localhost:8888/dashboard')

cy.wait(['@getActivities', '@getMessages'])

cy.get('h1').should('contain', 'Dashboard')
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?