Cypress维护上下文

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

Cypress maintain context

问题

我们有一个拥有大量工作流程和大量测试的平台。目前,对于每个测试,我们必须完成一个长工作流程。为了加快速度,我想与所有的cookies、URL和所有数据一起分享上下文或浏览器,在每个描述内部的所有测试之间。

我也可以只创建一个测试用例,并把所有内容都放在其中,但我更愿意保持所有的测试用例函数,以使其更有组织性。

我知道我在处理反模式(anti patterns)...但这将显著提高测试速度。

那么...有办法实现这个吗?我找不到相关信息。

谢谢!

英文:

We have a platform with a lots of workflows and a lots of tests. Right now for each test we have to complete a long workflow. In order to speed up this, I want to share the context or the browser, with all the cookies, the url, and all the data between all the it inside each describe.

I could also crete just one it and put everything inside but i'll prefer to maintain all the it functions to have all more organizated.

I know that i'm dealing with anti patterns.. but this will improve significantly the speed of the tests.

So.. is there a way to acomplish this? I can't find anything.

Thank you!

答案1

得分: 1

cy.session() should get you most of the way there. 它允许您创建一个session,从中Cypress将缓存大部分浏览器上下文。下次Cypress遇到cy.session()命令,它将检查是否已经创建了具有相同id的会话,如果是的话,它将使用这些缓存的值。否则,它执行cy.session()命令的内容。

From the docs:

缓存和恢复cookies、localStorage和sessionStorage(即会话数据),以便在测试之间重新创建一致的浏览器上下文。

一旦创建,给定id的会话将在规范文件的持续时间内被缓存。您不能在被缓存后修改存储的会话,但您可以随时使用不同的id创建新的会话。
为了减少开发时间,在"open"模式下运行Cypress时,会话将被缓存以进行规范文件的重新运行。要在多个规范文件之间保留会话,使用选项cacheAcrossSpecs=true

以及他们在cy.session内部使用的示例,用于登录命令。

Cypress.Commands.add('login', (username, password) => {
  cy.session([username, password], () => {
    cy.visit('/login')
    cy.get('[data-test=name]').type(username)
    cy.get('[data-test=password]').type(password)
    cy.get('form').contains('Log In').click()
    cy.url().should('contain', '/login-successful')
  })
})
英文:

cy.session() should get you most of the way there. It allows you create a session, from which Cypress will cache most of the browser context. The next time Cypress encounters the session command, it will check to see if it has created a session with the same id, and if it has, it will use those cached values. Otherwise, it executes the contents of the cy.session() command.

From the docs:
> Cache and restore cookies, localStorage, and sessionStorage (i.e. session data) in order to recreate a consistent browser context between tests.

>Once created, a session for a given id is cached for the duration of the spec file. You can't modify a stored session after it has been cached, but you can always create a new session with a different id.
In order to reduce development time, when running Cypress in "open" mode, sessions will be cached for spec file reruns. To persist a session across multiple specs, use the option cacheAcrossSpecs=true

And their example using cy.session inside of a login command.

Cypress.Commands.add('login', (username, password) => {
  cy.session([username, password], () => {
    cy.visit('/login')
    cy.get('[data-test=name]').type(username)
    cy.get('[data-test=password]').type(password)
    cy.get('form').contains('Log In').click()
    cy.url().should('contain', '/login-successful')
  })
})

huangapple
  • 本文由 发表于 2023年2月6日 20:28:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361316.html
匿名

发表评论

匿名网友

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

确定