Edu*_*oso 5 javascript reactjs e2e-testing 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');
Run Code Online (Sandbox Code Playgroud)
如果我在测试期间检查控制台,则可以看到请求是对服务器的
我的客户端和服务器都在本地运行,但运行在不同的端口。
错误如下:
CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'uploadShowroom'. No request ever occurred.
我认为这是因为您的表单正在使用本机表单提交,但 Cypresscy.route()只响应 XHR 调用(目前)。
第 #170 期有一个很大的讨论。
Gleb Bahmutov 在这个评论中有一个有趣的想法,代码在这个存储库中。本质上,他通过 XHR 提交动态地“模拟”本机提交。
我尝试了一种更接近您的场景的变体。按照存储库 READ.ME 设置测试,但首先更新package.json. 将要上传的文件添加到/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;
});
})
);
Run Code Online (Sandbox Code Playgroud)
使用 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
})
}
Run Code Online (Sandbox Code Playgroud)
规格
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')
})
})
Run Code Online (Sandbox Code Playgroud)
在测试之间删除上传文件的任务(修改“/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
}
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1375 次 |
| 最近记录: |