使用Cypress自动化一个需要已登录用户的表单。

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

Automating a form which requires a signed in user using Cypress

问题

我目前正在自动化一个表单(问卷调查),为了让用户填写这个表单,他们必须登录。

用户登录后,在问卷上选择一个选项,就会直接调用API将其保存到数据库中。

我正在使用Cypress编写测试,目前已成功自动化了登录过程,并通过表单选择了选项。然而,因为我已经使用这个用户完成了问卷调查,结果已经保存下来了。

我的问题是,如何使用Cypress自动化这个过程?我是否需要每次删除并重新创建用户(API目前不支持删除功能,只支持禁用)?我是否需要像编写单元测试时那样呈现组件(例如React Testing Library)- 但这会破坏端到端的自动化目的,因此(在我看来)最好的方式是实际上完成问卷调查。

我对编写测试还相对陌生,任何指导都将不胜感激。我相信这样的模式有很多,我只需要找到解决方法。

在使用Cypress自动化需要已验证用户并保存用户状态的网页时,通常要做什么,测试套件每天运行多次,目前没有删除用户的选项。

英文:

I am currently working on automating a form (Questionnaire), in order for a user to fill out this form they must be logged in.

After a user logs in, and chooses an option on the questionnaire, an API call is directly made to save this in the database.

I am writing a test using Cypress and have currently managed to automate the log in process, and choosing the options through the form. However, since I have gone through the questionaire with this user, the results are saved down.

My question is, how can I automate this using Cypress? Do I need to delete and re-create the user each time (API currently doesn't support delete functionality, only disable). Am I to render the component like one does when writing unit tests (e.g. React Testing Library) - but this would defeat the purpose end-to-end automation hence (in my opinion) preference is that we actually go through the questioner.

I am fairly new to writing tests and any guidance would be much appreciated. I am sure there are an abundance of such patterns out there and I just need to figure it out.

What does one usually do when automating a webpage using cypress that requires an authenticated user and saves the state of the user, the test suite is run multiple times a day - and there is (currently) no option to delete a user.

答案1

得分: 3

It sounds like you could handle the situation by intercepting the POST requests and stubbing them, so that the backend never gets updated.

For reference, see cy.intercept() - spying and response stubbing

cy.intercept('POST', '/users*', {
  statusCode: 201,
  body: {
   name: 'Peter Pan',
  },
})

This is (marginally?) better than unit testing, because you are in fact confirming that the API is being hit and that the correct calls are being made by the front end.

英文:

It sounds like you could handle the situation by intercepting the POST requests and stubbing them, so that the backend never gets updated.

For reference, see cy.intercept() - spying and response stubbing

> js
> cy.intercept('POST', '/users*', {
> statusCode: 201,
> body: {
> name: 'Peter Pan',
> },
> })
>

This is (marginally?) better than unit testing, because you are in fact confirming that the API is being hit and that the correct calls are being made by the front end.

huangapple
  • 本文由 发表于 2023年7月3日 22:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605630.html
匿名

发表评论

匿名网友

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

确定