英文:
How to identify if there is an actual issue in the code from cypress
问题
我正在尝试使用Cypress编写测试脚本,在这种情况下,系统中存在实际的500服务器错误。但是当我尝试使用Cypress自动化时,它不会给我任何错误并且正常运行。这是因为我使用了固定数据吗?有人可以告诉我在Cypress中处理这种情况的程序是什么吗?
最后,我希望Cypress在系统出现实际错误时抛出错误,一旦错误被修复,它应该再次正常运行,测试应该通过。
以下是你提供的代码部分,我不会翻译代码部分:
it('Add less than 400 (314) characters to value added service', function () {
cy.intercept("POST", "**/api/update_profile/overview", {
fixture: "supplier_profile_company_overview.json",
}).as('submitCompanyOverview');
cy.intercept("POST", "**/api/login/select_supplier_role", {
fixture: "supplier_profile_submit_select_supplier_role.json",
}).as('selectSupplierRole');
cy.fixture('supplier_profile_sample_characters_314.json')
.then((data) => {
cy.xpath('(//div[@class=\'v-input__slot\'])[5]')
.type(data.description);
});
cy.xpath('//button[@class=\'md-button save-btn md-theme-default\']')
.click();
cy.contains('Company Information Updated').should('be.visible');
cy.wait('@submitCompanyOverview').then(({response}) => {
expect(response.statusCode).to.eq(200);
});
cy.wait('@selectSupplierRole').then(({response}) => {
expect(response.statusCode).to.eq(200);
});
});
英文:
I am trying to write a test script using Cypress and in this scenario, there is an actual 500 server error in the system. But when I try to automate this using Cypress it does not give me any error and works fine. Is it because I am using fixtures? Could anyone tell me what is the procedure for handling these kinds of situations in Cypress?
Finally, I want cypress to throw an error if there is an actual error in the system and once it was fixed it should work fine again and the test should pass.
it('Add less than 400 (314) characters to value added service', function (){
cy.intercept("POST", "**/api/update_profile/overview", {
fixture: "supplier_profile_company_overview.json",
}).as('submitCompanyOverview');
cy.intercept("POST", "**/api/login/select_supplier_role", {
fixture: "supplier_profile_submit_select_supplier_role.json",
}).as('selectSupplierRole');
cy.fixture('supplier_profile_sample_characters_314.json')
.then((data)=> {
cy.xpath('(//div[@class=\'v-input__slot\'])[5]')
.type(data.description);
});
cy.xpath('//button[@class=\'md-button save-btn md-theme-default\']')
.click();
cy.contains('Company Information Updated').should('be.visible');
cy.wait('@submitCompanyOverview').then(({response}) =>{
expect(response.statusCode).to.eq(200);
});
cy.wait('@selectSupplierRole').then(({response}) =>{
expect(response.statusCode).to.eq(200);
});
});
答案1
得分: 2
以下是您要翻译的内容:
"The usual procedure is to separate front end and back end tests.
If you try to us fixture
and also test for status 500
in the same test, it will be extremely confusing when you look at it in a months time and try to debug.
Instead, have a new test to catch status 500
result from the server.
Back end
context('server tests', () => {
it('checks update profile', () => {
cy.fixture('supplier_profile_sample_characters_314.json').then((data) => {
cy.request("POST", "**/api/update_profile/overview", {
data.description
})
.then(({response}) =>{
expect(response.statusCode).to.eq(200);
})
})
})
Front end
it('Add less than 400 (314) characters to value added service', function (){
cy.intercept("POST", "**/api/update_profile/overview", {
fixture: "supplier_profile_company_overview.json",
}).as('submitCompanyOverview');
cy.intercept("POST", "**/api/login/select_supplier_role", {
fixture: "supplier_profile_submit_select_supplier_role.json",
}).as('selectSupplierRole');
cy.fixture('supplier_profile_sample_characters_314.json')
.then((data) => {
cy.xpath('(//div[@class=\'v-input__slot\'])[5]')
.type(data.description);
});
cy.xpath('//button[@class=\'md-button save-btn md-theme-default\']')
.click();
cy.contains('Company Information Updated').should('be.visible');
})
```"
<details>
<summary>英文:</summary>
The usual procedure is to separate front end and back end tests.
If you try to us `fixture` and also test for `status 500` in the same test, it will be extremely confusing when you look at it in a months time and try to debug.
Instead, have a new test to catch `status 500` result from the server.
**Back end**
```js
context('server tests', () => {
it('checks update profile', () => {
cy.fixture('supplier_profile_sample_characters_314.json').then((data)=> {
cy.request("POST", "**/api/update_profile/overview", {
data.description
})
.then(({response}) =>{
expect(response.statusCode).to.eq(200);
})
})
})
Front end
it('Add less than 400 (314) characters to value added service', function (){
cy.intercept("POST", "**/api/update_profile/overview", {
fixture: "supplier_profile_company_overview.json",
}).as('submitCompanyOverview');
cy.intercept("POST", "**/api/login/select_supplier_role", {
fixture: "supplier_profile_submit_select_supplier_role.json",
}).as('selectSupplierRole');
cy.fixture('supplier_profile_sample_characters_314.json')
.then((data)=> {
cy.xpath('(//div[@class=\'v-input__slot\'])[5]')
.type(data.description);
});
cy.xpath('//button[@class=\'md-button save-btn md-theme-default\']')
.click();
cy.contains('Company Information Updated').should('be.visible');
})
答案2
得分: 0
>But when I try to automate this using Cypress it does not give me any error and works fine. Is it because I am using fixtures?
当我尝试使用Cypress自动化执行时,它没有报错并且正常工作。这是因为我使用了fixtures吗?
You are not receiving errors from your backend because you are using cy.intercept()
, which will return the response that you specify. In this case, you are returning the fixture, and because you did not provide a status code, it defaults to a 200 response.
您没有从后端收到错误是因为您使用了cy.intercept()
,它将返回您指定的响应。在这种情况下,您返回了fixture,并且因为您没有提供状态代码,它默认为200响应。
> Could anyone tell me what is the procedure for handling these kinds of situations in Cypress?
有人可以告诉我在Cypress中处理这种情况的程序是什么吗?
This depends on your requirements and what you are trying to test. If your priority is to validate that the backend and frontend are working together correctly, you would probably want to go without the cy.intercept()
s and let the tests fail when the backend does not return successful responses. If your priority is to validate the UI of the frontend under some condition, then using cy.intercept()
to force that state is probably the preferred method.
这取决于您的需求以及您试图测试的内容。如果您的优先级是验证后端和前端是否正确协同工作,您可能不希望使用cy.intercept()
,并让测试在后端未返回成功响应时失败。如果您的优先级是在某些条件下验证前端UI,那么使用cy.intercept()
来强制该状态可能是首选方法。
> I want cypress to throw an error if there is an actual error in the system and once it was fixed it should work fine again and the test should pass.
我希望Cypress在系统中出现实际错误时抛出错误,一旦错误被修复,测试应该再次正常运行并通过。
There are two clear options I see for accomplishing this - you can remove the intercepts, which will cause Cypress to fail tests whenever the backend response is not a successful response, or you can do a cy.intercept()
that returns fixture data, but only if the real response from the backend is successful.
我看到有两个明显的选项可以实现这一点 - 您可以删除intercepts,这将导致Cypress在后端响应不成功时失败测试,或者您可以进行一个cy.intercept()
,返回fixture数据,但仅当来自后端的真实响应成功时。
英文:
>But when I try to automate this using Cypress it does not give me any error and works fine. Is it because I am using fixtures?
You are not receiving errors from your backend because you are using cy.intercept()
, which will return the response that you specify. In this case, you are returning the fixture, and because you did not provide a status code, it defaults to a 200 response.
> Could anyone tell me what is the procedure for handling these kinds of situations in Cypress?
This depends on your requirements and what you are trying to test. If your priority is to validate that the backend and frontend are working together correctly, you would probably want to go without the cy.intercept()
s and let the tests fail when the backend does not return successful responses. If your priority is to validate the UI of the frontend under some condition, then using cy.intercept()
to force that state is probably the preferred method.
> I want cypress to throw an error if there is an actual error in the system and once it was fixed it should work fine again and the test should pass.
There are two clear options I see for accomplishing this - you can remove the intercepts, which will cause Cypress to fail tests whenever the backend response is not a successful response, or you can do a cy.intercept()
that returns fixture data, but only if the real response from the backend is successful.
cy.intercept({
method: "POST",
url: "**/api/update_profile/overview"
}, (req) => { // don't provide a response yet
req.continue((res) => { // req.continue sends the request to the backend
if (res.statusCode < 400) { // checks if we get a 2XX or 3XX status code
// If successful statusCode, send our fixture instead of actual response
res.send({ fixture: "supplier_profile_company_overview.json" });
} else {
// If non-2XX/3XX statusCode, send the response as we receive it
res.send(res.statusCode, res.body, res.headers)
}
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论