[英]How to Receive cypress interception fixture twice but in different format?
I'm a beginner with Cypress and I am stuggeling with the following:我是赛普拉斯的初学者,我正在努力解决以下问题:
I am calling the following from my test file:我从我的测试文件中调用以下内容:
cy.SetupClassificationsStubFixture(1);
This refers to the following command:这指的是以下命令:
Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications/*', slicedClassifications)
}).as('classifications') });
As you can see this customcommand is intercepting 2 api request.如您所见,此自定义命令正在拦截 2 个 api 请求。 Namely:
即:
The first interception is with [] The second intercecption needs exactly the same response buth without brackets []第一个拦截是 [] 第二个拦截需要完全相同的响应,但没有括号 []
But you already understand that both respons are now with brackets '[]'.但是你已经明白这两个响应现在都带有括号'[]'。 I tried to make a second fixture file without [], but the the slice function is not working.
我试图制作没有 [] 的第二个夹具文件,但切片 function 不起作用。
So I need: The second intercept like:所以我需要:第二个拦截像:
{
"id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
"name": "CypressTest",
"userRole": "Editor",
"childClassifications": []
}
But I get:但我得到:
[{
"id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
"name": "CypressTest",
"userRole": "Editor",
"childClassifications": []
}]
How could I get this array back without []?如果没有 [],我怎么能找回这个数组? Thankyou indeed
谢谢你
UPDATE: I am trying now the following:更新:我现在正在尝试以下内容:
Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
const arr2 = slicedClassifications
const obj5 = Object.fromEntries(arr2)
cy.intercept('GET', '**/api/classifications/*', obj5)
}).as('classification')});
But this give me a emtpy.json file.但这给了我一个 emtpy.json 文件。 It the respons is not just ' {} '
它的响应不仅仅是'{}'
So in case, your data looks like this:因此,以防万一,您的数据如下所示:
var data = [{
"id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
"name": "CypressTest",
"userRole": "Editor",
"childClassifications": []
}]
So to extract the data with just curly braces you can do data[0]
.因此,要仅使用花括号提取数据,您可以执行
data[0]
。
{
"id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
"name": "CypressTest",
"userRole": "Editor",
"childClassifications": []
}
Working example console screenshot:工作示例控制台屏幕截图:
There looks to be some brackets out of place in the code.代码中似乎有一些括号不合适。
I would also recommend using different alias names, otherwise the calls to cy.wait('@classifications')
may not work as you expect.我还建议使用不同的别名,否则对
cy.wait('@classifications')
的调用可能无法按预期工作。
Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications', slicedClassifications)
.as('classifications')
const firstClassification = slicedClassifications[0] // just first item
cy.intercept('GET', '**/api/classifications/*', firstClassification)
.as('firstClassification')
})
});
Finally solved with:最后解决了:
Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
cy.intercept('GET', '**/api/classifications/*', (req) => {
const slicedClassifications = classificationsStub.slice(0, amount)
var selectedClassification = req.url.replace(new RegExp('.*api/classifications/'), '');
let foundClassification = FindChildClassification(selectedClassification, slicedClassifications);
//If not found return first always
req.reply({
statusCode: 200,
body: foundClassification ?? slicedClassifications[0]
})
})
}).as('classification')
And和
export function FindChildClassification(id, classificationArray) {
for (let i = 0; classificationArray.length - 1 >= i; i++) {
if (classificationArray[i].id == id) {
return classificationArray[i];
} else {
if (classificationArray[i].childClassifications.length > 0) {
let childClassification = FindChildClassification(id, classificationArray[i].childClassifications);
if (childClassification != null) { return childClassification }
}
}
}
return null;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.