typescript - 如何使用 Cypress 存储所有测试中的 session

标签 typescript cypress

我目前正在尝试学习 Cypress 10,我遇到了一个问题,我发现我所做的每一次 it 都会清除 cookie,这不是我想要的,这是我的代码...

Suite('My First Test', () => {
  before(() => {
    prepare();

    cy.visit('https://barrythrill.com');

    cy.get('.start-from-zero').should('be.visible').click();

    ...
  });

  Scenario('My first test cases', () => {
    Given('we are on the webpage');

    When('we save the list', () => {
          // no cy.visit
      ... // we save the list it will automatically login and it creates a cookie called BAR (has a dynamic cookie)
    });

    Then('we are able to open all-list and close it', () => {
      ... // no cy.visit
    });

    When('we remove the list through API', () => {
      ... // no cy.visit
    });
    Then('we should still be logged it', () => {
        ... // cy.visit(''https://barrythrill.com')
            // We should still be logged in through ` When('we save the list', ()`
    });
  });
});

我无法弄清楚如何在此处使用 cy.session 或为我的测试用例创建正确的测试。我想知道这里是否有人可以帮助我如何改进我的代码,让我仍然可以通过整个测试登录,或者如何实现 cy.session 而无需为每个 it 调用 cy.visit?

最佳答案

我认为 prepare() 正在获取您的 cookie?

我认为你需要做的是添加一个 beforeEach()cy.session() 并在其中调用 prepare() .

beforeEach(() => {
  cy.session('prepare', () => {
    prepare();
  })
})

每次 it() 都会调用 session ,但仅在第一次调用 prepare() 时 - 之后它会从缓存中设置 cookie。

您可能仍然需要 before() 按原样允许 cy.visit() 工作。

在这里,我从 Cypress 食谱页面的顶部开始,test1 最终到达博客部分,这也是 test2 开始的地方。


在测试之间保留位置

不确定这是否适用于 cucumber ,或者是否有任何访问会扰乱您的测试流程。

Cypress 建议在 cy.session() 之后立即执行 cy.visit(),如以下示例所示。

您可以使用 afterEach() 保留每次测试后的最后位置。下一个 beforeEach() 调用中的访问将使用该位置。

Cypress.session.clearAllSavedSessions()

let location = 'https://docs.cypress.io/examples/examples/recipes'

beforeEach(() => {
  cy.session('cookies', () => {
    cy.setCookie('test', 'my-cookie')
  })
  cy.visit(location)
})

afterEach(() => {
  location = cy.state('window').location.href
})

it('test1', () => {
  expect(cy.state('window').location.href)
    .to.eq('https://docs.cypress.io/examples/examples/recipes')

  cy.getCookie('test').then(cookie => {
    expect(cookie.value).to.eq('my-cookie')
  })

  // navigate elsewhere
  cy.visit('https://docs.cypress.io/examples/examples/recipes#Blogs')
});

it('test2', () => {
  expect(cy.state('window').location.href)
    .to.eq('https://docs.cypress.io/examples/examples/recipes#Blogs')

  cy.getCookie('test').then(cookie => {
    expect(cookie.value).to.eq('my-cookie')
  })
});

关于typescript - 如何使用 Cypress 存储所有测试中的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72797797/

相关文章:

version - 如何检查我通过命令行安装的 Cypress 版本

angular - 为什么我不能将字符串传递给@input

javascript - 在 typescript 类中使用 "this"

javascript - Typescript 类型转换现有变量

Dockerfile:是否允许多个 FROM?

html - Cypress 测试用例 : Select only text of an element (not the text of its children/descendants)

vue.js - Cypress Vue 组件测试从已挂载发出的事件

javascript - typescript 在函数中传递变量

typescript - 在版本 11.0.0 及更高版本中找不到模块 'highcharts' 的声明文件

javascript - 从 Cypress 的前五个 google 搜索结果中关注我的网站链接