赛普拉斯根据请求截断 XHR 响应

时间:2021-05-19 11:38:02

标签: url-routing cypress stubbing

我是 Cypress 的初学者,正在寻求有关网络存根的帮助。 单击 UI 中的按钮时,我的 UI 会同时调用 3 个 API。所有 3 个 API 都具有相同的端点,但每个 API 都有不同的请求和响应。

我可以使用 cy.fixture、cy.server() 和 cy.route() 存根 json 响应。 我的需要是“仅存根第 3 个 XHR 调用响应”,但是,由于相同的端点,我的测试存根了所有三个。

关于如何使用任何条件测试它的任何建议?示例 - 如果“request”XHR 的参数为“XXX”,则仅存根调用? 我尝试在提交按钮的 .click() 之前和之后使用,但没有用。

cy.fixture('myfixture').then(jsonresponse => {
function FixtureController(request, response) {
                    if (cy.url().request.body.contains("XXX")) {
                        cy.server()
                        cy.route('POST', 'URL', jsonresponse).as('myalias')

感谢任何支持。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用 cy.intercept 进行匹配,包括查询参数。

do_not_add_to_tap(eq_or_diff(...))
# or 
increased during runtime the planned tests
plan() = plan()+1...

如果需要对请求正文内容进行匹配,可以使用路由处理程序来指定。

cy.intercept({
  url: 'http://example.com/search*',
  query: { q: 'expected terms' },
}, { fixture: 'myfixture' } )

查看cy.intercept documentation for more info.