是赛普拉斯的新手,并试图作为公司的概念证明。我在通过POST请求无法测试React应用程序时遇到问题。以下是赛普拉斯电子应用程序中收到的错误消息:
超时重试:cy.wait()超时,等待1秒钟等待5000ms 路由请求:“ formRequest”。从未发生过请求。
以下是赛普拉斯commands.js
中使用的代码段:
Cypress.Commands.add("form_request", (method, urlPathParam, formData) => {
return cy
.server()
.route(method, "https://admin.teamapp.myhelpling.com" + urlPathParam)
.as("formRequest")
.window()
.then(win => {
const xhr = new XMLHttpRequest();
xhr.open(method, "https://admin.teamapp.myhelpling.com" + urlPathParam);
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("access-token", accesstoken);
xhr.setRequestHeader("client", client);
xhr.setRequestHeader("expiry", expiry);
xhr.setRequestHeader("token-type", tokentype);
xhr.setRequestHeader("uid", uid);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(formData);
})
.wait("@formRequest");
});
在测试规范文件中,以下是测试用例中的代码
cy.AppLogin();
let dataname = "Test FORMDATA API";
let formData = new FormData();
formData.append("client[name]", dataname);
formData.append(
"client[client_logo_attributes][content]",
cy.fixture("/images/clients/Golden JPEG.jpeg")
);
cy.form_request("POST", "/admin/clients", formData).then(response => {
cy.log(response.status);
});
此外,如果我在上面的.wait("@formRequest");
代码中注释了commands.js
,则会显示以下错误:
POST https://admin.teamapp.myhelpling.com/admin/clients 405(方法 不允许)
在开发工具的“源”标签中,我在下面提供的xhr.send(formData)
行上看到一个X(十字)标记:
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryAGOGnFVPveAa8gfv");
xhr.send(formData);
}); //.wait("@formRequest");
});
第二,有人还能帮助分享一些链接来调试我的计算机上的框架吗?
更新
该被测应用程序接受以下格式的表单数据:
client[name] = "Cypress TER Client"
client[client_logo_attributes][content] = Some_Blob
使用cy.Request()
,我假设我必须将其转换为JSON作为请求的主体。这是FormData向JSON的以下转换:
body: {
client: {
name: "Cypress TER Client",
client_logo_attributes: {
content: "fx:../images/clients/Bull Client.jpg"
}
}
我认为我的做法不正确。请提出建议。
我检查了POSTMAN如何发出此请求,并尝试了该请求,但没有运气。这是我尝试的代码:
body: qs.stringify({
mode: "formdata",
formdata: [
{
key: "client[name]",
value: "Cypress TER Client",
type: "text"
},
{
key: "client[client_logo_attributes][content]",
type: "file",
src: cy.readFile("cypress//fixtures//images/clients//Bull Client.jpg")
}
]
})
感谢Eric。根据您在下面的反馈,我将现有代码修改如下:
Cypress.Commands.add("formrequest", (method, url, formData, done) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("access-token", accesstoken);
xhr.setRequestHeader("client", client);
xhr.setRequestHeader("expiry", expiry);
xhr.setRequestHeader("token-type", tokentype);
xhr.setRequestHeader("uid", uid);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(formData);
xhr.onload = function() {
done(xhr);
};
xhr.onerror = function() {
done(xhr);
};
});
现在的呼叫代码如下:
it.only("Test", () => {
cy.AppLogin();
let blobObj = cy.create_blob(
"/images/clients/Bull Client.jpg",
"image/jpg"
);
dataname = "Test FORMDATA API";
formData = new FormData();
formData.append("client[name]", dataname);
formData.append("client[client_logo_attributes][content]", blobObj);
let url = "https://admin.teamapp.myhelpling.com/admin/clients";
let method = "POST";
cy.formrequest(method, url, formData, function(response) {
expect(response.status).to.eq(200);
});
});
没有运气。
该被测应用程序接受以下格式的表单数据:
client[name] = "Cypress TER Client"
client[client_logo_attributes][content] = Some_Blob
以下是FormData
到JSON的转换:
body: {
client: {
name: "Cypress TER Client",
client_logo_attributes: {
content: "fx:../images/clients/Bull Client.jpg"
}
}
我认为我的做法不正确。请提出建议。
我检查了POSTMAN如何发出此请求,并尝试了该请求,但没有运气。这是我尝试的代码:
body: qs.stringify({
mode: "formdata",
formdata: [
{
key: "client[name]",
value: "Cypress TER Client",
type: "text"
},
{
key: "client[client_logo_attributes][content]",
type: "file",
src: cy.readFile("cypress//fixtures//images/clients//Bull Client.jpg")
}
]
})