【发布时间】:2021-10-10 00:49:51
【问题描述】:
我整天都在做这件事,我真的需要朝着正确的方向轻推。
我的依赖是 -
"dependencies": {
"typescript": "^4.3.5",
"cypress": "^8.1.0",
"cypress-file-upload": "^5.0.8"
}
我有一个叫uploadBlob.txt的夹具
这就是它的样子-
------WebKitFormBoundary7BhOPSS0NpEAppSA
Content-Disposition: form-data; name="UploadedFileName"
Prod_CA.ACI
------WebKitFormBoundary7BhOPSS0NpEAppSA
Content-Disposition: form-data; name="OrderId"
7815968_13735
------WebKitFormBoundary7BhOPSS0NpEAppSA
Content-Disposition: form-data; name="Options[orderid]"
7815968_13735
------WebKitFormBoundary7BhOPSS0NpEAppSA
Content-Disposition: form-data; name="Options[clientcode]"
1135
------WebKitFormBoundary7BhOPSS0NpEAppSA
Content-Disposition: form-data; name="Options[vendorserviceurl]"
[... 80+ More Items ...]
------WebKitFormBoundary7BhOPSS0NpEAppSA--
我想做的是这样的-
Cypress.Commands.add("formRequest", (info: ReqInfo) => {
cy.readFile("./fixtures/uploadBlob.txt", "utf-8").then(fixture => {
const blob = Cypress.Blob.binaryStringToBlob(fixture, "application/text");
const formData = new FormData();
formData.append('file', blob, "uploadBlob.txt");
return cy.request({
url: info.url,
method: info.method,
headers: info.headers,
form: true,
body: formData
})
})
});
info 看起来像这样 -
let info : ReqInfo = {
method: "POST",
url : 'https://uat-delivery.acisky.com/Delivery/bkfs/Start',
headers: {
'Authority': 'uat-delivery.acisky.com',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7BhOPSS0NpEAppSA',
'Path': '/DeliveryUpload/bkfs/UploadFile',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
}
}
这是我得到的错误 -
From Node.js Internals:
TypeError [ERR_INVALID_ARG_TYPE] [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
at Function.from (buffer.js:333:9)
at Object.sendPromise
最后我基于这些尝试了XMLHttpRequest() 的多次迭代 -
- In Cypress sending form data using POST request is not working
- Multipart formdata POST request just doesn't work in Cypress for me
- How to Upload txt file with Cypress for API Testing - XMLHTTPRequest?
例如-
Cypress.Commands.add("formRequest", (info: ReqInfo) => {
cy.readFile("./fixtures/uploadBlob.txt", "utf-8").then(fixture => {
const blob = Cypress.Blob.binaryStringToBlob(fixture, "application/text");
const formData = new FormData();
formData.append('file', blob, "uploadBlob.txt");
const xhr = new XMLHttpRequest();
xhr.responseType = 'text';
xhr.onload = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
console.log(xhr.response);
console.log(xhr.responseText);
}
}
};
xhr.open(info.method, info.url);
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("content-Type", "multipart/form-data");
xhr.setRequestHeader('Authority', 'uat-delivery.acisky.com'),
xhr.send(formData);
})
但在这里我得到了错误-
{"errors":["No file found in request"]}
即使我可以看到ReadFile 工作正常。
我使用WebKitBoundry 使用 readFile/fixture 路线的原因是因为有超过 80 个“字段”,并且在未来,我们将需要该文件的 20-30 个变体。
如果有人对如何完成这项任务有任何想法,我会很乐意提供帮助。
谢谢
【问题讨论】:
-
在哪里可以找到
the first argument must be....问题的解决方案?我遇到了同样的问题。 -
@Edward - 我刚刚发布了我的答案,希望对您有所帮助。
标签: typescript xmlhttprequest cypress