如何识别Cypress中的代码是否存在实际问题

huangapple go评论46阅读模式
英文:

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(&#39;server tests&#39;, () =&gt; {

  it(&#39;checks update profile&#39;, () =&gt; {

    cy.fixture(&#39;supplier_profile_sample_characters_314.json&#39;).then((data)=&gt; {

      cy.request(&quot;POST&quot;, &quot;**/api/update_profile/overview&quot;, {
        data.description
      })
      .then(({response}) =&gt;{
        expect(response.statusCode).to.eq(200);
      })
  })
})

Front end

it(&#39;Add less than 400 (314) characters to value added service&#39;, function (){

  cy.intercept(&quot;POST&quot;, &quot;**/api/update_profile/overview&quot;, {
    fixture: &quot;supplier_profile_company_overview.json&quot;,
  }).as(&#39;submitCompanyOverview&#39;);

  cy.intercept(&quot;POST&quot;, &quot;**/api/login/select_supplier_role&quot;, {
    fixture: &quot;supplier_profile_submit_select_supplier_role.json&quot;,
  }).as(&#39;selectSupplierRole&#39;);

  cy.fixture(&#39;supplier_profile_sample_characters_314.json&#39;)
    .then((data)=&gt; {
      cy.xpath(&#39;(//div[@class=\&#39;v-input__slot\&#39;])[5]&#39;)
        .type(data.description);
    });

  cy.xpath(&#39;//button[@class=\&#39;md-button save-btn md-theme-default\&#39;]&#39;)
    .click();
  cy.contains(&#39;Company Information Updated&#39;).should(&#39;be.visible&#39;);
})

答案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: &quot;POST&quot;, 
  url: &quot;**/api/update_profile/overview&quot;
}, (req) =&gt; { // don&#39;t provide a response yet
  req.continue((res) =&gt; { // req.continue sends the request to the backend
    if (res.statusCode &lt; 400) { // checks if we get a 2XX or 3XX status code
      // If successful statusCode, send our fixture instead of actual response
      res.send({ fixture: &quot;supplier_profile_company_overview.json&quot; });
    } else {
      // If non-2XX/3XX statusCode, send the response as we receive it
      res.send(res.statusCode, res.body, res.headers)
    }
  });
});

huangapple
  • 本文由 发表于 2023年3月1日 15:01:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定