#javascript #cypress
#javascript #cypress
Вопрос:
У меня есть набор сквозных тестов Cypress для моего веб-приложения. При выполнении тестов вызовы AJAX выполняются на реальном сервере (т. Е. Запросы не обрабатываются и не прерываются). В настоящее время я делаю что-то вроде следующего, чтобы убедиться, что тесты ожидают завершения этих AJAX-запросов
// declare the AJAX request we will wait for
cy.route('POST', '/api/authenticate').as('authenticate')
// type the username and password and click the login button
cy.get('#username').type('john-doe')
cy.get('#password').type('secret')
cy.get('#login-button').click()
// wait for the AJAX request to complete before testing the response code
cy.wait('@authenticate')
cy.get('@authenticate').then(xhr => {
expect(xhr.status).to.eq(200)
})
Это кажется очень подробным, есть ли более простой способ? Я использую последнюю версию Cypress, v. 6.1.0.
Ответ №1:
cy.server() и cy.route() устарели в Cypress 6.0.0. Вместо cypress введен cy.intercept(). Итак, вы можете написать что-то вроде:
// declare the AJAX request we will wait for
cy.intercept('POST', '/api/authenticate').as('authenticate')
// type the username and password and click the login button
cy.get('#username').type('john-doe')
cy.get('#password').type('password')
cy.get('#login-button').click()
// wait till we get 200
cy.wait('@authenticate').its('response.statusCode').should('eq', 200)
//You can also assert request body and response body contents
cy.wait('@authenticate').its('request.body').should('include', 'username')
cy.wait('@authenticate').its('response.body').should('include', 'password')
Ответ №2:
В приведенном здесь примере это написано так.
cy.wait('@authenticate').should('have.property', 'status', 200)
Не очень большая экономия, но это уже кое-что 🙂
Редактировать: я только что заметил, что вы сказали, что у вас последняя версия. В этом случае я бы последовал предложению Алапана.