如何(我可以吗?)根据请求数据在拦截中加载 Cypress fixture?
How to (can i?) load Cypress fixture inside intercept based on request data?
我有一堆由后端生成的固定装置,并根据请求正文的哈希值命名。
我正在尝试找到一种根据发送的请求动态加载固定装置的方法,如下所示:
cy.intercept('POST', '**/login', (req) => {
const hash = md5(req.body);
cy.fixture(`${hash}.json`).then(data => {
req.reply(res => {
res.statusCode = data.statusCode;
res.body = data.body;
})
})
}).as('response');
cy.visit("/login");
cy.get('input[name="email"]').type('test@email.com');
cy.get('input[name="password"]').type('wrongpassword');
cy.contains('button', 'Login').click();
cy.wait('@response');
cy.contains('Invalid credentials.');
但是每次我尝试在拦截器内加载夹具时,我都会收到以下错误:
The following error originated from your test code, not from Cypress.
> A request callback passed to cy.intercept() threw an error while intercepting a request:
Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
The command that returned the promise was:
> cy.wait()
The cy command you invoked inside the promise was:
> cy.fixture()
有什么方法可以根据请求中的内容动态加载固定装置吗?
你可以先看夹具
cy.fixture('login_incorrect.json').then(data => { // may not need it, but in case...
cy.intercept('POST', '**/login', (req) => {
const body = JSON.parse(req.body);
if(body.password === 'wrongpassword') {
req.reply(res => {
res.statusCode = data.statusCode; // data = closure from above
res.body = data.body;
})
} else {
// load a different fixture based on something, etc.
}
}).as('response');
})
组合夹具
{
"put-first-hash-here": {
statusCode: ...,
body: ...
},
"put-second-hash-here": {
statusCode: ...,
body: ...
},
}
测试
cy.fixture('combined.json').then(data => {
cy.intercept('POST', '**/login', (req) => {
const body = JSON.parse(req.body);
const hash = md5(req.body);
if(body.password === 'wrongpassword') {
req.reply(res => {
res.statusCode = data[hash].statusCode;
res.body = data[hash].body;
})
}
}).as('response');
})
我有一堆由后端生成的固定装置,并根据请求正文的哈希值命名。 我正在尝试找到一种根据发送的请求动态加载固定装置的方法,如下所示:
cy.intercept('POST', '**/login', (req) => {
const hash = md5(req.body);
cy.fixture(`${hash}.json`).then(data => {
req.reply(res => {
res.statusCode = data.statusCode;
res.body = data.body;
})
})
}).as('response');
cy.visit("/login");
cy.get('input[name="email"]').type('test@email.com');
cy.get('input[name="password"]').type('wrongpassword');
cy.contains('button', 'Login').click();
cy.wait('@response');
cy.contains('Invalid credentials.');
但是每次我尝试在拦截器内加载夹具时,我都会收到以下错误:
The following error originated from your test code, not from Cypress.
> A request callback passed to cy.intercept() threw an error while intercepting a request:
Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
The command that returned the promise was:
> cy.wait()
The cy command you invoked inside the promise was:
> cy.fixture()
有什么方法可以根据请求中的内容动态加载固定装置吗?
你可以先看夹具
cy.fixture('login_incorrect.json').then(data => { // may not need it, but in case...
cy.intercept('POST', '**/login', (req) => {
const body = JSON.parse(req.body);
if(body.password === 'wrongpassword') {
req.reply(res => {
res.statusCode = data.statusCode; // data = closure from above
res.body = data.body;
})
} else {
// load a different fixture based on something, etc.
}
}).as('response');
})
组合夹具
{
"put-first-hash-here": {
statusCode: ...,
body: ...
},
"put-second-hash-here": {
statusCode: ...,
body: ...
},
}
测试
cy.fixture('combined.json').then(data => {
cy.intercept('POST', '**/login', (req) => {
const body = JSON.parse(req.body);
const hash = md5(req.body);
if(body.password === 'wrongpassword') {
req.reply(res => {
res.statusCode = data[hash].statusCode;
res.body = data[hash].body;
})
}
}).as('response');
})