赛普拉斯路由功能未检测到网络请求
Cypress route function not detecting the network request
我正在尝试等待我的应用程序发出的请求,但 Cypress 在 cy.wait
上未检测到它
cy.server();
cy.getAuthenticatedUrl();
cy.route('POST', '/showroom/validate').as('uploadShowroom');
cy.get('[data-testid=Next]').click();
cy.uploadFile('[id=uploadArea]', 'testfile-valid.xlsx', 'application/vnd.ms-excel');
cy.wait('@uploadShowroom');
cy.contains('FILE UPLOAD DONE');
如果我在测试期间检查我的控制台,我可以看到请求是对我的服务器发出的
我的客户端和服务器都是 运行 本地的,但在不同的端口。
错误如下:
CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'uploadShowroom'. No request ever occurred.
尝试覆盖默认的等待超时:
cy.wait('@uploadShowroom', {超时: 10000);
我认为这是因为您的表单使用的是原生表单提交,但 Cypress 的 cy.route()
仅响应 XHR 调用(目前)。
issue #170中有很大的讨论。
Gleb Bahmutov 在这个 comment, code in this repository 中有一个有趣的想法。本质上,他 'mocks' 本地提交和 XHR 提交,在运行中。
我尝试了一种更接近您的场景的变体。按照存储库 READ.ME 设置测试,但首先更新 package.json
中的 Cypress 版本。添加要上传的文件到/cypress/fixtures
.
然后尝试以下规范。
第三个测试是使用 cy.url()
而不是 cy.route()
的替代方法。
uploadFile 命令(或类似变体)
Cypress.Commands.add('uploadFile', (fileName, selector) =>
cy.get(selector).then(subject => {
return cy
.fixture(fileName, 'base64')
.then(Cypress.Blob.base64StringToBlob)
.then(blob => {
const el = subject[0];
const testFile = new File([blob], fileName, {
type: 'application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet',
});
const dataTransfer = new DataTransfer();
dataTransfer.items.add(testFile);
el.files = dataTransfer.files;
return subject;
});
})
);
函数 'mock' 使用 XHR 的本机提交(放置在规范顶部)
const mockNativeSubmitWithXhr = (form$) => {
let win
cy.window().then(w => {
win = w
})
form$.on('submit', e => {
e.preventDefault()
const form = new FormData(e.target)
const XHR = new win.XMLHttpRequest()
XHR.onload = response => {
win.document.write(XHR.responseText)
win.history.pushState({}, '', XHR.url)
}
XHR.open(e.target.method, e.target.action)
XHR.send(form)
return true
})
}
规格
describe('waiting for form-data post', () => {
beforeEach(() => {
cy.task('deleteFile', '../../uploads/Sample_data.xlsx')
cy.visit('localhost:3000')
cy.get('input[name="userid"]').type('foo@bar.com')
})
it('upload with native form submit (fails because of native event)', () => {
cy.server()
cy.route('POST', '/upload').as('upload');
cy.uploadFile('Sample_data.xlsx', '[id=fileToUpload]')
cy.get('input[type="submit"]').click()
cy.wait('@upload');
cy.readFile('uploads/Sample_data.xlsx') // check saved file
})
it('upload with form submit mocked to XHR send (succeeds)', () => {
cy.server()
cy.route('POST', '/upload').as('upload');
cy.uploadFile('Sample_data.xlsx', '[id=fileToUpload]')
cy.get('form').then(form => mockNativeSubmitWithXhr(form))
cy.get('input[type="submit"]').click()
cy.wait('@upload');
cy.readFile('uploads/Sample_data.xlsx')
})
it('upload with native form submit (testing url has changed)', () => {
cy.uploadFile('Sample_data.xlsx', '[id=fileToUpload]')
cy.get('input[type="submit"]').click()
cy.url().should('eq', 'http://localhost:3000/upload')
cy.readFile('uploads/Sample_data.xlsx')
})
})
在测试之间删除上传文件的任务(修改'/cypress/plugins/index.js')
const fs = require('fs')
module.exports = (on, config) => {
on('task', {
deleteFile: (path) => {
if (fs.existsSync(path)) {
fs.unlinkSync(path)
}
return null
}
})
}
我运行在类似的东西。诀窍不是根据字符串检查路由,而是根据正则表达式检查路由。试试这个来匹配你的 POST 路线:
cy.route('POST', /showroom\/validate/).as('uploadShowroom');
// [...]
cy.wait('@uploadShowroom');
如果您可以在 cypress 命令日志中看到匹配的路由(路由旁边的黄色标记),它应该可以,如您所愿。
我正在尝试等待我的应用程序发出的请求,但 Cypress 在 cy.wait
cy.server();
cy.getAuthenticatedUrl();
cy.route('POST', '/showroom/validate').as('uploadShowroom');
cy.get('[data-testid=Next]').click();
cy.uploadFile('[id=uploadArea]', 'testfile-valid.xlsx', 'application/vnd.ms-excel');
cy.wait('@uploadShowroom');
cy.contains('FILE UPLOAD DONE');
如果我在测试期间检查我的控制台,我可以看到请求是对我的服务器发出的
我的客户端和服务器都是 运行 本地的,但在不同的端口。
错误如下:
CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'uploadShowroom'. No request ever occurred.
尝试覆盖默认的等待超时:
cy.wait('@uploadShowroom', {超时: 10000);
我认为这是因为您的表单使用的是原生表单提交,但 Cypress 的 cy.route()
仅响应 XHR 调用(目前)。
issue #170中有很大的讨论。
Gleb Bahmutov 在这个 comment, code in this repository 中有一个有趣的想法。本质上,他 'mocks' 本地提交和 XHR 提交,在运行中。
我尝试了一种更接近您的场景的变体。按照存储库 READ.ME 设置测试,但首先更新 package.json
中的 Cypress 版本。添加要上传的文件到/cypress/fixtures
.
然后尝试以下规范。
第三个测试是使用 cy.url()
而不是 cy.route()
的替代方法。
uploadFile 命令(或类似变体)
Cypress.Commands.add('uploadFile', (fileName, selector) =>
cy.get(selector).then(subject => {
return cy
.fixture(fileName, 'base64')
.then(Cypress.Blob.base64StringToBlob)
.then(blob => {
const el = subject[0];
const testFile = new File([blob], fileName, {
type: 'application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet',
});
const dataTransfer = new DataTransfer();
dataTransfer.items.add(testFile);
el.files = dataTransfer.files;
return subject;
});
})
);
函数 'mock' 使用 XHR 的本机提交(放置在规范顶部)
const mockNativeSubmitWithXhr = (form$) => {
let win
cy.window().then(w => {
win = w
})
form$.on('submit', e => {
e.preventDefault()
const form = new FormData(e.target)
const XHR = new win.XMLHttpRequest()
XHR.onload = response => {
win.document.write(XHR.responseText)
win.history.pushState({}, '', XHR.url)
}
XHR.open(e.target.method, e.target.action)
XHR.send(form)
return true
})
}
规格
describe('waiting for form-data post', () => {
beforeEach(() => {
cy.task('deleteFile', '../../uploads/Sample_data.xlsx')
cy.visit('localhost:3000')
cy.get('input[name="userid"]').type('foo@bar.com')
})
it('upload with native form submit (fails because of native event)', () => {
cy.server()
cy.route('POST', '/upload').as('upload');
cy.uploadFile('Sample_data.xlsx', '[id=fileToUpload]')
cy.get('input[type="submit"]').click()
cy.wait('@upload');
cy.readFile('uploads/Sample_data.xlsx') // check saved file
})
it('upload with form submit mocked to XHR send (succeeds)', () => {
cy.server()
cy.route('POST', '/upload').as('upload');
cy.uploadFile('Sample_data.xlsx', '[id=fileToUpload]')
cy.get('form').then(form => mockNativeSubmitWithXhr(form))
cy.get('input[type="submit"]').click()
cy.wait('@upload');
cy.readFile('uploads/Sample_data.xlsx')
})
it('upload with native form submit (testing url has changed)', () => {
cy.uploadFile('Sample_data.xlsx', '[id=fileToUpload]')
cy.get('input[type="submit"]').click()
cy.url().should('eq', 'http://localhost:3000/upload')
cy.readFile('uploads/Sample_data.xlsx')
})
})
在测试之间删除上传文件的任务(修改'/cypress/plugins/index.js')
const fs = require('fs')
module.exports = (on, config) => {
on('task', {
deleteFile: (path) => {
if (fs.existsSync(path)) {
fs.unlinkSync(path)
}
return null
}
})
}
我运行在类似的东西。诀窍不是根据字符串检查路由,而是根据正则表达式检查路由。试试这个来匹配你的 POST 路线:
cy.route('POST', /showroom\/validate/).as('uploadShowroom');
// [...]
cy.wait('@uploadShowroom');
如果您可以在 cypress 命令日志中看到匹配的路由(路由旁边的黄色标记),它应该可以,如您所愿。